168

I have files stored on S3 with a GUID as the key name.

I am using a pre signed URL to download as per S3 REST API

I store the original file name in my own Database. When a user clicks to download a file from my web application I want to return their original file name, but currently all they get is a GUID. How can I achieve this?

My web app is in salesforce so I do not have much control to do response.redirects all download the file to the web server then rename it due to governor limitations.

Is there some HTML redirect, meta refresh, Javascript I can use? Is there some way to change the download file name for S3 (the only thing I can think of is coping the object to a new name, downloading it, then deleting it).

I want to avoid creating a bucket per user as we will have a lot of users and still no guarantee each file with in each bucket will have a unique name

Any other solutions?

3
  • 14
    Another legit SO question that's closed as off-topic! The question and answers too have more votes than a vast majority of usual questions/answers... Commented Apr 3, 2013 at 3:54
  • 2
    Yes, it's obviously more important that we try to organize things somewhat arbitrarily in the face of the fact it's not guaranteed to make sense in the future or even now. What happened when people just had fun asking and answering questions related to the software industry? Who actually finds the answer because of this organization? Who finds any organizational structure in software consistent and clearly defined across the industry? Without this clear definition that will last a significant amount of time organizing it is a fairly stupid waste of time...
    – Derek Litz
    Commented Sep 21, 2013 at 20:46
  • 2
    I find this answer, I have no issue with this organization. Commented Jun 8, 2014 at 18:11

11 Answers 11

122

I guess your cross posted this questions to Amazon S3 forum, but for the sake of others I'd like to post the answer here:

If there is only ever one "user filename" for each S3 object, then you can set the Content-Disposition header on your s3 file to set the downloading filename:

Content-Disposition: attachment; filename="foo.bar"

For the sake of fairness I'd like to mention that it was not me to provide the right answer on Amazon forum and all credits should go to Colin Rhodes ;-)

8
  • 3
    Take a look at the answer below, since January 2011 it is also possible on a per GET request basis. So it's possible to have as many "user filenames" as you want.
    – fabi
    Commented Sep 30, 2015 at 9:50
  • 3
    I needed to add quotes to the filename to get this to work, so my header was: Content-Disposition: attachment; filename="foo.bar" Commented Nov 11, 2015 at 21:34
  • 1
    In c#: request.ResponseHeaderOverrides.ContentDisposition = "attachment; filename=foo.bar";
    – Amir M
    Commented May 9, 2017 at 11:36
  • 2
    You could override response headers on the fly: https://...example.txt?response-content-disposition=attachment;filename=foo.bar
    – alaster
    Commented Aug 6, 2019 at 9:37
  • 3
    @alaster did this work for you? I'm getting The request signature we calculated does not match the signature you provided. Check your key and signing method. When I remove this response-content-disposition=attachment;filename=foo.bar it downloads the file but with original name.
    – lazycipher
    Commented Dec 24, 2020 at 12:08
106

While the accepted answer is correct I find it very abstract and hard to utilize.

Here is a piece of node.js code that solves the problem stated. I advise to execute it as the AWS Lambda to generate pre-signed Url.

var AWS = require('aws-sdk');
var s3 = new AWS.S3({
    signatureVersion: 'v4'
});
const s3Url = process.env.BUCKET;

module.exports.main = (event, context, callback) => {
var s3key = event.s3key
var originalFilename = event.originalFilename

var url = s3.getSignedUrl('getObject', {
        Bucket: s3Url,
        Key: s3key,
        Expires: 600,
        ResponseContentDisposition: 'attachment; filename ="' + originalFilename + '"'
    });

[... rest of Lambda stuff...]

}

Please, take note of ResponseContentDisposition attribute of params object passed into s3.getSignedUrl function.

More information under getObject function doc at http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property

4
  • Maybe it's not obvious, I will try to explain better. The idea is that I keep list of names of uploaded files in database, and then when someone wants to download it I execute lambda with a originalFilename as parameter.
    – Pawel
    Commented Apr 24, 2018 at 9:21
  • Header value cannot be represented using ISO-8859-1.
    – Alexey Sh.
    Commented Feb 22, 2021 at 1:00
  • the originalFilename should be encoded. I used encodeURIComponent to solve the error
    – Alexey Sh.
    Commented Feb 22, 2021 at 1:13
  • i found this for PHP, i get the pre signed url of the object so i can download it easily, i can also use Content-Disposition: attachment; filename="foo.bar"] to download it with a custom name. github.com/awsdocs/aws-doc-sdk-examples/blob/main/php/… Commented Jul 5, 2023 at 19:30
29

In early January 2011 S3 added request header overrides. This functionality allows you to 'dynamically' alter the Content-Disposition header for individual requests.

See the S3 documentation on getting objects for more details.

12

With C# using AWSSDK,

GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
    BucketName = BucketName,
    Key = Key,
    Expires = DateTime.Now.AddMinutes(25) 
};

request.ResponseHeaderOverrides.ContentDisposition = $"attachment; filename={FileName}";

var url = s3Client.GetPreSignedURL(request);
1
  • 1
    Thanks for this, I was able translate this to use in my VB.NET project as: request.ResponseHeaderOverrides.ContentDisposition = "attachment; filename=""my-new-filename-here.txt"""
    – Rich
    Commented Oct 28, 2021 at 12:33
5

For Java AWS SDK below Code Snippet should do the job:

GeneratePresignedUrlRequest generatePresignedUrlRequest = 
                new GeneratePresignedUrlRequest(s3Bucket, objectKey)
                .withMethod(HttpMethod.GET)
                .withExpiration(getExpiration());

ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
responseHeaders.setContentDisposition("attachment; filename =\"" + fileName + "\"");

generatePresignedUrlRequest.setResponseHeaders(responseHeaders);
5

Faced similar issue. Need to generate pre-signed url to download pdf from s3 but with different name other that what present in s3 bucket. This got resolved by using :-

Using Python boto3

file_download_url = client.generate_presigned_url(
ClientMethod = "get_object",
ExpiresIn = 3600,
Params = {
    "Bucket": "s3_bucket_name",
    "Key": "s3_key",
    "ResponseContentDisposition": "attachment; filename=file_name.pdf",
    "ResponseContentType" : "application/pdf"
}

)

Reference :- https://github.com/boto/boto3/issues/356

2

It looks like :response_content_disposition is undocumented in the presigned_url method. This is what worked for me

    signer = Aws::S3::Presigner.new
    signer.presigned_url(:get_object, bucket: @bucket, key: filename, 
    response_content_disposition: "attachment; filename =#{new_name}")
2

Using python and boto v2:

    conn = boto.connect_s3(
        AWS_ACCESS_KEY_ID,
        AWS_SECRET_ACCESS_KEY,
        host=settings.AWS_S3_HOST,
    )
    b = conn.get_bucket(BUCKET_NAME)
    key = b.get_key(path)
    url = key.generate_url(
        expires_in=60 * 60 * 10,  # expiry time is in seconds
        response_headers={
            "response-content-disposition": "attachment; filename=foo.bar"
        },
    )
1

I spent a few hours to find this solution.

const { CloudFront } = require("aws-sdk");
const url = require("url");

const generateSingedCloudfrontUrl = (path) => {
  const cloudfrontAccessKeyId = process.env.CF_ACCESS_KEY;
  const cloudFrontPrivateKey = process.env.CF_PRIVATE_KEY;
  const formattedKey = `${"-----BEGIN RSA PRIVATE KEY-----"}\n${cloudFrontPrivateKey}\n${"-----END RSA PRIVATE KEY-----"}`;
  const signer = new CloudFront.Signer(cloudfrontAccessKeyId, formattedKey);
  //  12 hours
  const EXPIRY_TIME = 43200000;

  const domain = process.env.CF_DOMAIN;
  const signedUrl = signer.getSignedUrl({
    url: url.format(`https://${domain}/${path}`),
    expires: Math.floor((Date.now() + EXPIRY_TIME) / 1000),
  });
  return signedUrl;
};

const fileName = "myFile.png";
  const result = generateSingedCloudfrontUrl(
    `originals/orgs/originals/MSP/1539087e-02b7-414f-abc8-3542ee0c8420/1644588362499/Screenshot from 2022-02-09 16-29-04..png?response-content-disposition=${encodeURIComponent(
      `attachment; filename=${fileName}`
    )
});
0

I have the same issue, I solved it by set http header "content-disposition" while submit the file to S3, the SDK version is AWS SDK for PHP 3.x. here is the doc http://docs.amazonaws.cn/en_us/aws-sdk-php/latest/api-s3-2006-03-01.html#putobject

a piece of my code

    public function __construct($config) 
    {
        $this->handle = new S3Client([
            'credentials' => array(
                'key' => $config['key'],
                'secret' => $config['secret'],
            ),
            ...
        ]);

        ...
    }

    public function putObject($bucket, $object_name, $source_file, $content_type = false, $acl = 'public-read', $filename = '')
    {
        try {
            $params = [
                'Bucket'      => $bucket,
                'Key'         => $object_name,
                'SourceFile'  => $source_file,
                'ACL'         => $acl,
            ];

            if ($content_type) $params['ContentType'] = $content_type;
            if ($filename) $params['ContentDisposition'] = 'attachment; filename="' . $filename . '"';

            $result = $this->handle->putObject($params);

            ...
        }
        catch(Exception $e)
        {
            ...
        }
    }
0

When work with AWS SDK for Java 2.x you can follow this guide for Work with Amazon S3 presigned URLs. Regardless of the request's model you build you can set Content-Disposition header by using this methods

Examples

PutObjectRequest

...
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
    .bucket(bucketName)
    .key(keyName)
    .contentType(contentType)
    .metadata(metadata)
    .contentDisposition("attachment;filename=" + displayName)
    .build();
        
PutObjectPresignRequest putObjectPresignRequest = PutObjectPresignRequest.builder()
        .signatureDuration(Duration.ofMinutes(10))
        .putObjectRequest(putObjectRequest)
        .build();

PresignedPutObjectRequest presignedPutObjectRequest = presigner.presignPutObject(putObjectPresignRequest);
...

GetObjectRequest

...
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
   .bucket(bucketName)
   .key(keyName)
   .responseContentDisposition("attachment;filename=" + displayName)
   .build();

GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder()
   .signatureDuration(Duration.ofMinutes(60))
   .getObjectRequest(getObjectRequest)
   .build();

PresignedGetObjectRequest presignedGetObjectRequest = presigner.presignGetObject(getObjectPresignRequest);
...

Not tested by I think this is the right way for Java client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.