Node.js 当文件在bucket中被覆盖时,下载URL更改。如何保持原始URL
我已经成功地为云存储创建了一个触发器,它可以调整和覆盖原始图像文件的大小Node.js 当文件在bucket中被覆盖时,下载URL更改。如何保持原始URL,node.js,google-cloud-functions,firebase-storage,Node.js,Google Cloud Functions,Firebase Storage,我已经成功地为云存储创建了一个触发器,它可以调整和覆盖原始图像文件的大小 return destBucket.file(filePath).download({ destination: tempFilePath }).then(() => { return spawn('convert', [tempFilePath, '-resize', '150x150', tempFilePath]) }).then(() => { metadata.isResize
return destBucket.file(filePath).download({
destination: tempFilePath
}).then(() => {
return spawn('convert', [tempFilePath, '-resize', '150x150', tempFilePath])
}).then(() => {
metadata.isResized = true
return destBucket.upload(tempFilePath,
{
destination: path.join(path.dirname(filePath), path.basename(filePath)),
metadata: { metadata: metadata }
})
}).then(() => fs.unlinkSync(tempFilePath))
});
不过,这也会更改我存储在firestore数据库中的文件的下载URL
const pathRef = storageRef.ref('profileImg/' + uid + '/' + image.name)
pathRef.put(image).then(() => {
//Get logo URL
const starsRef = storageRef.ref().child('profileImg/' + uid + '/' + image.name)
starsRef.getDownloadURL().then((url) => {
changeProfile({ profile: profile, profileImg: url }).then((result) => {
dispatch(progressSuccess(result))
}).catch((error) => {
dispatch(progressFailed(error.message))
});
}).catch((error) => {
dispatch(progressFailed(error.message))
})
有没有办法将原始下载URL保存在被覆盖的文件中?或者有什么方法可以完成这项任务吗?好的,我不得不采取另一种方法来完成这项任务,因为我认为没有办法保留原始URL。基本上,现在我在一个存储触发函数中完成所有操作(调整图像大小、删除原始图像、获取url、更新用户数据库)
return destBucket.file(filePath).download({
destination: tempFilePath
})
.then(() => {
console.log('The file has been downloaded to', tempFilePath)
return spawn('convert', [tempFilePath, '-resize', '140x140', tempLocalJPEGFile])
}).then(() => {
console.log('JPEG image created at', tempLocalJPEGFile)
metadata.modified = true
return destBucket.upload(tempLocalJPEGFile,
{
destination: JPEGFilePath,
metadata: { metadata: metadata }
})
}).then(() => {
console.log('JPEG image uploaded to Storage at', JPEGFilePath)
return destBucket.file(filePath).delete()
}).then(() => {
console.log('Original file deleted', filePath)
const logo = storageRef.file(JPEGFilePath)
return logo.getSignedUrl({ action: 'read', expires: '03-09-2491' })
}).then((url) => {
const newRef = db.collection("user").doc(uid)
return newRef.set({
profile: {profileImg: url[0]}
}, {
merge: true
})
}).then(()=> {
fs.unlinkSync(tempFilePath);
fs.unlinkSync(tempLocalJPEGFile)
console.log('User database updated')
return null
})
})
我不确定这是否是最佳实践,因此请随时评论或更新。我也不知道是否有更好的方法可以从存储图像的bucket中获取文件夹名,它被命名为用户id值(userImg/[userIdHere]/image.jpg),我需要它来更新数据库,所以我使用了老式的拆分代码
const uid = filePath.split("/").slice(1,2).join("")
但是,此触发器将使bucket仅可用于用户图像,因为我们无法对bucket中的特定文件夹使用触发器新路径是否只是添加了
-resize
的旧路径?嗨,Doug。不,这是一个完全不同的pathi codecubed.io实际上并不是所有的东西。仅在媒体和令牌后面的链接末尾媒体和令牌=6ddcd967-4213-47f8-badd-74dbd203e783媒体和令牌=8062018c-738b-4271-9c15-a1a2ce40505c