How can I set the Cache-Control when using Write-S

2019-07-23 20:58发布

问题:

I am using Windows Powershell for AWS and I have tried the following:

Write-S3Object 
   -BucketName 'user-ab-staging' 
   -KeyPrefix 'content/css' 
   -Folder 'content/css' 
   -SearchPattern '*.css' 
   -Metadata @{"Cache-Control" = "Value"}
   -CannedACLName PublicRead

It gives me a very strange error and only tries to load one css file:

Uploaded 1 object(s) to bucket 'user-ab-staging' from 'C:\g\ab-user\WebUserApp\content\css' with keyprefix
 'content/css'
Write-S3Object :
At line:1 char:1
+ Write-S3Object `
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Amazon.PowerShe...eS3ObjectCmdlet:WriteS3ObjectCmdlet) [Write-S3Objec
   t], InvalidOperationException
    + FullyQualifiedErrorId : Amazon.S3.AmazonS3Exception,Amazon.PowerShell.Cmdlets.S3.WriteS3ObjectCmdlet

Can anyone help tell me what is wrong with this and how I can set the Cache data for the object when I am using Write-S3Object and the Powershell extension for AWS?

回答1:

I set up some test folders locally and on S3 and can replicate this issue exactly.

After some digging/experimenting... it relates to the CannedACLName parameter. The files upload successfully when removing that parameter.

This documentation says that the correct option to use is PublicRead, but the correct option is actually public-read. After making that change, I was able to successfully write all objects in the specified folder.

Your updated code would look like this:

Write-S3Object 
   -BucketName 'user-ab-staging' 
   -KeyPrefix 'content/css' 
   -Folder 'content/css' 
   -SearchPattern '*.css' 
   -Metadata @{"Cache-Control" = "Value"}
   -CannedACLName public-read

If you need to use any of the other ACL options in the future, I would recommend trying from this list in the ACL documentation instead. I've tested a few of the options available there and they've all worked.



回答2:

Note that using the -Metadata argument for Write-S3Object sets the "x-amz-meta-cache-control" metadata property. If you want to set the "Cache-Control" (as for HTTP response headers for S3 website hosting), use this argument pattern instead:

-HeaderCollection @{"Cache-Control" = "Value"}