我跟随学院的youtube视频去看"Firebase Cloud Functions --上传后调整图像大小“,但我被卡住了,试图用storage.bucket(存储桶)解决一个问题。
这是我面临的错误:
1. (node:2) MetadataLookupWarning: received unexpected error = URL is not defined code = UNKNOWN
2. 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) 代码:
exports.onFileChange = functions.storage.object().onFinalize((object, context) => {
// ...
// Extract object data - left out to focus on the other parts of the function
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage({
projectId: "dude-garden-care"
});
console.log(object, context)
const bucket = object.bucket;
const contentType = object.contentType;
const filePath = object.name;
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 = storage.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,
})
})
})非常感谢你们的帮助
发布于 2020-06-08 10:27:42
我用以下方法解决了这个问题
const admin = require('firebase-admin');
const storage = admin.storage();
const destBucket = storage.bucket(bucket)希望它能帮助到一些人。
https://stackoverflow.com/questions/62245148
复制相似问题