Javascript 错误:匿名调用方没有storage.objects.get访问Google云存储对象
我正在尝试跟踪firebase中的图像大小调整器。我尝试用普通的NodeJS语法重写代码,并从其他来源借鉴了一些想法。下面是我在index.js中的最终代码Javascript 错误:匿名调用方没有storage.objects.get访问Google云存储对象,javascript,node.js,firebase,google-cloud-firestore,firebase-storage,Javascript,Node.js,Firebase,Google Cloud Firestore,Firebase Storage,我正在尝试跟踪firebase中的图像大小调整器。我尝试用普通的NodeJS语法重写代码,并从其他来源借鉴了一些想法。下面是我在index.js中的最终代码 const functions = require('firebase-functions') const { Storage } = require('@google-cloud/storage'); const projectId = "REDACTED INFO" let gcs = new Storage ({ projec
const functions = require('firebase-functions')
const { Storage } = require('@google-cloud/storage');
const projectId = "REDACTED INFO"
let gcs = new Storage ({
projectId
});
const os = require('os');
const path = require('path');
const sharp = require('sharp');
const fs = require('fs-extra');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
exports.onFileChange = functions.storage.object().onFinalize(async object => {
const bucket = gcs.bucket(object.bucket);
const filePath = object.name;
const fileName = filePath.split('/').pop();
const bucketDir = path.dirname(filePath);
const workingDir = path.join(os.tmpdir(), 'thumbs');
// const tmpFilePath = path.join(workingDir, 'source.png');
const tmpFilePath = path.join(workingDir, fileName);
//const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath));
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 = path.join(workingDir, thumbName);
// Resize source image
await sharp(tmpFilePath)
.resize(size, size)
.toFile(thumbPath);
// Upload to GCS
return bucket.upload(thumbPath, {
destination: path.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);
});
exports.onFileDelete = functions.storage.object().onDelete(event => {
console.log(event);
console.log('We deleted a file, exit...')
return;
});
但是,当我尝试上载图像时,我在firebase控制台日志中不断收到这些错误和警告
Error: Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.
at new ApiError (/srv/node_modules/@google-cloud/common/build/src/util.js:59:15)
at Util.parseHttpRespMessage (/srv/node_modules/@google-cloud/common/build/src/util.js:161:41)
at Util.handleResp (/srv/node_modules/@google-cloud/common/build/src/util.js:135:76)
at Duplexify.requestStream.on.on.res (/srv/node_modules/@google-cloud/storage/build/src/file.js:823:31)
at emitOne (events.js:116:13)
at Duplexify.emit (events.js:211:7)
at emitOne (events.js:116:13)
at DestroyableTransform.emit (events.js:211:7)
at onResponse (/srv/node_modules/retry-request/index.js:200:19)
at PassThrough.<anonymous> (/srv/node_modules/retry-request/index.js:152:11)
有人知道我错过了哪些步骤吗?如果需要更多信息,一定要通知我。谢谢。Works,也帮我节省了使用
@googlecloud/storage
的时间
Try this:
import * as admin from "firebase-admin";
admin.initializeApp(functions.config().firebase);
const gcs = admin.storage();
Try this:
import * as admin from "firebase-admin";
admin.initializeApp(functions.config().firebase);
const gcs = admin.storage();