Firebase cloud function API Error with Google Cloud Storage

With the introduction of Firebase Cloud Functions, we are looking at porting some of our current node.js server code to cloud functions. One of the problems I encountered is downloading a file from the GCS bucket to a temporary file on disk, and then sending it via email as an attachment (using mailgun-js).

A piece of code causing me grief:

return mkdirp(tempLocalDir).then(() => {
    const bucket = gcs.bucket(gcsBucket);
    const tempFilePath = tempLocalDir + gcsFile;
    return bucket.file(gcsFile).download({
        destination: tempFilePath
    }).then(() => {
        console.log('File downloaded locally to', tempFilePath);
        var messageSubject = "Test";
        var messageBody = "Test with attach";

        var mailgunData = {
            from: ,
            to: agentEmail,
            subject: messageSubject,
            html: messageBody,
            attachment: tempFilePath,
        };
        mailgunAgent.messages().send(mailgunData, function (error, body) {
            console.log(body);
        });

    });

});

The error message I get in the function logs is:

ApiError: Forbidden
    at Object.parseHttpRespMessage (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:156:33)
    at Object.handleResp (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:131:18)
    at Duplexify.<anonymous> (/user_code/node_modules/@google-cloud/storage/src/file.js:724:21)
    at emitOne (events.js:96:13)
    at Duplexify.emit (events.js:188:7)
    at emitOne (events.js:96:13)
    at DestroyableTransform.emit (events.js:188:7)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at Request.<anonymous> (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1108:14)

/tmp/ , , GCS, . "" auth GCS, , . auth .config() GCS, Firebase? , ? GCS Firebase Storage node, .

,

+2
3

SOLVED: , . , @google-cloud/storage auth GCS , admin-sdk Firebase, GCS. Firebase auth GCS Firebase .

_ (ツ) _/¯

, , , , .

, grll :

const gcs = require('@google-cloud/storage');
const storageClient = gcs({
    projectId: "this is the GCS project ID,
    keyFilename: 'this is the firebase admin-sdk keyFilename.json'
});
const bucket = storageClient.bucket(gcsBucket);
+3

projectId keyFilename:

  • Firebase
  • IAM
  • "App Engine " " Google API" " ".
  • !
+2

, API Google. API, .

- API Google: JSON . Firebase.

, :
const bucket = gcs.bucket(gcsBucket);

:

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

const storageClient = gcs({ projectId: projectId, keyFilename: './keyFilename.json' }); const bucket = storageClient.bucket(gcsBucket);
With your identifier projectId and keyFilename.

0
source

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