首页 > 解决方案 > Google Cloud Storage `predefinedAcl` 和 `file.makePublic()` 不起作用

问题描述

当我从 Firebase Cloud Functions (Google Cloud Functions) 将文件上传到 Firebase Storage (Google Cloud Storage) 时,我正在尝试获取永久下载 URL。

我尝试设置predefinedAcltoauthenticatedRead和 to publicRead403 (Forbidden)当我的应用程序尝试下载文件时,两者都导致错误。这来自CreateWriteStreamOptions的文档。这是代码:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage({ projectId: 'languagetwo-cd94d' });
const myBucket = storage.bucket('languagetwo-cd94d.appspot.com');

var mp3Promise = new Promise(function(resolve, reject) {
      let options = {
        metadata: {
          contentType: 'audio/mp3',
          public: true
        }
      };

      synthesizeParams.accept = 'audio/mp3';
      var file = myBucket.file('Audio/' + longLanguage + '/' + pronunciation + '/' + word + '.mp3');
      textToSpeech.synthesize(synthesizeParams)
      .then(function(audio) {
        audio.pipe(file.createWriteStream(options));
      })
      .then(function() {
        resolve('http://storage.googleapis.com/languagetwo-cd94d.appspot.com/Audio/' + longLanguage + '/' + pronunciation + '/' + word + '.mp3');
      })
      .catch(error => console.error(error));
    });

该代码执行没有错误,将文件写入存储,并将下载 URL 传递给下一个函数。当我尝试使用此 URL 下载文件时:

http://storage.googleapis.com/languagetwo-cd94d.appspot.com/Audio/English/United_States-Allison-Female-IBM/catbirds.mp3

我收到此错误:

<Error>
<Code>AccessDenied</Code>
<Message>Access denied.</Message>
<Details>
Anonymous caller does not have storage.objects.get access to languagetwo-cd94d.appspot.com/Audio/English/United_States-Allison-Female-IBM/catbirds.mp3.
</Details>
</Error>

从我的应用程序下载文件(作为授权用户)我收到403 (Forbidden)错误消息。

我也试过这个属性,结果相同:

let options = {
        metadata: {
          contentType: 'audio/webm',
          predefinedAcl: 'publicRead'
        }
      };

继续前进,我尝试了file.makePublic()

const {Storage} = require('@google-cloud/storage');
const storage = new Storage({ projectId: 'languagetwo-cd94d' });
const myBucket = storage.bucket('languagetwo-cd94d.appspot.com');

var mp3Promise = new Promise(function(resolve, reject) {
      let options = {
        metadata: {
          contentType: 'audio/mp3'
        }
      };

      synthesizeParams.accept = 'audio/mp3';
      var file = myBucket.file('Audio/' + longLanguage + '/' + pronunciation + '/' + word + '.mp3');
      textToSpeech.synthesize(synthesizeParams)
      .then(function(audio) {
        audio.pipe(file.createWriteStream(options));
      })
      .then(function() {
        file.makePublic()
        .then(function(data) {
          console.log(data)
          resolve('http://storage.googleapis.com/languagetwo-cd94d.appspot.com/Audio/' + longLanguage + '/' + pronunciation + '/' + word + '.mp3');
        })
        .catch(error => console.error(error));
      })
      .catch(error => console.error(error));
    });

这段代码甚至没有执行。它崩溃了file.makePublic(),错误消息是

{ Error: No such object: languagetwo-cd94d.appspot.com/Audio/English/United_States-Allison-Female-IBM/warblers.mp3

我不确定这个错误是什么意思。我的猜测是file.createWriteStream()写入文件位置,然后file.makePublic()找不到该文件位置。

warblers.mp3我在云存储浏览器上找到了权限:

在此处输入图像描述

标签: google-cloud-functionsgoogle-cloud-storagefirebase-storage

解决方案


第一个错误是由于对存储桶的访问权限,在链接中您可以找到身份和访问管理 (IAM)说明,在那里您可以检查角色和权限来管理存储桶访问。

第二个错误可能是第一个错误的结果。

请让我知道这些信息是否适合您。


推荐阅读