Skip to content
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

Proposal: Instantiate a File using Public URL #1898

Closed
GabrielBB opened this issue Apr 29, 2022 · 6 comments · Fixed by #2432
Closed

Proposal: Instantiate a File using Public URL #1898

GabrielBB opened this issue Apr 29, 2022 · 6 comments · Fixed by #2432
Labels
api: storage Issues related to the googleapis/nodejs-storage API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@GabrielBB
Copy link

GabrielBB commented Apr 29, 2022

Is your feature request related to a problem? Please describe.

Right now File can only be instantiated using the file name. However, is common for systems to save the entire file url in databases, in these cases, to later delete the file from the bucket you would need to extract the file name (including parent directories) from the public url. Which means that you would have to spend some time thinking how to properly do this and write unit tests for it. I would prefer if the library handles this for me.

Describe the solution you'd like

1 - Switch from file(name: string, options?: FileOptions): File to just file(options: FileOptions): File

2 - Extend FileOptions like:

interface FileOptions {
    name?: string,
    publicUrl?: string,
    ...
}

3- Extract the name from the public url:

If name is not filled, we can proceed and check publicUrl -> From https://storage.googleapis.com/${firebase-project}.appspot.com/images%2Ftest.jpeg extract just images%2Ftest.jpeg, decode it as images/test.jpeg and use it as name. This would require doing a split: We already know the host is a static https://storage.googleapis.com and the bucket name (${firebase-project}.appspot.com) we already have it from here.

Example of usage of proposed API

file({ name: "images/test.jpeg" }) and file({ publicUrl: "https://storage.googleapis.com/${firebase-project}.appspot.com/images%2Ftest.jpeg" })

Describe alternatives you've considered

A backwards compatible way to achieve this would be to leave the API as it is but automatically recognise if name is a public url, in this case you would apply the same extraction logic mentioned before.

I can offer myself to open the PR if you guys agree with one solution

@GabrielBB GabrielBB added priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. labels Apr 29, 2022
@product-auto-label product-auto-label bot added the api: storage Issues related to the googleapis/nodejs-storage API. label Apr 29, 2022
@shaffeeullah
Copy link
Contributor

Hi @GabrielBB ,

We will look into community / customer desire for this feature.

@danielbankhead
Copy link
Contributor

Implementation idea if we decide to adopt this - this could be a static method on File, say File.from:

class File {
  static from (publicUrl: string | URL, options: FileOptions): File {
    // ...
  }
}

This way:

@GabrielBB
Copy link
Author

Implementation idea if we decide to adopt this - this could be a static method on File, say File.from:

class File {
  static from (publicUrl: string | URL, options: FileOptions): File {
    // ...
  }
}

This way:

Sounds good

@danielbankhead
Copy link
Contributor

Additional implementation note: should support gs: URIs as well to satisfy this feature request:

@IchordeDionysos
Copy link

@shaffeeullah
This is so much needed, we have to many times parts gs:// URLs and everywhere we do it a bit differently (maybe not 100% considering all edge-cases).

Having an easy way to create a File instance would be so helpful to just be able to do:

storage.file('gs://...')
or:
File.from('gs://...')

@IchordeDionysos
Copy link

The Python SDK already has a similar function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/nodejs-storage API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants