Skip to content

Instantly share code, notes, and snippets.

@ovuruska
Last active May 13, 2024 18:17
Show Gist options
  • Save ovuruska/03560a4696a5818f81c83a5a2c891be1 to your computer and use it in GitHub Desktop.
Save ovuruska/03560a4696a5818f81c83a5a2c891be1 to your computer and use it in GitHub Desktop.
Convert AWS S3 URI to public URL.
const getUrl = (uri: string): string => {
/**
* Extracts the bucket and object key from the URI and returns the URL to the object.
* @param uri The URI of the object.
* @returns The URL to the object.
* @example
* getUrl('s3://bucket-name/object-key') // 'https://bucket-name.s3.amazonaws.com/object-key'
* getUrl('s3://bucket-name/folder/object-key') // 'https://bucket-name.s3.amazonaws.com/folder/object-key'
*/
const [bucket, ...objectKeyParts] = uri.slice(5).split('/');
const objectKey = objectKeyParts.join('/');
return `https://${bucket}.s3.amazonaws.com/${objectKey}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment