nodejs-storage icon indicating copy to clipboard operation
nodejs-storage copied to clipboard

Proposal: Instantiate a File using Public URL

Open GabrielBB opened this issue 2 years ago • 6 comments

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 avatar Apr 29 '22 09:04 GabrielBB

Hi @GabrielBB ,

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

shaffeeullah avatar May 02 '22 15:05 shaffeeullah

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:

  • the parent Bucket object can be determined and passed to File#constructor
  • the existing File#constructor remains the same
  • matches other JS static from conventions (e.g. https://nodejs.org/api/buffer.html#static-method-bufferfromarray)

danielbankhead avatar Jun 28 '22 04:06 danielbankhead

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:

  • the parent Bucket object can be determined and passed to File#constructor
  • the existing File#constructor remains the same
  • matches other JS static from conventions (e.g. https://nodejs.org/api/buffer.html#static-method-bufferfromarray)

Sounds good

GabrielBB avatar Jun 29 '22 09:06 GabrielBB

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

  • https://github.com/googleapis/nodejs-storage/issues/2057

danielbankhead avatar Sep 06 '22 20:09 danielbankhead

@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 avatar Feb 02 '24 08:02 IchordeDionysos

The Python SDK already has a similar function

IchordeDionysos avatar Feb 02 '24 08:02 IchordeDionysos