【发布时间】:2018-09-21 11:47:13
【问题描述】:
我是节点 js 和谷歌云功能的新手。我能够调整图像大小以生成缩略图。我想不通的是如何获取新生成的缩略图的下载网址。这是代码
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存储的图像的下载网址
storageRef.put(file, metadata).then(function(snapshot) {
var url = snapshot.downloadURL;
})
如何在node js中获取调整后图片的下载地址?
【问题讨论】:
标签: javascript node.js firebase google-cloud-functions firebase-storage