gpt4 book ai didi

node.js - 我可以通过 Firebase Cloud Functions 压缩 Firebase 存储中的文件吗?

转载 作者:行者123 更新时间:2023-12-03 12:12:47 26 4
gpt4 key购买 nike

是否可以使用 Cloud Functions 压缩 Firebase 存储中的多个文件?

例如,用户上传了 5 张图片,Firebase Cloud Functions 将为这 5 张图片创建一个 zip 文件

最佳答案

在我自己的函数中找不到类似场景的 e2e 指南,所以不得不结合压缩、访问云存储中的文件等的解决方案。见下面的结果:

import * as functions from 'firebase-functions';
import admin from 'firebase-admin';
import archiver from 'archiver';
import { v4 as uuidv4 } from 'uuid';

export const createZip = functions.https.onCall(async () => {
const storage = admin.storage();
const bucket = storage.bucket('bucket-name');

// generate random name for a file
const filePath = uuidv4();
const file = bucket.file(filePath);

const outputStreamBuffer = file.createWriteStream({
gzip: true,
contentType: 'application/zip',
});

const archive = archiver('zip', {
gzip: true,
zlib: { level: 9 },
});

archive.on('error', (err) => {
throw err;
});

archive.pipe(outputStreamBuffer);

// use firestore, request data etc. to get file names and their full path in storage
// file path can not start with '/'
const userFilePath = 'user-file-path';
const userFileName = 'user-file-name';

const userFile = await bucket.file(userFilePath).download();
archive.append(userFile[0], {
name: userFileName, // if you want to have directory structure inside zip file, add prefix to name -> /folder/ + userFileName
});

archive.on('finish', async () => {
console.log('uploaded zip', filePath);

// get url to download zip file
await bucket
.file(filePath)
.getSignedUrl({ expires: '03-09-2491', action: 'read' })
.then((signedUrls) => console.log(signedUrls[0]));
});

await archive.finalize();
});

关于node.js - 我可以通过 Firebase Cloud Functions 压缩 Firebase 存储中的文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51563883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com