Google Cloud Functions bucket.upload() [英] Google Cloud Functions bucket.upload()

查看:111
本文介绍了Google Cloud Functions bucket.upload()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用由firebase写入触发的google函数将pdf文件从远程网站存档到Google Cloud Storage.

I'm trying to archive pdf files from remote websites to Google Cloud Storage using a google function triggered by a firebase write.

以下代码有效.但是,此功能会将远程文件复制到存储桶根目录.

The code below works. However, this function copies the remote file to the bucket root.

我想将pdf复制到存储桶的第pth个:library-xxxx.appspot.com/Orgs/${params.ukey}.

I'd like to copy the pdf to the pth of the bucket: 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);
        });
    }
});

推荐答案

让我首先简要说明一下GCS文件系统的工作方式:如

Let me begin with a brief explanation of how GCS file system works: as explained in the documentation of Google Cloud Storage, GCS is a flat name space where the concept of directories does not exist. If you have an object like gs://my-bucket/folder/file.txt, this means that there is an object called folder/file.txt stored in the root directory of gs://my-bucket, i.e. the object name includes / characters. It is true that the GCS UI in the Console and the gsutil CLI tool make the illusion of having a hierarchical file structure, but this is only to provide more clarity for the user, even though those directories do not exist, and everything is stored in a "flat" name space.

话虽如此,如 storage.bucket.upload()方法的参考中所述,您可以指定包含destination字段的options参数,在其中可以指定具有完整文件名的 string .

That being said, as described in the reference for the storage.bucket.upload() method, you can specify an options parameter containing the destination field, where you can specify a string with the complete filename to use.

仅作为示例(请注意两个函数之间的options参数差异):

Just as an example (note the options paramter difference between both functions):

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

因此,在您的情况下,您可以构建一个包含要使用的完整名称的字符串(还包含您要记住的目录"结构).

So in your case you can build a string containing the complete name that you want to use (containing also the "directory" structure you have in mind).

这篇关于Google Cloud Functions bucket.upload()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆