从使用 Cloud Functions for Firebase 上传的文件中获取下载 URL [英] Get Download URL from file uploaded with Cloud Functions for Firebase

查看:29
本文介绍了从使用 Cloud Functions for Firebase 上传的文件中获取下载 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Firebase 函数在 Firebase 存储中上传文件后,我想获取文件的下载网址.

After uploading a file in Firebase Storage with Functions for Firebase, I'd like to get the download url of the file.

我有这个:

...

return bucket
    .upload(fromFilePath, {destination: toFilePath})
    .then((err, file) => {

        // Get the download url of file

    });

目标文件有很多参数.甚至一个名为mediaLink.但是,如果我尝试访问此链接,则会收到此错误:

The object file has a lot of parameters. Even one named mediaLink. However, if I try to access this link, I get this error :

匿名用户没有 storage.objects.get 访问对象......

Anonymous users does not have storage.objects.get access to object ...

谁能告诉我如何获取公共下载地址?

Can somebody tell me how to get the public download Url?

谢谢

推荐答案

您需要使用 getSignedURL 通过 @google-cloud/storage NPM 模块.

You'll need to generate a signed URL using getSignedURL via the @google-cloud/storage NPM module.

示例:

const gcs = require('@google-cloud/storage')({keyFilename: 'service-account.json'});
// ...
const bucket = gcs.bucket(bucket);
const file = bucket.file(fileName);
return file.getSignedUrl({
  action: 'read',
  expires: '03-09-2491'
}).then(signedUrls => {
  // signedUrls[0] contains the file's public URL
});

您需要使用 @google-cloud/storagerel="noreferrer">您的服务帐户凭据,因为应用程序默认凭据是不够的.

You'll need to initialize @google-cloud/storage with your service account credentials as the application default credentials will not be sufficient.

更新:现在可以通过 Firebase Admin SDK 访问 Cloud Storage SDK,充当@google-cloud/storage 的包装器.唯一的方法是:

UPDATE: The Cloud Storage SDK can now be accessed via the Firebase Admin SDK, which acts as a wrapper around @google-cloud/storage. The only way it will is if you either:

  1. 使用特殊服务帐户初始化 SDK,通常是通过第二个非默认实例.
  2. 或者,在没有服务帐户的情况下,向默认 App Engine 服务帐户授予signBlob"权限.

这篇关于从使用 Cloud Functions for Firebase 上传的文件中获取下载 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆