-
Notifications
You must be signed in to change notification settings - Fork 370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add ability to create a File object from URL #2432
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! One optional suggestion
static from( | ||
publicUrlOrGsUrl: string, | ||
storageInstance: Storage, | ||
options?: FileOptions | ||
): File { | ||
const gsMatches = [...publicUrlOrGsUrl.matchAll(GS_UTIL_URL_REGEX)]; | ||
const httpsMatches = [...publicUrlOrGsUrl.matchAll(HTTPS_PUBLIC_URL_REGEX)]; | ||
|
||
if (gsMatches.length > 0) { | ||
const bucket = new Bucket(storageInstance, gsMatches[0][1]); | ||
return new File(bucket, gsMatches[0][2], options); | ||
} else if (httpsMatches.length > 0) { | ||
const bucket = new Bucket(storageInstance, httpsMatches[0][2]); | ||
return new File(bucket, httpsMatches[0][3], options); | ||
} else { | ||
throw new Error( | ||
'URL string must be of format gs://bucket/file or https://storage.googleapis.com/bucket/file' | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we could parse the URL, then use protocol
to determine the logic. Here's an example with an optional storage
param:
static from( | |
publicUrlOrGsUrl: string, | |
storageInstance: Storage, | |
options?: FileOptions | |
): File { | |
const gsMatches = [...publicUrlOrGsUrl.matchAll(GS_UTIL_URL_REGEX)]; | |
const httpsMatches = [...publicUrlOrGsUrl.matchAll(HTTPS_PUBLIC_URL_REGEX)]; | |
if (gsMatches.length > 0) { | |
const bucket = new Bucket(storageInstance, gsMatches[0][1]); | |
return new File(bucket, gsMatches[0][2], options); | |
} else if (httpsMatches.length > 0) { | |
const bucket = new Bucket(storageInstance, httpsMatches[0][2]); | |
return new File(bucket, httpsMatches[0][3], options); | |
} else { | |
throw new Error( | |
'URL string must be of format gs://bucket/file or https://storage.googleapis.com/bucket/file' | |
); | |
} | |
} | |
static from( | |
publicUrlOrGsUrl: string | URL, | |
storage?: Storage, | |
options?: FileOptions | |
): File { | |
const url = new URL(publicUrlOrGsUrl); | |
if (url.protocol === 'gs:') { | |
const storageInstance = storage ?? new Storage(); | |
const bucket = new Bucket(storageInstance, url.host); | |
return new File(bucket, url.pathname.slice(1), options); | |
} else if (url.protocol === 'http:' || url.protocol === 'https:') { | |
const {groups: {bucketName, fileName}} = /\/(?<bucket>[^/]*)\/(?<filename>.*)/.exec(u.pathname); | |
const storageInstance = storage ?? new Storage({apiEndpoint: url.hostname}); | |
const bucket = new Bucket(storageInstance, bucketName); | |
return new File(bucket, fileName, options); | |
} else { | |
throw new Error( | |
'URL string must be of format gs://bucket/file or https://storage.googleapis.com/bucket/file' | |
); | |
} | |
} |
If we do this we'll need to make a small change to storage.ts
on line ~180 to prevent accidental custom apiEndpoint
when provided.
From:
if (options.apiEndpoint) {
To:
if (options.apiEndpoint && options.apiEndpoint !== apiEndpoint) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, I'm apt to leave this using the regexs to make it a bit more concise and avoid the apiEndpoint logic. It also matches how it was done in the python client (not that consistency is SUPER important).
let's gooooo 🚀 so excited to see this feature get implemented! |
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #1898 🦕