【问题标题】:Copy file from bucket1 to bucket2 in Google Cloud Storage在 Google Cloud Storage 中将文件从 bucket1 复制到 bucket2
【发布时间】:2019-01-01 20:34:08
【问题描述】:

我正在使用以下代码从我的云功能中的一个存储复制文件:

exports.copyFile = functions.storage.object().onFinalize((object) => {
const Storage = require('@google-cloud/storage');
const storage = new Storage();
const srcBucketName = 'bucket1';
const srcFilename = object.name;
const destBucketName = 'bucket2';
const destFilename = 'example.png';

storage
.bucket(srcBucketName)
.file(srcFilename)
.copy(storage.bucket(destBucketName).file(destFilename))
.then(() => {
  console.log(
    `gs://${srcBucketName}/${srcFilename} copied to gs://${destBucketName}/${destFilename}.`
  );
  return console.log('done!');
})
.catch(err => {
  console.error('ERROR:', err);
  })
 });

我在日志中收到以下错误:

ERROR: { ApiError: Not Found
at Object.parseHttpRespBody....}

不确定缺少什么。有什么帮助吗?

【问题讨论】:

  • 我的错。 srcBucketName 需要在函数中访问。谷歌文档的危害和复制粘贴习惯:(...exports.copyFile = functions.storage.bucket(srcBucketName).object().onFinalize((object)
  • 您可以发表您的评论作为答案吗?谢谢!
  • @Iñigo 已提交!这有效

标签: firebase google-cloud-platform google-cloud-storage google-cloud-functions


【解决方案1】:
const srcBucketName = 'bucket1';  
exports.copyFile = functions.storage.bucket(srcBucketName).object().onFinalize((object) => {
  const Storage = require('@google-cloud/storage');
  const storage = new Storage();  
  const srcFilename = object.name;
  const destBucketName = 'bucket2';
  const destFilename = 'file2';      
  storage
  .bucket(srcBucketName)
  .file(srcFilename)
  .copy(storage.bucket(destBucketName).file(destFilename))
  .then(() => {
    console.log(
      `gs://${srcBucketName}/${srcFilename} copied to gs://${destBucketName}/${destFilename}.`
    );
    return console.log('done!');
  })
  .catch(err => {
    console.error('ERROR:', err);
})
});

【讨论】:

  • 感谢您的回答!如果有人因为return console.log('done!') 而收到Admin SDK ERROR: Expression has type void。 Console.log 不返回值,因此您必须将 console.log('done!')return 放在两个单独的行中。
猜你喜欢
  • 2018-06-15
  • 2017-11-29
  • 2018-04-22
  • 1970-01-01
  • 2014-02-21
  • 1970-01-01
  • 2014-09-18
  • 2019-04-22
  • 1970-01-01
相关资源
最近更新 更多