【发布时间】:2018-07-22 05:43:49
【问题描述】:
我正在解析从 MailGun 转发的一些电子邮件,同时尝试存储附件时出现以下错误:
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)
这里是初始化
// 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');
我正在通过 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));
});
这就是失败的地方
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);
}
});
【问题讨论】:
标签: firebase google-cloud-functions firebase-storage