Google Cloud Functions bucket.upload()

﹥>﹥吖頭↗ 提交于 2020-06-14 04:24:49

问题


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.

I'd like to copy the pdf to the pth of the bucket: library-xxxx.appspot.com/Orgs/${params.ukey}.

How to do this?

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

回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/49961149/google-cloud-functions-bucket-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!