How can I read the contents of a new .json cloud storage file from within a cloud function?

eventpassed to my Google cloud function only actually tells me the name of the bucket and file, and whether the file was deleted. Yes, there is more, but all this is not so useful:

{ timestamp: '2017-03-25T07:13:40.293Z', 
eventType: 'providers/cloud.storage/eventTypes/object.change', 
resource: 'projects/_/buckets/my-echo-bucket/objects/base.json#1490426020293545', 
data: { kind: 'storage#object', 
       resourceState: 'exists', 
       id: 'my-echo-bucket/base.json/1490426020293545', 
       selfLink: 'https://www.googleapis.com/storage/v1/b/my-echo-bucket/o/base.json', 
       name: 'base.json', 
       bucket: 'my-echo-bucket', 
       generation: '1490426020293545', 
       metageneration: '1', 
       contentType: 'application/json', 
       timeCreated: '2017-03-25T07:13:40.185Z', 
       updated: '2017-03-25T07:13:40.185Z', 
       storageClass: 'STANDARD', 
       size: '548', 
       md5Hash: 'YzE3ZjUyZjlkNDU5YWZiNDg2NWI0YTEyZWZhYzQyZjY=', 
       mediaLink: 'https://www.googleapis.com/storage/v1/b/my-echo-bucket/o/base.json?generation=1490426020293545&alt=media', contentLanguage: 'en', crc32c: 'BQDL9w==' } 
}

How to get the content , not just the metadata of a new .json file loaded in gs-bucket?

I tried using npm:request()on event.data.selfLink, which is the URL for the file in the storage bucket and received an authorization error:

"code": 401, "message": "Anonymous users does not have storage.objects.get access to object my-echo-bucket/base.json."

There was a similar question on how to read storage buckets, but probably on a different platform. In any case, he was unanswered :

How to view file contents in Google Cloud Storage using javascript `

+3
2

Google URL. request() URL- , .

Google , npm, .

npm i @google-cloud/storage -S

npm google-cloud/storage , API, .

Google - api .. .

const storage = require('@google-cloud/storage')();

, , , , .

, file.download, , , .
Buffer, data.toString('utf-8'), utf-8.

const storage = require('@google-cloud/storage')();

exports.logNewJSONFiles = function logNewJSONFiles(event){
    return new Promise(function(resolve, reject){
        const file = event.data;
        if (!file){
            console.log("not a file event");
            return resolve();
        }
        if (file.resourceState === 'not_exists'){
            console.log("file deletion event");
            return resolve();
        }
        if (file.contentType !== 'application/json'){
            console.log("not a json file");
            return resolve();
        }
        if (!file.bucket){
            console.log("bucket not provided");
            return resolve();
        }
        if (!file.name){
            console.log("file name not provided");
            return resolve();
        }
        (storage
         .bucket(file.bucket)
         .file(file.name)
         .download()
         .then(function(data){
             if (data)
                 return data.toString('utf-8');
         })
         .then(function(data){
             if (data) {
                 console.log("new file "+file.name);
                 console.log(data);
                 resolve(data);
             }
         })
         .catch(function(e){ reject(e); })
             );
    });
};

, :

gcloud beta functions deploy logNewJSONFiles --stage-bucket gs://my-stage-bucket --trigger-bucket gs://my-echo-bucket

Stackdriver: Logging Google Cloud Platform console.log.

: (28 2017 .). , . ECONNRESET, Google Storage Google. , , ... npm: prom-retry , , OK ECONNRESET. - 10 .

npm: maybe-json. npm: pipe-to-storage, , .

+6
npm install @google-cloud/storage --production

package.json:

{
  "main": "app.js",
  "dependencies": {
    "@google-cloud/storage": "^1.2.1"
  }
}

, npm ls , npm ERR! missing:.

app.js:

...

  const storage = require("@google-cloud/storage")();
  storage.
    bucket("mybucket").
    file("myfile.txt").
    download( function(err, contents) {
      console.log(contents.toString());
    } );
+1

Source: https://habr.com/ru/post/1016094/


All Articles