【问题标题】:how to get the download url for image resized with node js in google cloud functions?如何在谷歌云功能中获取使用节点 js 调整大小的图像的下载 url?
【发布时间】:2018-09-21 11:47:13
【问题描述】:

我是节点 js 和谷歌云功能的新手。我能够调整图像大小以生成缩略图。我想不通的是如何获取新生成的缩略图的下载网址。这是代码

   exports.generateThumbnail =  functions.storage.object('{pushId}/ProductImages').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 resourceState = object.resourceState; // The resourceState is  'exists' or 'not_exists' (for file/folder deletions).
 const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.

console.log("this is the object ",object);
  // [END eventAttributes]

// [START stopConditions]

 if (!contentType.startsWith('image/')) {
  console.log('This is not an image.');
  return null;
  }


 const fileName = path.basename(filePath);

 if (fileName.startsWith('thumb_')) {

 return null;
 }
  // [END stopConditions]

  // [START thumbnailGeneration]
 // Download file from bucket.
  const bucket = gcs.bucket(fileBucket);
  const tempFilePath = path.join(os.tmpdir(), fileName);
 // const tempFilePath1 = path.join(os.tmpdir(), fileName+"1");

 // var storageRef = firebase.storage.ref("folderName/file.jpg");
    const storageRef = event.data.ref.parent;
 // return storageRef.getDownloadURL().then(function(url) {

 // });

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(() => {


const thumbFileName = `thumb_${fileName}`;
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));
});

在javascript中,可以使用此代码获取上传到firebase存储的图像的下载网址

   storageRef.put(file, metadata).then(function(snapshot) {

            var url = snapshot.downloadURL;
    })

如何在node js中获取调整后图片的下载地址?

【问题讨论】:

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


    【解决方案1】:

    您需要使用getSignedUrl() 来生成公共网址。在generate-thumbnail example 的函数样本存储库中有一个示例。缩写:

    const thumbFile = bucket.file(thumbFilePath);
    const config = {
      action: 'read',
      expires: '03-01-2500',
    };
    thumbFile.getSignedUrl(config);  // returns a promise with results
    

    您必须注意 README 并使用服务帐户初始化 admin SDK 才能使用此方法。 Cloud Functions 的默认初始化将不起作用。

    【讨论】:

      猜你喜欢
      • 2022-11-21
      • 2020-03-23
      • 2019-01-03
      • 2020-04-05
      • 1970-01-01
      • 2020-08-28
      • 2019-05-17
      相关资源
      最近更新 更多