Created
February 10, 2023 21:47
-
-
Save Tuarisa/6a6de55a08e8acab08dc61f7fdec5696 to your computer and use it in GitHub Desktop.
Update content type on all s3 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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