Skip to content

Instantly share code, notes, and snippets.

@Tuarisa
Created February 10, 2023 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tuarisa/6a6de55a08e8acab08dc61f7fdec5696 to your computer and use it in GitHub Desktop.
Save Tuarisa/6a6de55a08e8acab08dc61f7fdec5696 to your computer and use it in GitHub Desktop.
Update content type on all s3 files
const AWS = require('aws-sdk')
const s3 = new AWS.S3({
endpoint: process.env.S3_SPACES_ENDPOINT,
accessKeyId: process.env.S3_SPACES_ACCESS_KEY,
secretAccessKey: process.env.S3_SPACES_SECRET_KEY
})
async function changeContentType() {
// Name of the bucket
const bucketName = 'simplesocial'
// List all objects in the bucket
const objects = await s3.listObjects({ Bucket: bucketName }).promise()
// Loop through all objects in the bucket
for (const object of objects.Contents) {
// Get the current object
const currentObject = await s3.getObject({ Bucket: bucketName, Key: object.Key }).promise()
// Set the desired content type
const contentType = 'image/jpeg'
// Update the object's content type
await s3.copyObject({
Bucket: bucketName,
CopySource: `${bucketName}/${object.Key}`,
Key: object.Key,
ContentType: contentType,
Metadata: currentObject.Metadata,
MetadataDirective: 'REPLACE'
}).promise()
}
}
changeContentType().catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment