firebase - 从 Google Cloud 函数上传到 Firebase 存储
问题描述
我正在尝试创建一个 Firebase 函数,该函数允许我传入一组图像 URL 以创建蒙太奇,将文件上传到 Firebase 存储,然后返回生成的下载 URL。这将从我的应用程序中调用,所以我使用functions.https.onCall
.
const functions = require("firebase-functions");
const admin = require('firebase-admin');
var gm = require('gm').subClass({imageMagick: true});
admin.initializeApp();
exports.createMontage = functions.https.onCall((data, context) => {
var storageRef = admin.storage().bucket( 'gs://xyz-zyx.appspot.com' );
var createdMontage = storageRef.file('createdMontage.jpg');
function generateMontage(list){
let g = gm()
list.forEach(function(p){
g.montage(p);
})
g.geometry('+81+81')
g.density(5000,5000)
.write(createdMontage, function(err) {
if(!err) console.log("Written montage image.");
});
return true
}
generateMontage(data)
return createdMontage.getDownloadURL();
});
该函数generateMontage()
在 NodeJs 上本地工作(具有本地写入目标)。
谢谢你。
解决方案
看看文档中的这个例子:
https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-code-sample
2021-01-11 更新
这是一个工作示例。我正在使用常规的云函数,它的限制在于srcObject
,dstObject
和bucketName
是常量,但是它确实创建了蒙太奇,这是您的目标。
PROJECT=[[YOUR-PROJECT]]
BILLING=[[YOUR-BILLING]]
REGION=[[YOUR-REGION]]
FUNCTION=[[YOUR-FUNCTION]]
BUCKET=[[YOUR-BUCKET]]
OBJECT=[[YOUR-OBJECT]] # Path from ${BUCKET} root
gcloud projects create ${PROJECT}
gcloud beta billing projects link ${PROJECT} \
--billing-account=${BILLING}
gcloud services enable cloudfunctions.googleapis.com \
--project=${PROJECT}
gcloud services enable cloudbuild.googleapis.com \
--project=${PROJECT}
gcloud functions deploy ${FUNCTION} \
--memory=4gib \
--max-instances=1
--allow-unauthenticated \
--entry-point=montager \
--set-env-vars=BUCKET=${BUCKET},OBJECT=${OBJECT} \
--runtime=nodejs12 \
--trigger-http \
--project=${PROJECT} \
--region=${REGION}
ENDPOINT=$(\
gcloud functions describe ${FUNCTION} \
--project=${PROJECT} \
--region=${REGION} \
--format="value(httpsTrigger.url)")
curl \
--request GET \
${ENDPOINT}
`package.json`:
```JSON
{
"name": "montage",
"version": "0.0.1",
"dependencies": {
"@google-cloud/storage": "5.7.1",
"gm": "^1.23.1"
}
}
并且index.js
:
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
const gm = require('gm').subClass({ imageMagick: true });
const bucketName = process.env["BUCKET"];
const srcObject = process.env["OBJECT"];
const dstObject = "montage.png";
// Creates 2x2 montage
const list = [
`/tmp/${srcObject}`,
`/tmp/${srcObject}`,
`/tmp/${srcObject}`,
`/tmp/${srcObject}`
];
const montager = async (req, res) => {
// Download GCS `srcObject` to `/tmp`
const f = await storage
.bucket(bucketName)
.file(srcObject)
.download({
destination: `/tmp/${srcObject}`
});
// Creating GCS write stream for montage
const obj = await storage
.bucket(bucketName)
.file(dstObject)
.createWriteStream();
let g = gm();
list.forEach(f => {
g.montage(f);
});
console.log(`Returning`);
g
.geometry('+81+81')
.density(5000, 5000)
.stream()
.pipe(obj)
.on(`finish`, () => {
console.log(`finish`);
res.status(200).send(`ok`);
})
.on(`error`, (err) => {
console.log(`error: ${err}`);
res.status(500).send(`uhoh!`);
});
}
exports.montager = montager;
推荐阅读
- c# - 当应用程序是 64 位时,Wix 工具集不会在 64 位程序文件文件夹中安装 WPF 应用程序
- postgresql - 以更好的优化方式重写查询
- excel - Tableau,如何用sql查询自动化excel文件每天刷新?
- python - BS4 分页结果限制:10 而不是 200
- javascript - 在 Javascript 中更改图像
- python - 尝试为我的对抗性攻击显示不同的图像
- reactjs - 在包含多个文档的集合中查询文档 ID
- oracle - Oracle Application Express 伊斯兰或 Persion 日期打包程序
- javascript - Jquery将类添加到不同按钮上的div
- javascript - Javascript 控制台在折叠和展开时显示不同的值,并且与引用时不同