【发布时间】:2021-07-31 12:00:41
【问题描述】:
我正在做关于 firebase 函数的课程,下面的函数会将上传到 firebase 存储的图像复制到 GCP 存储桶。
对于讲师来说一切正常,但我收到错误:
上传到VCMBucket 错误:fbpipeline-9876@appspot.gserviceaccount.com 没有 storage.objects.create 对 Google Cloud Storage 对象的访问权限。在新的 ApiError (/workspace/node_modules/@google-cloud/common/build/src/util.js:73:15)
讲师没有提到要设置权限以访问 GCP 存储桶。是否需要执行任何步骤来授予 google 功能以复制到 GCP 存储?
遵循的步骤:
- 设置 Firestore、realtimedb 和 Storage
- 使用 react-firebase-file-uploader 将图像上传到 Firebase 存储。
- 在 GCP 中创建了一个存储桶,如下面的屏幕截图所示
- 将以下函数部署到 firebase 函数以将图像从 firebase 存储复制到 GCP 存储桶
//Project setup
//Automl information
const project_name = 'ml-imageclassifier'
const project_region = 'us-central1'
const dataset_id = 'ICN10548w25656579083008'
const bucket_prefix = 'animals'
const labels = ['dog', 'cat', 'other']
const model_name = `${bucket_prefix}_${new Date().getTime()}`
const num_labels = labels.length;
const img_threshold = 10;
//Dependencies
const fs = require('fs')
const functions = require("firebase-functions");
const firebase = require('firebase-admin');
const { Storage } = require('@google-cloud/storage');
const automl = require('@google-cloud/automl')
const cors = require('cors')({origin: true});
firebase.initializeApp();
const database = firebase.database();
const firestore = firebase.firestore();
const storage = new Storage();
//helper function that taken in a path of the last image
function writeToDB (path) {
console.log(path)
database.ref(path).transaction(function(labelCount) {
return labelCount + 1
})
}
//whenever a image is uploaded to the storage bucket trigger the below to copy the image to GCP storage
exports.uploadToVCMBucket = functions.storage.object().onFinalize(event => {
const file = storage.bucket(event.bucket).file(event.name)
const newLocation = `gs://${project_name}-vcm/${event.name}`
return file.copy(newLocation)
.then((err, copiedFile, resp) => {
return event.name.substring(0, event.name.lastIndexOf('/'))
}).then((label) => {
return writeToDB(label)
});
})
【问题讨论】:
标签: node.js firebase google-cloud-functions google-cloud-storage