11
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS SDK for Rubyを使ってS3オブジェクトにCache-Controlヘッダを追加する

Last updated at Posted at 2014-01-14

S3オブジェクトにCache-Controlヘッダを追加したいんだけど、AWS::S3::Object#metadataだとx-amz-metaってprefixが付いて困った。
解決策はObject#writeのオプションに指定すればよかった。
#move_toや#copy_toでも同じようにオプション指定ができる。

コードはざっくりこんな感じ

sample.rb
require "aws"
s3 = AWS::S3.new
bucket = s3.buckets["my-bucket"]
object = bucket.objects["obj-key"]

object.write(data, cache_control: "max-age=3600")

これだとデータのアップロードも走るんだけど、すでにS3に上がってるオブジェクトに対してCache-Controlだけ追加したいと思ったらどうするんだろ・・・。object.write(object.read, cache_control: "max-age=...") ・・・?まさかね。
今度調べてみよう。

追記
#copy_fromを使ってsourceに同一objectを指定することで、アップロードせずにCache-Controlを付けることが出来た

sample2.rb
require "aws"
s3 = AWS::S3.new
bucket = s3.buckets["my-bucket"]
object = bucket.objects["obj-key"]

object.copy_from(object, cache_control: "max-age=3600")
11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?