7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RubyでAmazon S3のファイルに対して期限付きURLを発行する

Last updated at Posted at 2015-10-30

参考にしようとしたサイトが大概aws-sdkのバージョンが古いようで動かないので、自分で書くことにする。なお、Amazon S3にファイルはアップロード済みとする。

インストール

まずはaws-sdkのバージョン2をインストール。

Gemfile
gem 'aws-sdk', '~> 2'

試す

rails consoleで試してみた。

bin/rails c

ここからrails console。
アクセスキー等は適宜発行して置き換えてください。
リージョンは東京(ap-northeast-1)を指定。
バケットとファイル名を指定して、有効期限(expires_in)に60秒を設定しています。expires_inを省略した場合、デフォルトは15分の模様。

rails_console
Aws.config = { access_key_id: 'ACCESS_KEY_ID', secret_access_key: 'SECRET_ACCESS_KEY' }
s3 = Aws::S3::Client.new(region: 'ap-northeast-1')
signer = Aws::S3::Presigner.new(client: s3)
puts signer.presigned_url(:get_object, bucket: 'bucket_name', key: 'filename.png', expires_in: 60)

これで出力されたURLが、期限付きURLになります。

ブラウザでコピペして表示されたらOK。
1分後のアクセスして表示されなかったらOK。

参考URL

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?