使用命名存储桶创建存储触发器时权限被拒绝

时间:2018-06-18 20:54:32

标签: javascript firebase google-cloud-functions firebase-storage

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

我对firebase很新。我试图实现,如果我上传图像,它会调整大小并更改名称。

我希望调整大小图像的存储桶名称是“图像” 如果我有权读取和写入,为什么会抱怨权限?

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
        });
      });
  });

1 个答案:

答案 0 :(得分:3)

您要描述的是存储桶内的文件夹,而不是存储桶。您可能想要做的是在触发器定义中执行.bucket().object(),然后在函数顶部执行以下操作:

// 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;
}