node.js - 从 firebase 下载文件并上传到 stripe 进行身份验证

标签 node.js typescript firebase google-cloud-functions stripe-payments

我试图获取存储在 firebase 存储中的 id jpeg 并将其上传到 stripe,以便我的客户验证他们的身份。但是我遇到了几个错误并且功能停止了

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” 然后我的功能停止。

最佳答案

我不使用 URL。使用 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',
    },
  });
});

关于node.js - 从 firebase 下载文件并上传到 stripe 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57779826/

相关文章:

javascript - 如何使用 Node.js 在 Hexo 等框架中编辑 CSS 和 HTML

typescript - 在引用其他命名空间的同时导出环境声明中的命名空间

ios - 如何使用 Firestore 中的对象将数据设置为数组

node.js - 我需要安装哪些软件包才能完成 "vue build <src path> --config <config file path> --dist <dist location> --prod --lib"命令?

node.js 段错误 :11

node.js - npm 安装返回错误。显示 "no such file directory ../node_modules/mongodb-client-encryption/deps/lib/...'

firebase - 在Firebase中处理推荐系统的大量数据

typescript - TS 标记字符串作为对象的键

angular - 类型 'share' 上不存在属性 'Navigator'

python - 火力地堡 "message": "PASSWORD_LOGIN_DISABLED" response when trying to authenticate