简体   繁体   English

Firebase 云函数服务本地文件以供下载

[英]Firebase Cloud Function Serving Local File for Download

I created cloud function that generates an xlsx file, I need the user to download that file after it's generated.我创建了生成 xlsx 文件的云函数,我需要用户在生成后下载该文件。

Method 1: Upload to Bucket, then redirect方法一:上传到Bucket,然后重定向

So far i've tried uploading the file to a bucket using this API , and then redirect him to the bucket file url, I also double checked the bucket name using this API , but I get the same error every time:到目前为止,我已经尝试使用此API将文件上传到存储桶,然后将他重定向到存储桶文件 url,我还使用此API仔细检查了存储桶名称,但每次都遇到相同的错误:

{"error":{"code":500,"status":"INTERNAL","message":"function crashed","errors":["socket hang up"]}} {"error":{"code":500,"status":"INTERNAL","message":"function crashed","errors":["socket hang up"]}}

Portion of the code that contains uploading to bucket:包含上传到存储桶的代码部分:

    const { Storage } = require('@google-cloud/storage');
    const storage = new Storage();
    await storage.bucket('bucket-name').upload('myfile.xlsx', {
        gzip: false,
    });

Portion of the code that proves file exists:证明文件存在的代码部分:

    fs.access('myfile.xlsx', fs.constants.F_OK, (err) => {
        console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
    });

I also checked if the library "@google-cloud/storage" reads the file, and it reads it correctly and gets the file size right.我还检查了库“@google-cloud/storage”是否读取了该文件,它是否正确读取并获取了正确的文件大小。

Method 2: Direct Download方法二:直接下载

Download the file directly, the problem is that every doc online for nodejs to download a local file to the user is setting up a custom server to download the file, but i'm using firebase, so it's not in control of that server.直接下载文件,问题是nodejs下载本地文件给用户的每个在线文档都在设置自定义服务器来下载文件,但我使用的是firebase,所以它不受该服务器的控制。

If your excel file is created and should be returned to the client as a response to an HTTP request (calling to an API endpoint) then this is how you can do it.如果您的 excel 文件已创建并且应该作为对 HTTP 请求(调用 API 端点)的响应返回给客户端,那么您可以这样做。

export const getExcelFile = functions.https.onRequest(async (request, response) => {
    // ...
    // Create your file and such
    // ..

    await storage.bucket('bucket-name').upload('myfile.xlsx', {
        gzip: false,
    });

    response.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    response.send(fs.readFileSync('myfile.xlsx'));
    return null;
});

Otherwise, if the excel file is created as a response to an event, and you want the user to download the file at another time, then you create a download link and serve it to the user in any way you want.否则,如果创建 excel 文件作为对事件的响应,并且您希望用户在其他时间下载该文件,则您可以创建一个下载链接并以您想要的任何方式将其提供给用户。

// ...
// Create your file and such
// ..


const [file] = await storage.bucket('bucket-name').upload('myfile.xlsx', {
    gzip: false,
});
const [downloadUrl] = await file.getSignedUrl({ 
  action: 'read',
  expires: '20-03-2019' // Link expiry date: DD-MM-YYYY
});

console.log(downloadUrl);

Just wanted to add more detail to the answer, since there's no need to write into a file and read from it to download it's data, simply take the data and send it, using the few lines below.只是想为答案添加更多细节,因为无需写入文件并从中读取以下载其数据,只需使用以下几行获取数据并发送即可。

    res.setHeader('Content-Type', 'application/vnd.openxmlformats');
    res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    res.end(fileData, 'binary');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase的云功能图像下载功能错误 - Cloud Functions for Firebase Image download function Error 本地云功能Firebase将数据添加到Firestore - Local Cloud Function Firebase add data to Firestore 为什么通过 Firebase 中的 Express 应用从 Google Cloud Storage 提供 PDF 时,Chrome 中的下载会挂起? - Why is download hanging in Chrome when serving PDF from Google Cloud Storage via an Express App in Firebase? 可以使用云 function 将视频下载到 Firebase 服务器上吗? - Possible to use cloud function to download video onto Firebase server? 使用下载 url 和 Cloud Functions 从 Firebase 存储中删除文件 - Delete a file from firebase storage using download url with Cloud Functions 从使用 Cloud Functions 上传的文件中获取下载 URL for Firebase - Get Download URL from file uploaded with Cloud Functions for Firebase 如何通过带有firebase功能的FTP下载文件? - How to download a file through FTP with a firebase function? 参数未传递给 firebase 云函数中的本地函数 - Param didn't pass to local function in firebase cloud functions 我可以制作一个仅适用于本地 Firebase 模拟器的云 Function 吗? - can I make a Cloud Function that only for local Firebase Emulators? Firebase 云 function 本地代码更改未反映在模拟器中 - Firebase cloud function local code changes are not reflected in emulators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM