Skip to content

Instantly share code, notes, and snippets.

@troyharvey
Created July 21, 2018 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save troyharvey/2cf3767ad2c71685788b147c5a10343b to your computer and use it in GitHub Desktop.
Save troyharvey/2cf3767ad2c71685788b147c5a10343b to your computer and use it in GitHub Desktop.
Extract bucket and path from Google Cloud Storage URL
/**
* 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