Ric*_*rdZ 8 node.js google-cloud-storage firebase google-cloud-platform google-cloud-functions
我正在尝试使用由 firebase 写入触发的 google 功能将 pdf 文件从远程网站存档到 Google Cloud Storage。
下面的代码有效。但是,此函数会将远程文件复制到存储桶根目录。
我想将 pdf 复制到存储桶的 pth: library-xxxx.appspot.com/Orgs/${params.ukey}
。
这该怎么做?
exports.copyFiles = functions.database.ref('Orgs/{orgkey}/resources/{restypekey}/{ukey}/linkDesc/en').onWrite(event => {
const snapshot = event.data;
const params = event.params;
const filetocopy = snapshot.val();
if (validFileType(filetocopy)) {
const pth = 'Orgs/' + params.orgkey;
const bucket = gcs.bucket('library-xxxx.appspot.com')
return bucket.upload(filetocopy)
.then(res => {
console.log('res',res);
}).catch(err => {
console.log('err', err);
});
}
});
Run Code Online (Sandbox Code Playgroud)
dse*_*sto 16
让我首先简要说明 GCS 文件系统的工作原理:如Google Cloud Storage 文档中所述,GCS 是一个平面名称空间,其中不存在目录的概念。如果你有一个像 的对象gs://my-bucket/folder/file.txt
,这意味着有一个名为 的对象folder/file.txt
存储在 的根目录中gs://my-bucket
,即对象名称包含/
字符。确实,Console 中的 GCS UI 和gsutil
CLI 工具会让人产生具有分层文件结构的错觉,但这只是为了给用户提供更清晰的信息,即使这些目录不存在,并且所有内容都存储在一个“平面”名称空间。
话虽如此,如方法参考中所述storage.bucket.upload()
,您可以指定一个options
包含该destination
字段的参数,您可以在其中指定一个包含要使用的完整文件名的字符串。
举个例子(注意options
两个函数的参数区别):
var bucket = storage.bucket('my-sample-bucket');
var options = {
destination: 'somewhere/here.txt'
};
bucket.upload('sample.txt', function(err, file) {
console.log("Created object gs://my-sample-bucket/sample.txt");
});
bucket.upload('sample.txt', options, function(err, file) {
console.log("Created object gs://my-sample-bucket/somewhere/here.txt");
});
Run Code Online (Sandbox Code Playgroud)
因此,在您的情况下,您可以构建一个包含要使用的完整名称的字符串(还包含您想到的“目录”结构)。
归档时间: |
|
查看次数: |
8682 次 |
最近记录: |