rorono
6/24/2016 - 8:57 AM

AWS S3のキャッシュコントロール(cache control)を一括で書き換えるスクリプト - Script be rewritten in bulk cache control of AWS S3

AWS S3のキャッシュコントロール(cache control)を一括で書き換えるスクリプト - Script be rewritten in bulk cache control of AWS S3

#! /usr/bin/env ruby

require 'aws-sdk'

REGION = "ap-northeast-1"
ACCESS_KEY_ID = ""
SECRET_ACCESS_KEY = ""
BUCKET_NAME = ""
CACHE_CONTROL = "max-age=86400" # one day

Aws.config.update({
  region: REGION,
  credentials: Aws::Credentials.new(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
});

s3 = Aws::S3::Resource.new

# reference an existing bucket by name
bucket = s3.bucket(BUCKET_NAME)

# enumerate every object in a bucket
bucket.objects.each do |object|

  object.copy_from(object, {
    cache_control: CACHE_CONTROL,
    metadata_directive: "REPLACE"
  })

  puts "#{object.key} => cache_control: \"#{CACHE_CONTROL}\""
end

AWS S3のキャッシュコントロール(cache control)を一括で書き換えるスクリプト - Script be rewritten in bulk cache control of AWS S3

リソース配信用AWS S3のバケットのメタデータを一括で書き換えるスクリプト

使い方 - Usage

  1. Enter the ACCESS_KEY_ID, SECRET_ACCESS_KEY and BUCKET_NAME.
  2. Run this script
    in this way
$ ruby main.rb

参考資料 - Reference materials

Copyright (c) 2016 Kosuke Akizuki
Released under the MIT license
http://opensource.org/licenses/mit-license.php