Created
July 21, 2018 21:32
-
-
Save troyharvey/2cf3767ad2c71685788b147c5a10343b to your computer and use it in GitHub Desktop.
Extract bucket and path from Google Cloud Storage URL
This file contains hidden or 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
/** | |
* Convert Google Cloud Storage object url into an object with bucket and file path. | |
*/ | |
function fromStorageUrl(url) { | |
var tokens = RegExp('https://storage.googleapis.com/(.+?)/(.+)','g').exec(url); | |
if (tokens === null) throw "Invalid Google Cloud Storage url."; | |
return { | |
'bucket': tokens[1], | |
'file': tokens[2] | |
}; | |
} | |
console.log(fromStorageUrl('https://storage.googleapis.com/uploads.birdperson.com/some-path/to-a-file.mp3')); | |
// > Object { bucket: "uploads.birdperson.com", file: "some-path/to-a-file.mp3" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot.