Skip to content

Instantly share code, notes, and snippets.

@linyows
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linyows/4ea00946b8d585290ad8 to your computer and use it in GitHub Desktop.
Save linyows/4ea00946b8d585290ad8 to your computer and use it in GitHub Desktop.
S3のリソースのhttpヘッダーにcache系追加
#!/usr/bin/env ruby
require 'aws-sdk-core'
require 'parallel'
require 'active_support/time'
require 'awesome_print'
ENV['AWS_ACCESS_KEY_ID'] = 'xxxxxxxxxxxxxxx'
ENV['AWS_SECRET_ACCESS_KEY'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
region = 'ap-northeast-1'
bucket = 'foo'
resource = 'bar'
host = 'http://foo.s3.amazonaws.com/'
s3 = Aws::S3::Client.new(region: region)
arr = File.open("./lists/#{resource}.txt").read.split("\n")
total = arr.length
Parallel.each_with_index(arr, in_threads: 8) do |key, i|
begin
puts "#{i}/#{total}: #{host}#{key}"
head = s3.head_object(bucket: bucket, key: key)
s3.copy_object(
bucket: bucket,
key: key,
content_type: head.content_type,
cache_control: "max-age=#{10.years.to_i}",
expires: "#{10.years.from_now.httpdate}",
copy_source: "#{bucket}/#{key}",
acl: 'public-read',
metadata_directive: 'REPLACE'
)
rescue => e
ap e
end
end
$ aws s3 ls --recursive s3://foo/bar > ./lists/bar.txt
@linyows
Copy link
Author

linyows commented Sep 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment