0

I am making a web service with sveltekit and firebase. By the way, when users save images on firebase storage and other users try to use these images, I want to create a signed url and show it on the page to prevent Hotlink.

I searched and found that there is a function called getSignedUrl that generates a signed url, but there is no official document page that describes it in the firebase document.

Where can I get some example functions or information related to this?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Viewee
  • 13
  • 2

2 Answers2

0

The Firebase SDK for Cloud Storage uses a different type of URL, called a download URL. You can generate a download URL by calling getDownloadURL with/on a reference to the file, as shown in the documentation on downloading data through a URL.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you for leaving a comment. I know `getDownloadURL`. However, the URL I received using `getDownloadURL` has not expired. I want the URL to expire after a certain period of time. – Viewee Jun 03 '23 at 19:28
  • The Firebase SDKs currently have no way to generate expiring signed URLs. If you need such a URL, you will have to use one of the Cloud SDK in a trusted environment (such as you development machine, a server that you control, or Cloud Functions/Cloud Run). If you need the value to be available in your client-side application, you can make an API endpoint in a trusted environment, use the server/admin SDKs there to generate the signed URL, and have the client-side application call it. For example, for Cloud Functions that could be https://firebase.google.com/docs/functions/callable – Frank van Puffelen Jun 03 '23 at 19:56
  • Thank you for your answer. I understand what you mean. However, after leaving a question, I succeeded in generating an url with an expiration date from storage, using a function called 'getSignedUrl' in the firebase SDK. I don't understand why this method isn't explained in any official document. – Viewee Jun 03 '23 at 22:28
  • I wrote down the code in the answer area below. Please Check my code @_@ – Viewee Jun 03 '23 at 22:41
  • The Firebase Admin SDK for Cloud Storage is a thin wrapper around the [Google Cloud SDK for Node.js](https://www.google.com/search?q=Google+Cloud+Storage+SDK+for+Node.js). The method is documented there as far as I know, although I haven't look in a while: https://cloud.google.com/storage/docs/access-control/signing-urls-with-helpers#client-libraries. If you can't find it there either, it might be good to leave feedback to that effect at the bottom of the page. – Frank van Puffelen Jun 03 '23 at 23:19
  • Thank you very much for answering my question. Through the contents you told me, my curiosity was somewhat resolved to some extent. – Viewee Jun 03 '23 at 23:41
  • Good to hear @Viewee --- Also see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Frank van Puffelen Jun 04 '23 at 00:29
0

The code below is the code I wrote on firebase functions using Firebase SDK. This code shows you using a function called getSignedUrl to write an url with an expiration date. I don't know why the official document doesn't have this information.

const { onObjectFinalized } = require('firebase-functions/v2/storage');
const { getStorage } = require('firebase-admin/storage');
const path = require('path');

const admin = require('firebase-admin');
const serviceAccount = require('./name-firebase-adminsdk.json');
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: '~~~~',
});
exports.generateThumbnail = onObjectFinalized(
    { cpu: 2, region: 'asia-northeast3' },
    async (event) => {
        const fileName = path.basename(filePath);
        const filePath = event.data.name; // File path in the bucket.

        const fileBucket = event.data.bucket; // Storage bucket containing the file.
        const bucket = getStorage().bucket(fileBucket);

        // Prefix 'thumb_' to file name.
        const thumbFileName = `thumb_${fileName}`;
        const thumbFilePath = path.join(path.dirname(filePath) + '/thumbnail', thumbFileName);

        const thumbnailFile = bucket.file(thumbFilePath);
        const expirationDate = new Date();
        expirationDate.setMinutes(expirationDate.getMinutes() + 1);

        const thumbnailURLArr = await thumbnailFile.getSignedUrl({
            action: 'read',
            expires: expirationDate,
        });
        const thumbnailURL = thumbnailURLArr[0];
    }
);

And, this is the page(captured img) showing Expired Token about url.

enter image description here

Viewee
  • 13
  • 2