Last active
May 13, 2024 18:17
-
-
Save ovuruska/03560a4696a5818f81c83a5a2c891be1 to your computer and use it in GitHub Desktop.
Convert AWS S3 URI to public URL.
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 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