firebase-android-sdk icon indicating copy to clipboard operation
firebase-android-sdk copied to clipboard

Firebase Storage. Generate on the client side a Download URL before uploading an item.

Open FranRiadigos opened this issue 3 years ago • 3 comments

What feature would you like to see?

Ability to generate, on the client side, a Download URL before running the upload process. So that we can avoid the costs of unnecessary uploads if any error or exception happens.

How would you use it?

Some of my documents in Firestore depend on a link of an uploaded image from Storage. To add the image URL into a document field I first need to upload the file.

However, if for any reason, an error, exception, security rule rejection, etc. occurs, I have to delete the uploaded file since it's no longer relevant, as the user might decide to use another image.

This incurs in extra costs I think can be easily avoided by having the final generated URL with its token. Similar to how Firebase provides a reference id locally on the client side.

If there is already a better approach to tackle this use case, please let me know. Thanks.

FranRiadigos avatar Mar 08 '22 18:03 FranRiadigos

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Mar 08 '22 18:03 google-oss-bot

@FranRiadigos Would Storage references work for you? You can use anything as a storage ref to your image, even the firestore doc id could be the reference. So for example:

var documentId = // ... use your own logic to generate an id or let firestore auto-generate it
var fileExtension  = ".jpg" // maybe you can get this extension when the user picks an image?
val imageStorageRef = Firebase.storage.reference.child(documentId + fileExtension)

// upload your image using:
imageStorageRef.putFile( /* your file */ )

Whenever the user decides to use another image, you can use the call putFile() again to overwrite the file and the storage reference will remain the same.

Only downside is that in order to download the image, you need to get the download link first or alternatively use FirebaseUI. But at least this doesn't incur costs AFAIK.

(I wrote a broad example because I haven't seen your code yet)

thatfiredev avatar Apr 12 '22 11:04 thatfiredev

@thatfiredev Thanks for replying.

The option you propose would work in some scenarios, but not very well when it comes to changing/deleting images. Changing an image by using the same reference id would not work for cached images. When it comes to deletion, I still have to re-create a new reference for a new image.

Besides I store full URL into firestore, since users might not upload an image but use an external link instead.

In addition, the Storage url follows a pattern like https://firebasestorage.googleapis.com/v0/b/example.com/o/folderName%fileName.jpeg?alt=media&token=tokenNumber

tokenNumber is unknown until uploaded. firebasestorage.googleapis.com/v0/b might eventually change and I don't know where to get this path.

But thanks for giving some support.

FranRiadigos avatar Apr 30 '22 14:04 FranRiadigos