简体   繁体   English

在存储中获取图片下载网址并将其保存在Firestore中

[英]Get image downloadURL in Storage and save it in Firestore

I am creating a thumbnail from each image I upload to my Firebase Storage. 我正在从上传到Firebase存储的每个图像创建缩略图。 How can I get the downloadURL of the newly created thumbnail image and save it in Firestore? 如何获取新创建的缩略图的downloadURL并将其保存在Firestore中?

Thanks for your help. 谢谢你的帮助。

Path in Firestore: Firestore中的路径:

groups --> groupId --> smallImage

Sample code I use for thumbnail creation: 我用于创建缩略图的示例代码:

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]
});

Yes, your bucket.upload() function can return a File object that you will need: 是的,您的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
        });
    }); 

});

See: https://cloud.google.com/nodejs/docs/reference/storage/2.3.x/File#getSignedUrl 请参阅: https : //cloud.google.com/nodejs/docs/reference/storage/2.3.x/File#getSignedUrl

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

相关问题 我想将云 function 中的图像保存到 Firestore 并获取 URL - I want to save the image from the cloud function to the firestore and get the URL 如何使用firebase admin sdk获取downloadUrl firebase存储并使用multer在nodejs中放入img html的标签? - How to get downloadUrl firebase storage with firebase admin sdk and put in img html's tag in nodejs with multer? 将图像从URL直接保存到Azure存储 - save an image from URL to Azure storage directly 使用 Firebase 获取 downloadURL 不断抛出错误“未定义 XMLHttpRequest” - Get downloadURL keeps throwing error 'XMLHttpRequest is not defined' using Firebase node js使用multer上传图像并保存到firebase存储 - node js uploading image with multer and save to firebase storage firebase 存储上上传的缺少图像(使用云 function 更新 Firestore DB) - Missing image uploaded on firebase storage (Using cloud function to update firestore DB) 从本地存储保存并获取express.js令牌 - Save and get express.js token from local storage 如何将图像 url 保存到 mongodb 并取回 - How to save image url to mongodb and get it back 尝试从 axios 获取请求中保存图像 - Trying to save an image from an axios get request 将jwt保存到本地存储 - Save jwt to local storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM