2

I am trying to upload folders to S3 to serve as static website. The problem I have is that within the folders, there are files of certain extensions such as .css, .png, .svg, etc. I am using AWS PowerShell tools and when I upload the following command, the .svg files are uploaded with wrong content-type.

Write-S3Object -BucketName bucket-name -Folder folderName -Recurse -Force -KeyPrefix folderName -CannedACLName NoACL

As a workaround, I came up with is to iterate the folders and do not upload the .svg files. The re-iterate the folders and only upload .svg setting up correct -content-type

Write-S3Object -BucketName bucket-name -Folder folderName -Recurse -Force -KeyPrefix folderName -CannedACLName NoACL -SearchPattern "*.svg" -ContentType "image/svg+xml"

So I have two questions here.

1) Instead of doing the two step approach, can I upload all the files in one go setting up correct content-type for svg files?

2) Is there a away to specify files to exclude in -SearchPattern parameter of Write-S3Object?

Thanks.

Junaid
  • 1,708
  • 16
  • 25

2 Answers2

1

First thing is that I cannot use -ContentType "image/svg+xml" when uploading the folder since it doesn't apply the content-type to the files.

So I took a two step approach; first uploading all files including SVG with incorrect Content-Type. This is to create the folders in case the folder only has SVG files.

Write-S3Object -BucketName react-ui -Folder $i.FullName -Recurse -KeyPrefix $i.Name -CannedACLName NoACL

After that, I re-upload the SVG file with correct Content-Type of /svg+xml". The -Key parameter is really important here since you can define the path of target folder using this.

foreach ($i in Get-ChildItem $path -Recurse -Include "*.svg") 
{
  $filename = [System.IO.Path]::GetFileName($i)
  $awsPath = $i.DirectoryName.Substring($i.DirectoryName.IndexOf($path) + $path.Length)
  $fileWithPath = $awsPath + "\"+ $filename
  Write-S3Object -BucketName react-ui -File $i -Key $fileWithPath -CannedACLName NoACL -ContentType "image/svg+xml"
}
Junaid
  • 1,708
  • 16
  • 25
-1

You can add content-typeto the files you upload to S3. See: S3 PUT API - however that means you'd be using the S3 SDK provided by AWS plus some coding experience.

To avoid coding, use S3 Browser that can be configured with a specific mime-type when you manually upload any files. SVG is already supported by S3 Browser, so you don't need to do anything, just upload with this tool.

Matt
  • 74,352
  • 26
  • 153
  • 180
Alvin K.
  • 4,329
  • 20
  • 25