将文件从bucket1复制到Google Cloud Storage中的bucket2

时间:2018-07-25 06:05:24

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

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

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....}

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

1 个答案:

答案 0 :(得分:2)

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);
})
});