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


i need update headers on files after uploaded s3. don't have control on upload process (i'm using filepicker.io api doesn't provide way specify cache-control header far now), magically appear in bucket. have full s3 path objects , key , secret bucket.

using go, easiest way add new headers these objects? seems need put copy request requires request signing , overwrites of existing headers. want add cache-control header, there has easier way right?

the small program below should add cache-control header the given bucket / key combo. important bit s3.copyoptions struct. metadatadirective can copy - see http://docs.aws.amazon.com/amazons3/latest/api/restobjectcopy.html details. source must bucket/key since source of course can in bucket.

package main  import (     "fmt"     "os"      "github.com/goamz/goamz/aws"     "github.com/goamz/goamz/s3"     //// should work     //"github.com/crowdmob/goamz/aws"     //"github.com/crowdmob/goamz/s3" )  func main() {     // use     //  $ go run s3meta.go bucket key     // add 1 hour cache-control header     // key in bucket     auth := aws.auth{         accesskey: os.getenv("aws_access_key_id"),         secretkey: os.getenv("aws_secret_key_id"),     }      bucketname, keyname := os.args[1], os.args[2]      bucket := s3.new(auth, aws.useast).bucket(bucketname)     opts := s3.copyoptions{}     opts.cachecontrol = "maxage=3600"     opts.metadatadirective = "replace"      _, err := bucket.putcopy(keyname, s3.publicread, opts, bucketname+"/"+keyname)     if err != nil {         panic(err)     }  } 

trial run (bucket has since been deleted):

╭─brs @ stengaard in ~/ using ╰─○ curl  -i https://s3.amazonaws.com/cf-templates-1r14by1vl75o0-us-east-1/success.png http/1.1 200 ok x-amz-id-2: 49oturarmhlx32nqv34cmojdtmbuczivzp8ykbs2wz5h1w5kbg62u8nfru1ukibj x-amz-request-id: c92e9952bff31d77 date: mon, 30 jun 2014 08:57:15 gmt last-modified: mon, 30 jun 2014 08:50:45 gmt etag: "41b9951893f1bbff89e2b9c8a5b7ea18" accept-ranges: bytes content-type: image/png content-length: 61585 server: amazons3  ╭─brs @ stengaard in ~/ using ╰─○ go run s3meta.go cf-templates-1r14by1vl75o0-us-east-1 success.png ╭─brs @ stengaard in ~/ using ╰─○ curl  -i https://s3.amazonaws.com/cf-templates-1r14by1vl75o0-us-east-1/success.png http/1.1 200 ok x-amz-id-2: oidexjo1v4kquwo8ulnwbi/hahoqfvloshvexfzxv2ya4o0+njcdshhu15piiw7j x-amz-request-id: 0bb1a397de7ebe75 date: mon, 30 jun 2014 09:00:17 gmt cache-control: maxage=3600 last-modified: mon, 30 jun 2014 09:00:12 gmt etag: "41b9951893f1bbff89e2b9c8a5b7ea18" accept-ranges: bytes content-type: binary/octet-stream content-length: 61585 server: amazons3 

note content-type changes since have opts.metadatadirective = "replace". if little thing worth hassle of updating headers out-of-band domain specific: how important cache uploaded files in client? expensive head request s3?


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -