Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 如何使用云函数中的response.sendFile从云存储发送文件?_Firebase_Google Cloud Storage_Google Cloud Functions - Fatal编程技术网

Firebase 如何使用云函数中的response.sendFile从云存储发送文件?

Firebase 如何使用云函数中的response.sendFile从云存储发送文件?,firebase,google-cloud-storage,google-cloud-functions,Firebase,Google Cloud Storage,Google Cloud Functions,我试图使用firebase云函数的http触发器将响应发送回客户端。 当我使用云存储中的文件位置发送响应时,sendFile方法抛出以下错误: "path must be absolute or specify root to res.sendFile" res.sendFile(obj.path(param1, param2, param3, param4)); StatusCodeError: 403 - "{\n \"error\": {\n \"code\": 403,\n

我试图使用firebase云函数的http触发器将响应发送回客户端。 当我使用云存储中的文件位置发送响应时,sendFile方法抛出以下错误:

"path must be absolute or specify root to res.sendFile"

res.sendFile(obj.path(param1, param2, param3, param4));
StatusCodeError: 403 - "{\n  \"error\": {\n    \"code\": 403,\n    \"message\": \"Permission denied. Could not perform this operation\"\n  }\n}"
path(param1,param2,param3,param4)这将构建一个指向gs://或带有参数的https://的路径

然后我决定这样做:

const rp = require("request-promise");

exports.fun = functions.https.onRequest( async (req, res) => {
let extResponse = await rp('firebase storage location');
          extResponse.pipe(res);
});
rp现在返回此错误:

"path must be absolute or specify root to res.sendFile"

res.sendFile(obj.path(param1, param2, param3, param4));
StatusCodeError: 403 - "{\n  \"error\": {\n    \"code\": 403,\n    \"message\": \"Permission denied. Could not perform this operation\"\n  }\n}"
此错误是因为云存储要求对请求进行身份验证,以便让服务从存储下载文件


有没有一种方法可以使这个工作正常并将文件返回给客户机

sendFile无法工作,因为它不理解URL。它只理解本地文件系统上的文件。您应该使用来执行此操作。创建一个指向要发送的文件的对象,在其上打开一个读取流,然后通过管道将该流传输到响应:

const file = ... // whatever file you want to send
const readStream = file.createReadStream()
readStream.pipe(res)

我无法测试您编写的内容,但我相信这有助于解决问题。一旦我能测试它,我会将它标记为已回答。我在一个工作项目中也会做同样的事情。我借用了其他示例代码中的技术。如果你做得不对,它会起作用的。