Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pwsh for upload, and mime type #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,47 @@ resource "azurerm_storage_account" "storeacc" {
resource "null_resource" "copyfilesweb" {
count = var.upload_to_static_website ? 1 : 0
provisioner "local-exec" {
command = "az storage blob upload-batch --no-progress --account-name ${azurerm_storage_account.storeacc.name} -s ${var.static_website_source_folder} -d '$web' --output none"
command = <<EOT
function Get-MimeType {
param(
[string] $FileName
)

$map=@{
".jpg" = "image/jpeg";
".jpeg" = "image/jpeg";
".gif" = "image/gif";
".png" = "image/png";
".tiff" = "image/tiff";
".zip" = "application/zip";
".json" = "application/json";
".js" = "application/javascript";
".html" = "text/html";
".rar" = "application/x-rar-compressed";
".gzip" = "application/x-gzip";
".gz" = "application/x-gzip";
".svg" = "image/svg+xml";
".pdf" = "application/pdf";
".xml" = "application/xml";
".xls" = "application/vnd.ms-excel";
".xlsx" = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
$File=Get-ChildItem "$FileName"
$map[$File.Extension.ToLower()] ? $map[$File.Extension.ToLower()] : "application/octet-stream" ;
}
# Build a context using storage primary key
$c=New-AzStorageContext -StorageAccountName ${azurerm_storage_account.storeacc.name} -StorageAccountKey ${azurerm_storage_account.storeacc.primary_access_key}
# Build a 1 hour token and give it full access to the blob
$sas=New-AzStorageAccountSASToken -Service Blob -ResourceType Service,Container,Object -Permission 'rwdl' -ExpiryTime (Get-Date).AddHours(1) -Context $c
# Upload directory's full content
# File by file, to get mime type
# Probably slow with many files...
Foreach ($file in (Get-ChildItem -File -Recurse ${var.static_website_source_folder} ) ) {
Set-AzStorageBlobContent -Container '$web' -Context $c -Properties @{"ContentType" = (Get-MimeType -FileName "$file")} -Force -File $file
}

EOT
interpreter = ["pwsh", "-Command"]
}
}

Expand Down Expand Up @@ -99,7 +139,7 @@ resource "azurerm_cdn_endpoint" "cdn-endpoint" {
}

resource "null_resource" "add_custom_domain" {
count = var.custom_domain_name != null ? 1 : 0
count = var.custom_domain_name != null ? 1 : 0
triggers = { always_run = timestamp() }
depends_on = [
azurerm_cdn_endpoint.cdn-endpoint
Expand Down