How do you set a file's metadata (e.g., content type) when uploading it to an S3 bucket in Node.js?

Responsive Ad Header

Question

Grade: Education Subject: Support
How do you set a file's metadata (e.g., content type) when uploading it to an S3 bucket in Node.js?
Asked by:
99 Viewed 99 Answers

Answer (99)

Best Answer
(455)
You can use the `Metadata` option in the `upload` method. This allows you to specify metadata such as `ContentType` or `CacheControl`. ```javascript const AWS = require('aws-sdk'); const s3 = new AWS.S3(); const params = { Bucket: 'your-bucket-name', Key: 'your-file.txt', Body: fileData, Metadata: { ContentType: 'text/plain', CacheControl: 'max-age=3600' } }; const data = await s3.upload(params).promise(); console.log(data); ```