Javascript 如何在谷歌云功能中获得使用node js调整大小的图像的下载url?
我不熟悉NodeJS和google云功能。我能够调整图像大小以生成缩略图。我不知道如何获得新生成的缩略图的下载url。这是密码Javascript 如何在谷歌云功能中获得使用node js调整大小的图像的下载url?,javascript,node.js,firebase,google-cloud-functions,firebase-storage,Javascript,Node.js,Firebase,Google Cloud Functions,Firebase Storage,我不熟悉NodeJS和google云功能。我能够调整图像大小以生成缩略图。我不知道如何获得新生成的缩略图的下载url。这是密码 exports.generateThumbnail = functions.storage.object('{pushId}/ProductImages').onFinalize((object) => { // [END generateThumbnailTrigger] // [START eventAttributes] const fileB
exports.generateThumbnail = functions.storage.object('{pushId}/ProductImages').onFinalize((object) => {
// [END generateThumbnailTrigger]
// [START eventAttributes]
const fileBucket = object.bucket; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const contentType = object.contentType; // File content type.
const resourceState = object.resourceState; // The resourceState is 'exists' or 'not_exists' (for file/folder deletions).
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
console.log("this is the object ",object);
// [END eventAttributes]
// [START stopConditions]
if (!contentType.startsWith('image/')) {
console.log('This is not an image.');
return null;
}
const fileName = path.basename(filePath);
if (fileName.startsWith('thumb_')) {
return null;
}
// [END stopConditions]
// [START thumbnailGeneration]
// Download file from bucket.
const bucket = gcs.bucket(fileBucket);
const tempFilePath = path.join(os.tmpdir(), fileName);
// const tempFilePath1 = path.join(os.tmpdir(), fileName+"1");
// var storageRef = firebase.storage.ref("folderName/file.jpg");
const storageRef = event.data.ref.parent;
// return storageRef.getDownloadURL().then(function(url) {
// });
const metadata = {
contentType: contentType,
};
return bucket.file(filePath).download({
destination: tempFilePath,
}).then(() => {
console.log('Image downloaded locally to', tempFilePath);
// Generate a thumbnail using ImageMagick.
return spawn('convert', [tempFilePath, '-thumbnail', '200x200>', tempFilePath] );
}).then(() => {
const thumbFileName = `thumb_${fileName}`;
const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
// Uploading the thumbnail.
return bucket.upload(tempFilePath, {
destination: thumbFilePath,
metadata: metadata,
})
// Once the thumbnail has been uploaded delete the local file to free up disk space.
}).then(() => fs.unlinkSync(tempFilePath));
});
在javascript中,可以使用以下代码获得上传到firebase存储的图像的下载URL
storageRef.put(file, metadata).then(function(snapshot) {
var url = snapshot.downloadURL;
})
如何获取节点js中已调整大小的图像的下载url?您需要使用它来生成公共url。中的函数示例repo中有一个示例。缩写:
const thumbFile = bucket.file(thumbFilePath);
const config = {
action: 'read',
expires: '03-01-2500',
};
thumbFile.getSignedUrl(config); // returns a promise with results
要使用此方法,您必须注意自述文件并使用服务帐户初始化AdminSDK。云函数的默认初始化将不起作用。您需要使用来生成公共URL。中的函数示例repo中有一个示例。缩写:
const thumbFile = bucket.file(thumbFilePath);
const config = {
action: 'read',
expires: '03-01-2500',
};
thumbFile.getSignedUrl(config); // returns a promise with results
要使用此方法,您必须注意自述文件并使用服务帐户初始化AdminSDK。云函数的默认初始化将不起作用