使用firebase云功能将文件上载到云存储

使用firebase云功能将文件上载到云存储,firebase,google-cloud-storage,google-cloud-functions,Firebase,Google Cloud Storage,Google Cloud Functions,我正在检查此示例代码: // Download file from bucket. const bucket = gcs.bucket(fileBucket); const tempFilePath = path.join(os.tmpdir(), fileName); return bucket.file(filePath).download({ destination: tempFilePath }).then(() => { console.log('Image downlo

我正在检查此示例代码:

// Download file from bucket.
const bucket = gcs.bucket(fileBucket);
const tempFilePath = path.join(os.tmpdir(), fileName);
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(() => {
  console.log('Thumbnail created at', tempFilePath);
  // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
  const thumbFileName = `thumb_${fileName}`;
  const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
  // Uploading the thumbnail.
  return bucket.upload(tempFilePath, {destination: thumbFilePath});
// Once the thumbnail has been uploaded delete the local file to free up disk space.
}).then(() => fs.unlinkSync(tempFilePath));
我的问题具体是:

return bucket.upload(tempFilePath, {destination: thumbFilePath});
为什么我们要在这里提供指向
目标
参数的完整文件路径?据我所知,
destination
参数表示上传到存储桶后将使用的文件名

因此,我的猜测是类似于
“thumb_Qsdflsdfa.png”
就足够了,而不是
“/tmp/。/thumb_Qsdflsdfa.png”

根据文档,第二个参数是可选的

如果您不介意在bucket中使用与文件名相同的图像名,那么可以将其留空。e、 g.-

bucket.upload('/local/path/image.png')
-您的bucket名称将是
image.png

然而,如果您想根据您的项目命名其他有意义的东西,您可以传入第二个参数,如下所示-

bucket.upload('/local/path/image.png',{destination:'thumb_image.png'})
-您的bucket名称现在将是
thumb_image.png

希望这是有意义的

这里有一个屏幕截图来说明问题-


权限问题?这个问题没有说任何关于许可的问题哦,对不起,我有两个类似的问题,我以为你回答了另一个;好的,谢谢你的解释,我认为你的答案是有道理的,但是在代码中,他们传递的是完整的文件路径作为目标参数,而不仅仅是文件名。你知道吗?哈哈,不用担心。所以我做了一个快速测试,它看起来像
path.join(path.dirname(filePath),thumbFileName)
只提供文件名
thumb\u image.png
而不是
myFolder/thumb\u image.png
,并且它确实正确地将其存储在
myFolder/
中。因此,为了回答您的问题,在本例中,您需要它定义您希望它存储在哪里,但它将采用的名称将是文件名而不是整个路径名。因此整个
path.join(path.dirname(filePath),thumbFileName)
与thumbFileName相同?抱歉,我可能误解了,该值为
myFolder/thumb\u image.png
,它将其保存在
myFolder
中,并带有
thumb\u image.png
名称。为了清晰起见,我放了一张照片,看一看。