Last active
August 29, 2015 14:06
-
-
Save linyows/4ea00946b8d585290ad8 to your computer and use it in GitHub Desktop.
S3のリソースのhttpヘッダーにcache系追加
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ aws s3 ls --recursive s3://foo/bar > ./lists/bar.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/aws/aws-sdk-core-ruby/blob/b3a39dad428cc2d4ff810c79805d952f416b5ab6/aws-sdk-core/lib/seahorse/model/shapes.rb#L391