问题
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'
});
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\'' } }
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.
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?
Edit 1
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
Of course I already have a folder called listings
in my firebase storage. So why on earth would the second bucket be undefined?
回答1:
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');
来源:https://stackoverflow.com/questions/53474205/firebase-storage-admin-error400-invalid-bucket-name