firebase 云功能:从云存储压缩多个文档

ddarikpa  于 2022-11-17  发布在  其他
关注(0)|答案(2)|浏览(126)

我已经搜索了很多关于堆栈溢出的问题,但没有找到一个合适的答案,从中我可以得出我需要的答案:
我想从Google Cloud Storage/Firebase Storage中的一个文件夹中使用云功能压缩多个文件。
我已经找到了从 * 本地文件系统 * 压缩文档的解决方案,但无法导出如何在云云存储功能中执行此操作。

wkyowqbh

wkyowqbh1#

Google云存储支持解压缩形式的转码,但不支持压缩形式的转码。然而,在云存储中,任何用户都可以存储gzip压缩文件。
要使用Cloud Functions从云存储压缩多个文档,可以使用gcs.bucket.file将文件从云存储下载到函数示例(filePath). download,压缩文件,然后重新上传文件到云存储。在这里,您可以找到下载、转换和上传文件。您可以在此StackOverflowthread中找到压缩多个文件的示例。此document解释了如何使用Console、Gsutil、代码示例或REST API。

hgb9j2n6

hgb9j2n62#

有点晚了,但我有同样的问题要解决。
以下Firebase函数:
1.以1 GB / 120秒的超时运行(作为良好的衡量标准)
1.由WRITE调用触发(仅当调用很少时才执行此操作!)
1.忽略除background_thumbnail/之外的所有路径
1.创建一个随机工作目录,然后将其删除
1.从Firebase存储下载图像
1.将这些图像压缩到文件夹中:background_thumbnail/<IMAGE>
1.将创建的ZIP文件上传至Firebase存储
1.为Firebase存储中的ZIP文件创建签名URL
1.将签名的URL存储在Firestore中。
代码可能可以改进,使其更优雅,但它工作(目前)。

const {v4: uuidv4} = require("uuid"); // for random working dir
const JSZip = require("jszip");

exports.generateThumbnailZip = functions
  .runWith({memory: "1GB", timeoutSeconds: 120})
  .region("europe-west3")
  .storage.object()
  .onFinalize(async (object) => {

  // background_thumbnail/ is the watched folder

  if (!object.name.startsWith("background_thumbnail/")) {
    return functions.logger.log(`Aborting, got: ${object.name}.`);
  }

  const jszip = new JSZip();

  const bucket = admin.storage().bucket();
  const fileDir = path.dirname(object.name);
  const workingDir = path.join(os.tmpdir(), uuidv4()); 

  const localZipPath = path.join(workingDir, `${fileDir}.zip`);
  const remoteZipPath = `${fileDir}.zip`;

  await mkdirp(workingDir);

  // -------------------------------------------------------------------
  // DOWNLOAD and ZIP
  // -------------------------------------------------------------------

  const [files] = await bucket.getFiles({prefix: `${fileDir}/`});

  for (let index = 0; index < files.length; index++) {
    const file = files[index];
    const name = path.basename(file.name);
    const tempFileName = path.join(workingDir, name);

    functions.logger.log("Downloading tmp file", tempFileName);

    await file.download({destination: tempFileName});

    jszip.folder(fileDir).file(name, fs.readFileSync(tempFileName));
  }

  const content = await jszip.generateAsync({
    type: "nodebuffer",
    compression: "DEFLATE",
    compressionOptions: { level: 9 }
  });

  functions.logger.log("Saving zip file", localZipPath);

  fs.writeFileSync(localZipPath, content);

  // -------------------------------------------------------------------
  // UPLOAD ZIP
  // -------------------------------------------------------------------

  functions.logger.log("Uploading zip to storage at", remoteZipPath);

  const uploadResponse = await bucket
    .upload(path.resolve(localZipPath), {destination: remoteZipPath});

  // -------------------------------------------------------------------
  // GET SIGNED URL FOR ZIP AND STORE IT IN DB
  // -------------------------------------------------------------------

  functions.logger.log("Getting signed URLs.");

  const signedResult = await uploadResponse[0].getSignedUrl({
    action: "read",
    expires: "03-01-2500",
  });

  const signedUrl = signedResult[0];

  functions.logger.log("Storing signed URL in db", signedUrl);

  // Stores the signed URL under "zips/<WATCHED DIR>.signedUrl"
  await db.collection("zips").doc(fileDir).set({
    signedUrl: signedUrl,
  }, {merge: true});

  // -------------------------------------------------------------------
  // CLEAN UP
  // -------------------------------------------------------------------

  functions.logger.log("Unlinking working dir", workingDir);

  fs.rmSync(workingDir, {recursive: true, force: true});

  functions.logger.log("DONE");

  return null;
});

相关问题