【问题标题】:Get image downloadURL in Storage and save it in Firestore在 Storage 中获取图片 downloadURL 并将其保存在 Firestore 中
【发布时间】:2019-09-01 02:31:15
【问题描述】:

我正在从上传到 Firebase 存储的每张图片中创建一个缩略图。如何获取新创建的缩略图的 downloadURL 并将其保存在 Firestore 中?

感谢您的帮助。

Firestore 中的路径:

groups --> groupId --> smallImage

我用于创建缩略图的示例代码:

const functions = require("firebase-functions");
const { Storage } = require('@google-cloud/storage');
const projectId = 'xx'
let gcs = new Storage ({
  projectId
});
const spawn = require("child-process-promise").spawn;
const path = require("path");
const os = require("os");
const fs = require('fs');
// [END import]
// [START generateThumbnail]
// [START generateThumbnailTrigger]
exports.generateThumbnail = functions.storage.object().onFinalize((object) => {
// [END generateThumbnailTrigger]
  // [START eventAttributes]
  const fileBucket = object.bucket; // The Storage bucket that contains the file.
  const filePath = object.name; // File path in the bucket.
  const contentType = object.contentType; // File content type.
  const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.

  const fileName = path.basename(filePath);

  if (fileName.endsWith('_small')) {
    console.log('Already a Thumbnail.');
    return null;
  }
  // [START thumbnailGeneration]
  // Download file from bucket.
  const bucket = gcs.bucket(fileBucket);
  const tempFilePath = path.join(os.tmpdir(), fileName);
  const metadata = {
    contentType: contentType,
  };
  return bucket.file(filePath).download({
    destination: tempFilePath,
  }).then(() => {
    console.log('Image downloaded locally to', tempFilePath);
    // Generate a thumbnail using ImageMagick.
    return spawn('convert', [tempFilePath, '-thumbnail', '200x200>', tempFilePath]);
  }).then(() => {
    console.log('Thumbnail created at', tempFilePath);
    const thumbFileName = path.basename(filePath) + '_small';
    const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
    // Uploading the thumbnail.
    return bucket.upload(tempFilePath, {
      destination: thumbFilePath,
      metadata: metadata,
    });
    // Once the thumbnail has been uploaded delete the local file to free up disk space.
  }).then(() => fs.unlinkSync(tempFilePath));
  // [END thumbnailGeneration]
});

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions firebase-storage


    【解决方案1】:

    是的,您的 bucket.upload() 函数可以返回您需要的 File 对象:

    const options = {destination: thumbFilePath, metadata: metadata};
    
    bucket.upload(tempFilePath, options, function(err, file, apiResp) {
    
        // `file` is an instance of a File object that refers to your new file.
        const request = require('request');
    
        const config = {
            action: 'read',
            expires: '03-17-2025'
        };
    
        file.getSignedUrl(config, function(err, url) {
            if (err) {
                console.error(err);
                return;
            }
    
            // The file is now available to read from this URL.
            request(url, function(err, resp) {
                // resp.statusCode = 200
            });
        }); 
    
    });
    

    见: https://cloud.google.com/nodejs/docs/reference/storage/2.3.x/File#getSignedUrl

    【讨论】:

    • 我还建议您查看 Firebase Storage,从中您可以立即获得永久下载 URL
    • 感谢您的回复!如何在 Firestore 中集成保存?
    • 其实很相似,请仔细阅读:firebase.google.com/docs/storage/web/start
    • 有什么方法可以将Connection集成到Cloud功能中?在关于存储/网络的说明中,我找不到任何有用的东西:-/
    • 你不需要做任何特别的事情来在云功能中使用它,只需对其进行编程并上传并调用它。
    猜你喜欢
    • 2022-01-07
    • 2021-02-06
    • 2021-05-17
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多