Firebase存储管理员错误:400无效的存储桶名称 [英] Firebase Storage Admin Error:400 Invalid Bucket Name
问题描述
我正在尝试使用Firebase函数来维护数据库和存储.过期后,基本上将一些旧条目从一个ref/bucket移到另一个ref/bucket. 数据库部分效果很好.但是,存储部分并没有那么多.这是我初始化代码中所有内容的方法:
I'm trying to use firebase functions to do maintenance of my database and storage. Basically remove some old entries from one ref/bucket to another after they expire. The database part works great. However, the storage part, not so much. Here's how I initialize everything in my code:
var functions = require('firebase-functions');
var admin = require("firebase-admin");
var serviceAccount = require('./my-app-bla-bla.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://my-app.firebaseio.com',
storageBucket: 'gs://my-app.appspot.com'
});
然后在清理数据库和存储的cron作业中,我有以下内容(这只是一些相关的小部分):
Then in the cron job that cleans the database and storage, I have the following (this is only some small relevant part):
const st = admin.storage();
st.bucket("gs://my-app.appspot.com/old-listings/"+listingKey).create(function(error, bucket, apiResponse) {
if (error) {
console.log("Couldn't create an OldListing bucket: " + error.code);
console.log(apiResponse);
} else {
console.log("Created OldListing bucket");
}
});
最后一段代码触发了错误,并提供了以下日志:
This last piece of code triggers the error and gives me the following log:
Couldn't create an OldListing bucket: 400
{ error:
{ errors: [ [Object] ],
code: 400,
message: 'Invalid bucket name: \'my-app.appspot.com/old-listings/SomeUniqueID\'' } }
因为我是第一次运行此代码,所以文件夹old-listings
尚不存在.因此,尽管我也许应该首先自己创建它的存储桶.它给了我同样的错误.
Because I'm running this code for the first time, the folder old-listings
does not exist yet. So I though maybe I should create its bucket on its own first. It gives me the same error.
我也尝试使用没有gs链接的存储桶,例如st.bucket("old-listings/"+listingKey)
代替st.bucket("gs://my-app.appspot.com/old-listings/"+listingKey)
.仍然给我同样的错误.
I also tried using the buckets without the gs link, e.g. st.bucket("old-listings/"+listingKey)
instead of st.bucket("gs://my-app.appspot.com/old-listings/"+listingKey)
. Still gives me the same error.
那么这里到底缺少什么?我在做什么错了?
so what exactly is missing here? What am I doing wrong?
我尝试在cron函数的开头添加以下代码段.为了更好地了解发生了什么.
I tried adding the following code snippet at the beginning of my cron function. In an effort to better understand what's going on.
admin.storage().bucket("my-app.appspot.com").exists(function(error, exists) {
if (!error) {
if (exists) {
console.log("Top Bucket Exists");
} else {
console.log("Top Bucket Does Not Exist");
}
} else {
console.log("Top Bucket Error " + error.code);
}
});
admin.storage().bucket("my-app.appspot.com/listings").exists(function(error, exists) {
if (!error) {
if (exists) {
console.log("Listings Bucket Exists");
} else {
console.log("Listings Bucket Does Not Exist");
}
} else {
console.log("Listings Bucket Error " + error.code);
}
});
我在日志中得到以下内容:
I get the following in my log:
Top Bucket Exists
Listing Bucket Error undefined
当然,我的Firebase存储器中已经有一个名为listings
的文件夹.那么为什么在地球上第二个桶是不确定的?
Of course I already have a folder called listings
in my firebase storage. So why on earth would the second bucket be undefined?
推荐答案
在为存储桶构建名称时,不应在其中包含文件路径组件,而应仅是存储桶的唯一名称-容器为您所有的对象.如果您要引用存储桶中的文件,请使用 bucket对象上的> file()方法来获取
When you build a name to a bucket, it's not supposed to contain file path components in it, It should just be the unique name of the bucket - the container for all your objects. If you want to reference a file in the bucket, use the file() method on the bucket object to get a File object to deal with.
const st = admin.storage();
const bucket = st.bucket('name-of-your-bucket');
const file = bucket.file('name-of-your-file');
这篇关于Firebase存储管理员错误:400无效的存储桶名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!