0

I am trying to upload images to firebase storage and to save the download URL in my database. However, I cannot find a way to get the download Url in springboot unlike when an upload is performed in android. Saving the media URL does not work as it doesn't have the access token. Below is the code I've used to upload the image to firebase.

File file = convertMultiPartToFile(multipartFile);
        Path filePath = file.toPath();
        String objectName = generateFileName(multipartFile);

        BlobId blobId = BlobId.of(bucketName, objectName);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
        Blob blob = storage.create(blobInfo, Files.readAllBytes(filePath));
        log.info("File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
        return blob.getMediaLink();

  • Does this answer your question? [How to get the download url from Firebase Storage?](https://stackoverflow.com/questions/53299915/how-to-get-the-download-url-from-firebase-storage) – Ruli Jan 14 '21 at 07:16
  • Hi Ruli, This answer is on how it is done in android if I've not mistaken. I'm trying to upload images using my server which is implemented using springboot – Avishka Jayasundara Jan 14 '21 at 09:09

3 Answers3

1

I had the same problem this week when I was developing an endpoint responsible for uploading a file to Firebase and retrieving the downloadable URL in order to save it in a database. It seems like Firebase Admin Java JDK does not give any feature to either directly retrieve a downloadable URL for the file just uploaded or retrieving an access token to make possible to build it. Although there is a property configured as a metadata named 'firebaseStorageDownloadTokens' that can be find after the file get uploaded to the Firebase and stores the necessary token to authorize the download of the file, it still not working because this property is not retrieved for all requests and to be honest I cannot find why it happens. Anyway, the solution that worked for me was to add 'firebaseStorageDownloadTokens' directing as a metadata for the BlobInfo before uploading the file. It will make the Firebase assuming the passed value as the access token for the file being uploaded and once handing this information it's possible to build the downloadable URL.

In your scenario it would be like:

File file = convertMultiPartToFile(multipartFile);
    Path filePath = file.toPath();
    String objectName = generateFileName(multipartFile);

    BlobId blobId = BlobId.of(bucketName, objectName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setMetadata(Map.of("firebaseStorageDownloadTokens","randomAccessToken")).build();
    Blob blob = storage.create(blobInfo, Files.readAllBytes(filePath));
    log.info("File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
    return blob.getMediaLink().concat("&token=randomAccessToken");
0

Firebase by default does not return any download URLs on Service Accounts until in Client Side SDKs, therefore, what you can do is, construct your own download URL by following the Firebase Convention.

`https://firebasestorage.googleapis.com/v0/b/<bucket name>/o/%s?alt=media`

`<bucket name>` = Firebase Storage Bucket Name
`%s` = The media name (file name)
0

You can get the download link after uploading a file to firebase Storage by using: storage.get(BlobId.of(bucketName,fileName)).getSelfLink();