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.
