使用 firebase 云函数将文件上传到云存储

Upload file to cloud storage using firebase 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 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 参数的完整文件路径?据我理解,destination参数表示上传到存储桶后的文件名。

所以我的猜测是 "thumb_Qsdflsdfa.png" 就足够了,而不是 "/tmp/../thumb_Qsdflsdfa.png"

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

如果您不介意将图像名称与存储桶中的文件名相同,则可以将此留空。例如-

bucket.upload('/local/path/image.png') - 您的存储桶名称将为 image.png

但是,如果你想根据你的项目命名其他有意义的东西,你可以传入第二个参数,比如 -

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

希望这是有道理的。

这里截图说明一下-