Overriding file name of an S3 object using pre-signed URL and AWS SDK for JavaScript
Thalles Lossurdo
1 min read
When you use pre-signed URL for sharing objects with AWS S3, you can change the default file name (the name of object in bucket) using the header: Content-Disposition.
The Content-Disposition header in HTTP dictates whether the content is displayed in the browser or, instead, made available as an attachment to be downloaded to local storage.
You can change the Content-Disposition using the AWS SDK for JavaScript, which can be the version 2 or the new major version 3. Follow the code snippet with version 2 of AWS SDK to do this:
import { S3 } from 'aws-sdk'
const s3Client = new S3({
apiVersion: '2006-03-01'
})
const myNewFileName = 'example.json'
s3Client.getSignedUrlPromise('getObject', {
Bucket: 'BUCKET_NAME',
Key: 'OBJECT_KEY',
Expires: 300,
ResponseContentDisposition: `attachment; filename=${myNewFileName}`
})
.then((preSignedUrl) => {
console.info(`S3 Pre-signed URL: ${preSignedUrl}`)
})
References
https://stackoverflow.com/questions/2611432/amazon-s3-change-file-download-name
0
Subscribe to my newsletter
Read articles from Thalles Lossurdo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by