简体   繁体   English

Firebase 谷歌云功能 - 下载文件 - 未找到

[英]Firebase google cloud functions - Download File - Not Found

I'm trying to do the thumb generator example (generate image thumbnail when one is uploaded).我正在尝试做拇指生成器示例(上传时生成图像缩略图)。 This example was adapter from a previous version of the API.此示例是来自先前版本 API 的适配器。

const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const os = require('os');
const path = require('path');
const sharp = require('sharp');
const fs = require('fs-extra');

exports.generateThumbs = functions.storage
  .object()
  .onFinalize(async (object) => {

    const storage = new Storage();
    const bucket = await storage.bucket(object.name);

    const filePath = object.name;
    const fileName = filePath.split('/').pop();
    const bucketDir = path.dirname(filePath);

    const workingDir = path.join(os.tmpdir(), 'thumbs');
    const tmpFilePath = path.join(workingDir, 'source.png');

    if (fileName.includes('thumb@') || !object.contentType.includes('image')) {
      console.log('exiting function');
      return false;
    }

    // 1. Ensure thumbnail dir exists
    await fs.ensureDir(workingDir);

    // 2. Download Source File
    const file = await bucket.file(filePath);

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

    // 3. Resize the images and define an array of upload promises
    const sizes = [64, 128, 256];

    const uploadPromises = sizes.map(async size => {
      const thumbName = `thumb@${size}_${fileName}`;
      const thumbPath = path.join(workingDir, thumbName);

      // Resize source image
      await sharp(tmpFilePath)
        .resize(size, size)
        .toFile(thumbPath);

      // Upload to GCS
      return bucket.upload(thumbPath, {
        destination: path.join(bucketDir, thumbName)
      });
    });

    // 4. Run the upload operations
    await Promise.all(uploadPromises);

    // 5. Cleanup remove the tmp/thumbs from the filesystem
    return fs.remove(workingDir);
  });

But I get the following error:但我收到以下错误:

Error: Not Found
    at new ApiError (/srv/node_modules/@google-cloud/common/build/src/util.js:58:28)
    at Util.parseHttpRespMessage (/srv/node_modules/@google-cloud/common/build/src/util.js:159:41)
    at Util.handleResp (/srv/node_modules/@google-cloud/common/build/src/util.js:136:74)
    at Duplexify.requestStream.on.on.res (/srv/node_modules/@google-cloud/storage/build/src/file.js:392: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:194:19)
    at Request.<anonymous> (/srv/node_modules/retry-request/index.js:149:11)

at bucket.file(...).download()在bucket.file(...).download()

API 2.XX introduced some changes and I can't seem to make this work. API 2.XX 引入了一些更改,我似乎无法完成这项工作。 Can anyone give me a hand?谁能帮我一把? Thank you.谢谢你。

Got it working with the following code:得到它与以下代码的工作:

const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const os = require('os');
const path = require('path');
const sharp = require('sharp');
const fs = require('fs-extra');

exports.generateThumbs = functions.storage
  .object()
  .onFinalize(async (object) => {
    const storage = new Storage();
    const bucket = storage.bucket(object.bucket);
    const filePath = object.name;
    const fileName = filePath.split('/').pop();
    const bucketDir = path.dirname(filePath);

    const workingDir = path.join(os.tmpdir(), 'thumbs');
    const tmpFilePath = path.join(workingDir, 'source.png');

    if (fileName.includes('thumb@') || !object.contentType.includes('image')) {
      console.log('exiting function');
      return false;
    }

    // 1. Ensure thumbnail dir exists
    await fs.ensureDir(workingDir);

    // 2. Download Source File
    await  bucket.file(filePath).download({
      destination: tmpFilePath
    });

    // 3. Resize the images and define an array of upload promises
    const sizes = [64, 128, 256];

    const uploadPromises = sizes.map(async size => {
      const thumbName = `thumb@${size}_${fileName}`;
      const thumbPath = path.join(workingDir, thumbName);

      // Resize source image
      await sharp(tmpFilePath)
        .resize(size, size)
        .toFile(thumbPath);

      // Upload to GCS
      return bucket.upload(thumbPath, {
        destination: path.join(bucketDir, thumbName)
      });
    });

    // 4. Run the upload operations
    await Promise.all(uploadPromises);

    // 5. Cleanup remove the tmp/thumbs from the filesystem
    return fs.remove(workingDir);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从使用 Cloud Functions 上传的文件中获取下载 URL for Firebase - Get Download URL from file uploaded with Cloud Functions for Firebase 使用下载 url 和 Cloud Functions 从 Firebase 存储中删除文件 - Delete a file from firebase storage using download url with Cloud Functions 使用Google Firebase云功能下载或查看文件 - Download or view file using google firebase cloud function Firebase的云功能图像下载功能错误 - Cloud Functions for Firebase Image download function Error 使用 firebase 函数重定向到 URL - 谷歌云函数 - Redirecting to URL with firebase functions - google cloud functions Firebase 云功能 + 托管 - 404 Not Found - Firebase Cloud Functions + Hosting - 404 Not Found 通过 Firebase 函数下载文件到 Firebase 存储 - Download file to Firebase Storage via Firebase Functions Firebase云功能:如何从FTP帐户检索文件并将其下载到浏览器中 - Firebase cloud functions: how to retrieve a file from an FTP account and download it in the browser 从Google云端存储中获取更短的文件网址(使用Firebase云功能) - Get shorter file URL from Google Cloud Storage (with Firebase Cloud Functions) 从Firebase提取数据-Google云功能 - Fetching data from Firebase - Google Cloud Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM