Question
How do you set a file's metadata (e.g., content type) when uploading it to an S3 bucket in Node.js?
Asked by: USER9873
99 Viewed
99 Answers
Answer (99)
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);
```