LoginSignup
9
6

More than 5 years have passed since last update.

Google Cloud Storageのファイルを指定したファイル名でダウンロードしたい

Last updated at Posted at 2017-05-30

背景

1. Upload時にDLファイル名を指定する方法


@api = Google::Cloud::Storage.new(project: $PROJECT_ID)
bucket = @api.bucket $BUCKET_NAME
raise("bucket not exist. bucket: #{$BUCKET_NAME}") if bucket.nil?
bucket.create_file($FILE_PATH,
                   $STORAGE_PATH,
                   content_disposition: "attachment; filename=#{$DL_FILE_NAME}")

2. signed_urlの生成時に指定する場合


@api = Google::Cloud::Storage.new(project: $PROJECT_ID) bucket = @api.bucket $BUCKET_NAME
raise("bucket not exist. bucket: #{$BUCKET_NAME}") if bucket.nil?
file = bucket.file $STORAGE_PATH
raise("file not exist. file: #{$STORAGE_PATH}") if file.nil?
url = file.signed_url(method: "GET", expires: 60 * 60 * 24)
url + "&response-content-disposition=attachment%3B%20filename%3D%22#{$DL_FILE_NAME}%22"

# ex. https://storage.googleapis.com/$BUCKET/$PUT_STORAGE_PATH?GoogleAccessId=xxx&Expires=xxx&response-content-disposition=attachment%3B%20filename%3D%22$DL_FILE_NAME%22
  • $PROJECT_ID: プロジェクトID
  • $BUCKET_NAME: バケット名
  • $FILE_PATH: アップロード対象ファイル
  • $STORAGE_PATH: アップロード先のパス指定
  • $DL_FILE_NAME: DL時のファイル名

参照

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