Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Functions bucket.upload()

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);
        });
    }
});
like image 464
RichardZ Avatar asked Apr 21 '18 23:04

RichardZ


1 Answers

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

like image 111
dsesto Avatar answered Nov 07 '22 15:11

dsesto