从 firebase 下载文件并上传到 Stripe 进行身份验证
Posted
技术标签:
【中文标题】从 firebase 下载文件并上传到 Stripe 进行身份验证【英文标题】:Download file from firebase and upload to stripe for id verification 【发布时间】:2020-01-06 20:12:00 【问题描述】:我正在尝试获取存储在 Firebase 存储中的 id jpeg 并将其上传到条带,以便让我的客户验证他们的身份。但是我遇到了几个错误并且函数停止了
import * as https from 'https'
import * as fs from 'fs'
const stripe = Stripe('sk_test');
function getidFile(url: string):Promise<any>
return new Promise((resolve,reject) =>
const file = fs.createWriteStream("test.jpg");
https.get(
url,
function(response: IncomingMessage)
console.log('Response ', response);
response.pipe(file);
file.on('finish', function()
file.close();
resolve(file);
);
).on('error', function(err) // Handle errors
reject(err);
);
);
function uploadFileToStripe(fileUrl:string,fileType:string,fileName:string):Promise<any>
return getidFile(
fileUrl
).then(
(data:any) =>
console.log('File as blob ', data);
const upload: Promise<any> =
stripe.files.create(
file:
data: data,
name: fileName,
type: 'image/jpeg',
,
purpose: 'identity_document',
);
return upload.then(
(document: any) =>
console.log('Document upload ', document);
return document;
).catch(
(_error: any) =>
console.log('Error uploading document ', _error);
return _error;
)
).catch(
(_error:any) =>
console.log('Error with response ', _error);
return _error;
);
错误:EROFS:只读文件系统,打开“test.jpg” 然后我的功能停止了。
【问题讨论】:
您是否从 Stripe 或您的getidFile
函数返回错误?
根据docs:Modifying a file rather than replacing it may require a flags mode of r+ rather than the default mode w
。如果您在这里遇到错误fs.createWriteStream("test.jpg");
,这可能是问题的根源
【参考方案1】:
我不使用网址。使用 admin.storage() 访问您的文件。
export const uploadFileToStripe = functions.https.onCall(async(data,context)=>
const bucket = admin.storage().bucket('xyz.appspot.com');
const path = require('path');
const os = require('os');
const fs = require('fs');
const fileName = data['fileName'];
const tempFilePath = path.join(os.tmpdir(), fileName);
await bucket.file(fileName).download(destination:tempFilePath);
console.log('File created at', tempFilePath);
return await stripe.files.create(
purpose: 'identity_document',
file:
data: fs.readFileSync(tempFilePath),
name: 'fileUploadDoc.png',
type: 'application/octet-stream',
,
);
);
【讨论】:
以上是关于从 firebase 下载文件并上传到 Stripe 进行身份验证的主要内容,如果未能解决你的问题,请参考以下文章
如何将多个图像上传到 Firebase 存储并返回多个下载 URL
从使用Cloud Functions for Firebase上传的文件中获取下载URL