Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 Change file download name

People also ask

Can you rename files in S3?

There is no direct method to rename the file in s3. what do you have to do is copy the existing file with new name (Just set the target key) and delete the old one.

Is S3 key the file name?

The object key (or key name) uniquely identifies the object in an Amazon S3 bucket. Object metadata is a set of name-value pairs. For more information about object metadata, see Working with object metadata. When you create an object, you specify the key name, which uniquely identifies the object in the bucket.

Can we rename folder in S3 bucket?

To rename a folder in an S3 bucket, you have to: Open the AWS S3 console and click on your bucket's name. In the Objects tab, optionally use the search input to find your folder. Click on the checkbox next to your folder's name. Click on the Actions button and select Move.

Can we update a file in S3?

Using Hudi, you can perform record-level inserts, updates, and deletes on S3 allowing you to comply with data privacy laws, consume real time streams and change data captures, reinstate late arriving data and track history and rollbacks in an open, vendor neutral format.


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 ;-)


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


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.


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);

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);

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}")