google-cloud-functions - Google Cloud Storage `predefinedAcl` 和 `file.makePublic()` 不起作用
问题描述
当我从 Firebase Cloud Functions (Google Cloud Functions) 将文件上传到 Firebase Storage (Google Cloud Storage) 时,我正在尝试获取永久下载 URL。
我尝试设置predefinedAcl
toauthenticatedRead
和 to publicRead
。403 (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
我在云存储浏览器上找到了权限:
解决方案
推荐阅读
- javascript - AngularJS SlimFramework
- c# - LINQ 字符串比较与 NULL 处理
- regex - Python正则表达式区别zA-Z0-9]\S*@\S*[a-zA-Z] vs ('\S+@\S+'
- django - IntegrityError:唯一约束失败:user_userprofile.user_id
- python - ImportError:没有名为“yaml”的模块
- android-studio - zxing 条码未使用导入语句
- dart - Flutter:如何使用 Image 插件调整图像大小
- python - 识别同一行中最接近1000、2000的x、y坐标
- c# - 将(总和)添加到列值中,而不是更新/替换现有值
- elasticsearch - 用于获取图形邻接列表的所有节点的 Elasticsearch 查询(在 javascript 中构建图形)