错误:**** @ appspot.gserviceaccount.com没有storage.objects.get访问权限

时间:2019-08-10 16:06:00

标签: firebase google-cloud-functions google-cloud-storage react-native-firebase

我有一个简单的firebase函数,该函数会在将文件上传到Firebase Storage时触发。它正在非主存储桶上运行,但是一旦将其更改为侦听主存储桶,我就开始收到这些错误消息。...

Error: *****@appspot.gserviceaccount.com does not have storage.objects.get access to *****.appspot.com/ff-icon-01.png.

该功能与存储桶位于同一项目中。

const admin = require('firebase-admin');
admin.initializeApp();
const functions = require('firebase-functions');

const {Storage} = require('@google-cloud/storage');
const gcs = new Storage();

import { tmpdir } from 'os';
import { join, dirname } from 'path';

import * as sharp from 'sharp';
import * as fs from 'fs-extra';

export const makeThumbnail = functions.storage
  .object()
  .onFinalize(async object => {
    const bucket = gcs.bucket(object.bucket);
    const filePath = object.name;
    const fileName = filePath.split('/').pop();
    const bucketDir = dirname(filePath);

    const workingDir = join(tmpdir(), 'thumbs');
    const tmpFilePath = join(workingDir, 'source.png');

    if (fileName.includes('thumb@') || !object.contentType.includes('image')) {
      console.log('exiting function');
      return false;
    }

    // 1. Ensure thumbnail dir exists
    await fs.ensureDir(workingDir);

    // 2. Download Source File
    await bucket.file(filePath).download({
      destination: tmpFilePath
    });

    // 3. Resize the images and define an array of upload promises
    const sizes = [64, 128, 256];

    const uploadPromises = sizes.map(async size => {
      const thumbName = `thumb@${size}_${fileName}`;
      const thumbPath = join(workingDir, thumbName);

      // Resize source image
      await sharp(tmpFilePath)
        .resize(size, size)
        .toFile(thumbPath);

      // Upload to GCS
      return bucket.upload(thumbPath, {
        destination: join(bucketDir, thumbName)
      });
    });

    // 4. Run the upload operations
    await Promise.all(uploadPromises);

    // 5. Cleanup remove the tmp/thumbs from the filesystem
    return fs.remove(workingDir);
  });

它们具有相同的规则。不知道怎么了。

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

1 个答案:

答案 0 :(得分:1)

针对遇到此问题的其他人。

对我来说,问题是上传函数时我在gcloud设置中使用了错误的项目。因此,我在firebase cli中使用了一个项目,而在gcloud cli中使用了另一个项目。

当我删除所有功能,将gcloud cli项目更改为正确的项目,然后再次上传功能时,它为我工作。