【发布时间】:2019-09-21 07:10:00
【问题描述】:
当我从 Firebase Cloud Functions (Google Cloud Functions) 将文件上传到 Firebase Storage (Google Cloud Storage) 时,我正在尝试获取永久下载 URL。
我尝试将predefinedAcl 设置为authenticatedRead 和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 的权限:
【问题讨论】:
标签: google-cloud-functions google-cloud-storage firebase-storage