4

I'm parsing some emails forwarded from MailGun, while trying to store the attachment I'm getting the error below:

ApiError: Error during request.
        at Object.parseHttpRespBody (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:193:32)
        at Object.handleResp (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:137:18)
        at /user_code/node_modules/firebase-admin/node_modules/@google-cloud/common/src/util.js:496:12
        at Request.onResponse [as _callback] (/user_code/node_modules/firebase-admin/node_modules/retry-request/index.js:195:7)
        at Request.self.callback (/user_code/node_modules/firebase-admin/node_modules/request/request.js:186:22)
        at emitTwo (events.js:106:13)
        at Request.emit (events.js:191:7)
        at Request.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/request/request.js:1163:10)
        at emitOne (events.js:96:13)
        at Request.emit (events.js:188:7)

Here's the initialization

// Initializes the database and storage
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const storage = admin.storage();
const bucket = storage.bucket('gs://my-bucket-address');

I'm grabbing the file via busboy

busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
    // Note that os.tmpdir() is an in-memory file system
    const filepath = path.join(tmpdir, filename)
    uploads[fieldname] = filepath;
    file.pipe(fs.createWriteStream(filepath));
});

Here's where it fails

busboy.on('finish', () => {
    const body_text = formData['body-plain'];
    const body_data = JSON.parse(body_text);
    const sender = body_data.sender;

    for (const name in uploads) {
        const file = uploads[name];

        const metadata = { contentType: 'image/jpeg'};
        const random_id = uuid4();
        const destination = `images/${sender}/${random_id}.jpg`;

        bucket.upload(filepath, { destination: destination, metadata: metadata });
        fs.unlinkSync(file);
    }

});

1 Answer 1

6

I had to use GCS and not functions.storage.bucket. I was also having the same issue with GCS but that was because I was initializing GCS before initializing the the app with initializeApp. This was causing GCS not being properly authenticated. I just which the API error was a bit more helpful.

EDIT: Added code for reference

// Import the modules
const functions = require('firebase-functions');
const admin = require('firebase-admin');

// Initialize Admin FIRST
admin.initializeApp();

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

// Than you can access the bucket
const bucket = gcs.bucket('your-bucket.appspot.com');

You can find your bucket url here

Sign up to request clarification or add additional context in comments.

2 Comments

Can you post the final code that worked as a reference?
@Shaun Code added, sorry for the late reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.