Aws-sdk-ruby: Is there a way to provide content-disposition option for #public_url?

Created on 11 Mar 2015  路  2Comments  路  Source: aws/aws-sdk-ruby

I'm generating the url by using public_url method but when I click on the url the content is getting showed in browser. Is there a way to provide content-disposition option for public_url?
Below is the way am storing the csv file in s3 and generating the public_url.

s3_credentials = Aws::Credentials.new(S3_CREDENTIALS['access_key_id'],S3_CREDENTIALS['secret_access_key'])

s3_bucket = Aws::S3::Resource.new(region: S3_CREDENTIALS['region'], credentials: s3_credentials).bucket(S3_CREDENTIALS['bucket'])

s3_obj = s3_bucket.object(s3_file_path)

s3_obj.upload_file(file_name, acl: 'public-read')

s3_obj.public_url

guidance

Most helpful comment

From the Amazon S3 API reference docs for GET Object:

There are times when you want to override certain response header values in a GET response. For example, you might override the Content-Disposition response header value in your GET request.

You can override values for a set of response headers using the query parameters listed in the following table. These response header values are sent only on a successful request, that is, when status code 200 OK is returned. The set of headers you can override using these parameters is a subset of the headers that Amazon S3 accepts when you create an object. The response headers that you can override for the GET response are Content-Type, Content-Language, Expires, Cache-Control, Content-Disposition, and Content-Encoding. To override these header values in the GET response, you use the request parameters described in the following table.

Note
You must sign the request, either using an Authorization header or a pre-signed URL, when using these parameters. They cannot be used with an unsigned (anonymous) request.

Emphasis added was mine. This means the #public_url method will not be able to provide what you are looking for. Instead, you should look at the #presigned_url method:

s3 = Aws::S3::Resource.new
obj = s3.bucket('aws-sdk').object('key')
obj.presigned_url(:get, response_content_disposition: '...')
#=> "https://aws-sdk.s3.amazonaws.com/key?response-content-disposition=..."

Because this is a presigned url, it will expire. You can specify how long the URL is valid for. By default it expires after 900 seconds (15 minutes).

All 2 comments

From the Amazon S3 API reference docs for GET Object:

There are times when you want to override certain response header values in a GET response. For example, you might override the Content-Disposition response header value in your GET request.

You can override values for a set of response headers using the query parameters listed in the following table. These response header values are sent only on a successful request, that is, when status code 200 OK is returned. The set of headers you can override using these parameters is a subset of the headers that Amazon S3 accepts when you create an object. The response headers that you can override for the GET response are Content-Type, Content-Language, Expires, Cache-Control, Content-Disposition, and Content-Encoding. To override these header values in the GET response, you use the request parameters described in the following table.

Note
You must sign the request, either using an Authorization header or a pre-signed URL, when using these parameters. They cannot be used with an unsigned (anonymous) request.

Emphasis added was mine. This means the #public_url method will not be able to provide what you are looking for. Instead, you should look at the #presigned_url method:

s3 = Aws::S3::Resource.new
obj = s3.bucket('aws-sdk').object('key')
obj.presigned_url(:get, response_content_disposition: '...')
#=> "https://aws-sdk.s3.amazonaws.com/key?response-content-disposition=..."

Because this is a presigned url, it will expire. You can specify how long the URL is valid for. By default it expires after 900 seconds (15 minutes).

馃憤

Was this page helpful?
0 / 5 - 0 ratings