arisalexis
arisalexis

Reputation: 2210

Cannot set metadata on a Google Storage Object from Node.js using Firebase Admin

I am writing a Firebase app and I need to change something in the backend running Node.js

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  storageBucket: config.get("firebase.storage")
});

I get the bucket from initializing admin and am able to see the existing metadata from that file. Setting it doesn't work:

const myfile = {URL: "/users/1/demo.png"}; //this file exists

const bucket = admin.storage().bucket();
const [metadata] = await bucket.file(redeemable.URL).getMetadata();
console.log(metadata); //this works
if (!metadata.metadata.whitelist) metadata.metadata.whitelist = {};

metadata.metadata.whitelist[req.user.userId] = true;

await bucket.file(myfile.URL).setMetadata(metadata);

Shouldn't the firebase admin have access rights? I also don't get any error.

Upvotes: 6

Views: 1758

Answers (1)

dwelle
dwelle

Reputation: 7284

As the OP figured out in the comments, metadata object must be of type { metadata: Record<string, string> }. The documentation doesn't mention this, and the TypeScript typings do not help either.

EDIT: the docs do actually mention it (emphasis mine):

You can set custom key/value pairs in the metadata key of the given object,

but the wording makes it is easy to miss.

bucket
    .file("path/to/file")
    .setMetadata({
      metadata: {
        someKey: "someValue",
      },
    })

Upvotes: 5

Related Questions