【发布时间】:2020-10-18 09:07:01
【问题描述】:
大家。我已经经历了几个小时,我需要一些帮助。
我正在尝试设置一个云函数来调整上传到 Firebase 云存储的图像的大小。我关注了谷歌的文档,我通过 GCF 下载和重新上传的每个文件都无法访问,我不知道为什么。对于初学者,我只是想在函数中重新上传我正在下载的图像,但我什至无法做到这一点。
这是 GCF 本身:
const functions = require('firebase-functions');
const path = require('path');
const os = require('os');
const fs = require('fs-extra');
const { Storage } = require('@google-cloud/storage');
const gcs = new Storage();
exports.generateOptions = 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(), 'copies');
const tmpFilePath = path.join(workingDir, fileName);
if (fileName.includes('copy_') || !object.contentType.startsWith('image/')) {
return false;
}
await fs.ensureDir(workingDir);
await bucket.file(filePath).download({
destination: tmpFilePath
});
const copyName = `copy_${fileName}`;
await bucket.upload(tmpFilePath, {
destination: path.join(bucketDir, copyName)
})
// Cleanup
return fs.remove(workingDir);
});
现在,我设置了标志以查看存储桶和文件是否存在(使用来自 @google-cloud/storage 包的 bucket.exists() 和 bucket.file.exists() 方法)并且它们存在并且文件据我所知已正确下载。
我重新上传文件后,函数运行正常,我最终得到了原始文件和复制文件,如下所示:
到目前为止一切顺利。但是当我尝试打开文件时,它卡住了,没有打开它,也没有返回下载链接。
所以我尝试以另一种方式打开文件,使用这个:
点击“打开”(西班牙语为“Abrir”)后,它会打开一个新选项卡,显示以下链接:
因此,我没有获得常规的下载 URL,而是得到了“未定义”,我不知道为什么。
我希望有人有某种解决方案或解决方法,或者以前遇到过类似的问题。
这是 package.json(不管它值多少钱):
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"dependencies": {
"@google-cloud/storage": "^5.1.1",
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1",
"fs-extra": "^9.0.1",
"sharp": "^0.25.4"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
任何帮助将不胜感激!
【问题讨论】:
标签: node.js firebase google-cloud-functions google-cloud-storage firebase-storage