给定一个S3路径和有效的密钥和秘钥,我如何更新对象的缓存控制头?

huangapple go评论139阅读模式
英文:

Given an S3 path and a valid key and secret, how do I update an objects cache-control headers?

问题

我需要在文件上传到S3后更新文件的头部信息。我无法控制上传过程(我正在使用FilePicker.io API,据我所知,它没有提供指定缓存控制头部的方法),它们只是神奇地出现在一个存储桶中。我有对象的完整S3路径以及存储桶的密钥和密钥。

使用Go语言,最简单的方法是如何向这些对象添加新的头部信息?似乎需要进行PUT复制请求,但这需要请求签名,并且会覆盖所有现有的头部信息。我只想添加一个缓存控制头部,肯定有更简单的方法,对吗?

英文:

I need to update the headers on files after they are uploaded to S3. I don't have control over the upload process (I'm using the FilePicker.io API which doesn't provide a way to specify the cache-control header as far as I now), they just magically appear in a bucket. I have the full s3 path to the objects and the key and secret for the bucket.

Using Go, what is the easiest way to add new headers to these objects? Seems like you need to do a PUT copy request but that requires request signing and it overwrites all of the existing headers. All I want to do is add a cache-control header, there has to be an easier way right?

答案1

得分: 1

下面的小程序应该只是向给定的存储桶/键组合添加一个缓存控制头。重要的部分是s3.CopyOptions结构体。MetadataDirective也可以是COPY-请参阅http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html了解详细信息。
此外,源必须是bucket/key,因为源当然可以在另一个bucket中。

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/goamz/goamz/aws"
  6. "github.com/goamz/goamz/s3"
  7. //// should work as well
  8. //"github.com/crowdmob/goamz/aws"
  9. //"github.com/crowdmob/goamz/s3"
  10. )
  11. func main() {
  12. // use as
  13. // $ go run s3meta.go bucket key
  14. // will add a 1 hour Cache-Control header to
  15. // key in bucket
  16. auth := aws.Auth{
  17. AccessKey: os.Getenv("AWS_ACCESS_KEY_ID"),
  18. SecretKey: os.Getenv("AWS_SECRET_KEY_ID"),
  19. }
  20. bucketName, keyName := os.Args[1], os.Args[2]
  21. bucket := s3.New(auth, aws.USEast).Bucket(bucketName)
  22. opts := s3.CopyOptions{}
  23. opts.CacheControl = "maxage=3600"
  24. opts.MetadataDirective = "REPLACE"
  25. _, err := bucket.PutCopy(keyName, s3.PublicRead, opts, bucketName+"/"+keyName)
  26. if err != nil {
  27. panic(err)
  28. }
  29. }

试运行(存储桶已被删除):

  1. ╭─brs at stengaard in ~/ using
  2. ╰─○ curl -I https://s3.amazonaws.com/cf-templates-1r14by1vl75o0-us-east-1/success.png
  3. HTTP/1.1 200 OK
  4. x-amz-id-2: 49oTuRARMHlx32nqv34CMOjdTMBUCZIVzP8YKBS2Wz5h1w5KBG62u8nFru1UkIbJ
  5. x-amz-request-id: C92E9952BFF31D77
  6. Date: Mon, 30 Jun 2014 08:57:15 GMT
  7. Last-Modified: Mon, 30 Jun 2014 08:50:45 GMT
  8. ETag: "41b9951893f1bbff89e2b9c8a5b7ea18"
  9. Accept-Ranges: bytes
  10. Content-Type: image/png
  11. Content-Length: 61585
  12. Server: AmazonS3
  13. ╭─brs at stengaard in ~/ using
  14. ╰─○ go run s3meta.go cf-templates-1r14by1vl75o0-us-east-1 success.png
  15. ╭─brs at stengaard in ~/ using
  16. ╰─○ curl -I https://s3.amazonaws.com/cf-templates-1r14by1vl75o0-us-east-1/success.png
  17. HTTP/1.1 200 OK
  18. x-amz-id-2: oiDeXjO1V4kquWo8UlNWBi/HAHoqfvlOSHVeXFZXv2yA4o0+Njcdshhu15PIiw7J
  19. x-amz-request-id: 0BB1A397DE7EBE75
  20. Date: Mon, 30 Jun 2014 09:00:17 GMT
  21. Cache-Control: maxage=3600
  22. Last-Modified: Mon, 30 Jun 2014 09:00:12 GMT
  23. ETag: "41b9951893f1bbff89e2b9c8a5b7ea18"
  24. Accept-Ranges: bytes
  25. Content-Type: binary/octet-stream
  26. Content-Length: 61585
  27. Server: AmazonS3

请注意,Content-Type也会更改,因为我们设置了opts.MetadataDirective = "REPLACE"。是否值得为了更新头部而麻烦一番,这实际上取决于特定的领域:在客户端中缓存上传的文件有多重要?执行到S3的HEAD请求是否太昂贵?

英文:

The small program below should simply add a cache-control header to the the given bucket / key combo. The important bit is the s3.CopyOptions struct. The MetadataDirective can also be COPY - see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html for details.
Also the source must be bucket/key since the source of course can be in another bucket.

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/goamz/goamz/aws"
  6. "github.com/goamz/goamz/s3"
  7. //// should work as well
  8. //"github.com/crowdmob/goamz/aws"
  9. //"github.com/crowdmob/goamz/s3"
  10. )
  11. func main() {
  12. // use as
  13. // $ go run s3meta.go bucket key
  14. // will add a 1 hour Cache-Control header to
  15. // key in bucket
  16. auth := aws.Auth{
  17. AccessKey: os.Getenv("AWS_ACCESS_KEY_ID"),
  18. SecretKey: os.Getenv("AWS_SECRET_KEY_ID"),
  19. }
  20. bucketName, keyName := os.Args[1], os.Args[2]
  21. bucket := s3.New(auth, aws.USEast).Bucket(bucketName)
  22. opts := s3.CopyOptions{}
  23. opts.CacheControl = "maxage=3600"
  24. opts.MetadataDirective = "REPLACE"
  25. _, err := bucket.PutCopy(keyName, s3.PublicRead, opts, bucketName+"/"+keyName)
  26. if err != nil {
  27. panic(err)
  28. }
  29. }

Trial run (bucket has since been deleted):

  1. ╭─brs at stengaard in ~/ using
  2. ╰─○ curl -I https://s3.amazonaws.com/cf-templates-1r14by1vl75o0-us-east-1/success.png
  3. HTTP/1.1 200 OK
  4. x-amz-id-2: 49oTuRARMHlx32nqv34CMOjdTMBUCZIVzP8YKBS2Wz5h1w5KBG62u8nFru1UkIbJ
  5. x-amz-request-id: C92E9952BFF31D77
  6. Date: Mon, 30 Jun 2014 08:57:15 GMT
  7. Last-Modified: Mon, 30 Jun 2014 08:50:45 GMT
  8. ETag: "41b9951893f1bbff89e2b9c8a5b7ea18"
  9. Accept-Ranges: bytes
  10. Content-Type: image/png
  11. Content-Length: 61585
  12. Server: AmazonS3
  13. ╭─brs at stengaard in ~/ using
  14. ╰─○ go run s3meta.go cf-templates-1r14by1vl75o0-us-east-1 success.png
  15. ╭─brs at stengaard in ~/ using
  16. ╰─○ curl -I https://s3.amazonaws.com/cf-templates-1r14by1vl75o0-us-east-1/success.png
  17. HTTP/1.1 200 OK
  18. x-amz-id-2: oiDeXjO1V4kquWo8UlNWBi/HAHoqfvlOSHVeXFZXv2yA4o0+Njcdshhu15PIiw7J
  19. x-amz-request-id: 0BB1A397DE7EBE75
  20. Date: Mon, 30 Jun 2014 09:00:17 GMT
  21. Cache-Control: maxage=3600
  22. Last-Modified: Mon, 30 Jun 2014 09:00:12 GMT
  23. ETag: "41b9951893f1bbff89e2b9c8a5b7ea18"
  24. Accept-Ranges: bytes
  25. Content-Type: binary/octet-stream
  26. Content-Length: 61585
  27. Server: AmazonS3

Note that Content-Type changes as well since we have opts.MetadataDirective = "REPLACE". If this little thing is worth the hassle of updating headers out-of-band is really domain specific: How important is it to cache the uploaded files in the client? Is it to expensive to do the HEAD request to S3?

huangapple
  • 本文由 发表于 2014年6月28日 05:03:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/24460749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定