LoginSignup
0
0

More than 5 years have passed since last update.

rails: AWS s3から画像を一括でダウンロードする

Last updated at Posted at 2018-10-20

S3からローカルにdownloadするには send_fileメソッドが有名だけどコネクションが一回切りなので繰り返し文にして複数回するのは無理、つまりファイル一枚のみ。

それでS3側でファイルを圧縮してくれるgem zipline を発見しました。

include ActionController::Streaming
include Zipline
def s3all
      myBacket = 'backet-name'
      bucket = Aws::S3::Client.new(
             :region => 'ap-northeast-1',
              :access_key_id => 'AKIAxxxxxxxx',
              :secret_access_key => 'xxxxxxxxxxxxxxxx'
             )
      lists = []
      bucket.list_objects(:bucket => myBacket).contents.each do |b|
        lists.push(b)
      end
      lists.lazy.map do |list|
        s3_object = bucket.get_object(bucket: myBacket, key: list.key)
        [s3_object.body, list.key+".png"]
      end

これでbacket内のすべての画像をダウンロード出来ます。

bucket.list_objects(:bucket => myBacket, :prefix => "name_no#{@user.id}").contents.each do |b|

とかプレフィックスを利用する事もできる。

ただ、解凍時に警告が出るのはなぜなんだろうか?

0
0
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
0
0