Skip to content

Instantly share code, notes, and snippets.

@jupapios
Created December 12, 2023 12:52
Show Gist options
  • Save jupapios/92d1ee4da2c73c3420adaea16f4d2c61 to your computer and use it in GitHub Desktop.
Save jupapios/92d1ee4da2c73c3420adaea16f4d2c61 to your computer and use it in GitHub Desktop.
Migrate s3 objects to support being embedded.
import {
CopyObjectCommand,
HeadObjectCommand,
ListObjectsV2Command,
S3Client,
} from "@aws-sdk/client-s3";
const client = new S3Client({
credentials: {
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
accessKeyId: process.env.S3_ACCESS_KEY_ID,
},
});
const Bucket = "official.whatsapp.api.files";
const companyIdToMigrate = "9331";
const { Contents } = await client.send(
new ListObjectsV2Command({
Bucket,
Prefix: companyIdToMigrate,
}),
);
Contents.forEach(async ({ Key }) => {
const ObjectMetadata = await client.send(
new HeadObjectCommand({ Bucket, Key }),
);
if (
ObjectMetadata.CacheControl === "no-cache" ||
ObjectMetadata.ContentDisposition?.startsWith("attachment")
) {
const { $metadata: _, ...MetadataToCopy } = ObjectMetadata;
console.log({ Key, MetadataToCopy });
client.send(
new CopyObjectCommand({
Bucket,
Key,
CopySource: `${Bucket}/${Key}`,
ACL: "public-read",
MetadataDirective: "REPLACE",
...MetadataToCopy,
CacheControl: null,
ContentDisposition: null,
}),
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment