読者です 読者をやめる 読者になる 読者になる

Amazon S3からフォルダの中身を一括でダウンロードするスクリプト

個人向け備忘録。

#!/usr/bin/env ruby

require 'aws-sdk'
require 'json'

creds = JSON.parse(File.read("credentials.json"))

Aws.config.update({
  credentials: Aws::Credentials.new(creds["aws_access_key_id"], creds["aws_secret_access_key"])
})

s3 = Aws::S3::Resource.new(region: 'ap-northeast-1')
bucket = s3.bucket('YOUR_BUCKET_NAME')
target_objects = bucket.objects({prefix: "PATH/TO/FOLDER/"})

if !Dir.exists?('download')
  Dir.mkdir('download')
end
`rm -f download/*`

# download
target_objects.each do |obj|
  fname = obj.key.split("/").last
  obj.get({response_target: "download/" + fname})
  puts "Downloaded #{fname}"
end
{
  "aws_access_key_id"     : "AKIA***",
  "aws_secret_access_key" : "***"
}