0

I want to display an image saved in google storage with a URL or PATH saved in my database.

I create a file in my storage bucket with a cloud function

bucket.upload(thumbPath, {
      destination: join(bucketDir, thumbName),
    });

then i create a singnedUrl

const config = {
    action: 'read',
    expires: '03-01-2500',
  };
  const result = await thumbFile.getSignedUrl(config);

and save it in my database

I want to be able to always load the image with the saved URL. This works for about a week. But then I get 403 errors.

My storage Rules are:

service firebase.storage {
  match /b/{bucket}/o {
   match /{allPaths=**} {
     allow read: if true;
     allow write: if request.auth != null;
   }
  }
}

Is there another way without using signed URLs because I think this is the source of the error

Alan
  • 386
  • 1
  • 3
  • 17
Ivan Tarskich
  • 1,332
  • 1
  • 12
  • 19

1 Answers1

3

Just a note, if your signed URLs are expiring after rhoughly 7 days, it may be because you are using the V4 signing API (or the method getSignedUrl() is calling this API). From the documentation:

X-Goog-Expires: The length of time the signed URL remained valid, measured in seconds from the value in X-Goog-Date. In this example the Signed URL expires in 15 minutes. The longest expiration value is 604800 seconds (7days).

On the other hand, V2 signing (you can check this documentation) has no restrictions on the expiration date of the Signed URLS that generates.

I'm not sure which is the library that you are using to invoke the getSignedUrl() function, but I suspect that it was updated and changed the signed method from V2 to V4, and now there is the limit of 7 days for the generated Signed URLs. If you want to keep using them, maybe rolling back to a previous version of the library could help, or you could directly call the V2 API to generate the urls.

Anyhow, if you would like an alternative method, probably the most straight forward one is to just make the files in your bucket public, see documentation here. You could even make the whole bucket public so all of its contents are freely readable by users.

Joan Grau Noël
  • 3,084
  • 12
  • 21
  • i use this library [link](https://googleapis.dev/nodejs/storage/latest) which doesn't seem to have the getDownloadURL method, also in the docs it says it uses v2 if not specified [link](https://googleapis.dev/nodejs/storage/latest/File.html#getSignedUrl) – Ivan Tarskich Oct 02 '19 at 20:02
  • Right now the only solution for me would be to save the path in the database and query the url in the frontend via this [link](https://firebase.google.com/docs/storage/web/download-files) method – Ivan Tarskich Oct 02 '19 at 20:08
  • If the signed URLs are expiring after 7 days, and are using the V2 API, then this is an issue. I would recommend you to contact [Firebase support](https://firebase.google.com/support) for it, or report an issue on it [GitHub repo](https://github.com/firebase/firebase-admin-node/issues), since the issue seems to be located on their libraries. – Joan Grau Noël Oct 03 '19 at 07:58
  • Regarding the download URL, instead you can use [this workaround](https://stackoverflow.com/a/58016766/10561875) using the Cloud SDK libraries, that allow you to directly get the file with the `https://storage.googleapis.com//` url pattern. – Joan Grau Noël Oct 03 '19 at 07:58