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

时间:2017-07-17 06:47:41

标签: 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 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"

1 个答案:

答案 0 :(得分:1)

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

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

bucket.upload('/local/path/image.png') - 您的广告位名称为image.png

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

bucket.upload('/local/path/image.png', { destination: 'thumb_image.png' }) - 您的广告管理名称现在为thumb_image.png

希望这是有道理的。

这是一个明确的截图 -

enter image description here