Skip to content

Instantly share code, notes, and snippets.

@bericp1

bericp1/s3.js Secret

Last active June 5, 2024 17:21
Show Gist options
  • Select an option

  • Save bericp1/ff4596303a577ed19f5d330230a9e4fa to your computer and use it in GitHub Desktop.

Select an option

Save bericp1/ff4596303a577ed19f5d330230a9e4fa to your computer and use it in GitHub Desktop.
const s3Client = ({ bucket }) => {
// If the bucket is a "directory bucket" then we need to
// use a custom endpoint via the availability zone.
// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/directory-bucket-naming-rules.html
if (bucket.endsWith('--x-s3')) {
const bucketAndAzid = bucket.slice(0, -6);
const parts = bucketAndAzid.split('--');
const azid = parts[parts.length - 1];
// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-express-Regions-and-Zones.html
const customEndpoint = `https://s3express-${azid}.${region}.amazonaws.com`;
return new AWS.S3({
endpoint: customEndpoint,
});
}
return s3;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment