Anton
Anton

Reputation: 1502

Permission denied when creating a storage trigger with a named storage bucket

Insufficient permissions to (re)configure a trigger (permission denied for bucket images). Please, give owner permissions to the editor role of the bucket and try again.

enter image description here

I am pretty new to firebase. I am trying to implement that if i upload image it resizes it and change the name.

The bucket name in what i want to have resized images is "images" Why it complains about permissions if i have access to read and write?

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

_

const functions = require("firebase-functions");
const gcs = require("@google-cloud/storage")();
const os = require("os");
const path = require("path");
const spawn = require("child-process-promise").spawn;

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.onFileChange = functions.storage
  .bucket("images")
  .object()
  .onFinalize(event => {
    const bucket = event.bucket;
    const contentType = event.contentType;
    const filePath = event.name;
    console.log("File change detected, function execution started");

    if (object.resourceState === "not_exists") {
      console.log("We deleted a file, exit...");
      return;
    }

    if (path.basename(filePath).startsWith("resized-")) {
      console.log("We already renamed that file!");
      return;
    }

    const destBucket = gcs.bucket(bucket);
    const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath));
    const metadata = { contentType: contentType };
    return destBucket
      .file(filePath)
      .download({
        destination: tmpFilePath
      })
      .then(() => {
        return spawn("convert", [
          tmpFilePath,
          "-resize",
          "500x500",
          tmpFilePath
        ]);
      })
      .then(() => {
        return destBucket.upload(tmpFilePath, {
          destination: "resized-" + path.basename(filePath),
          metadata: metadata
        });
      });
  });

Upvotes: 1

Views: 811

Answers (1)

Michael Bleigh
Michael Bleigh

Reputation: 26343

What you're describing is a folder inside a bucket, not a bucket. What you likely want to do instead do .bucket().object() in your trigger definition and then, at the top of your function, do something like:

// Exit if this is triggered on a file that is not an image.
if (!filePath.startsWith('images/')) {
  console.log('This is not in the images directory.');
  return null;
}

Upvotes: 4

Related Questions