将图像从S3存储桶下载到Lambda临时文件夹(Node.js) [英] Download image from S3 bucket to Lambda temp folder (Node.js)

查看:178
本文介绍了将图像从S3存储桶下载到Lambda临时文件夹(Node.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们.

我有一个简单的问题:如何将图像从S3存储桶下载到Lambda函数的temp文件夹进行处理?基本上,我需要将其附加到电子邮件中(在本地测试时可以做到).

I have a simple question: How do I download an image from a S3 bucket to Lambda function temp folder for processing? Basically, I need to attach it to an email (this I can do when testing locally).

我尝试过:

s3.download_file(bucket, key, '/tmp/image.png')

以及(不确定哪些参数可以帮助我完成工作):

as well as (not sure which parameters will help me get the job done):

s3.getObject(params, (err, data) => {
    if (err) {
        console.log(err);
        const message = `Error getting object ${key} from bucket ${bucket}.`;
        console.log(message);
        callback(message);
    } else {

        console.log('CONTENT TYPE:', data.ContentType);
        callback(null, data.ContentType);
    }
});

就像我说的那样,这是一个简单的问题,出于某种原因,我找不到解决方案.

Like I said, simple question, which for some reason I can't find a solution for.

谢谢!

推荐答案

您可以使用aws s3 api获取图像,然后使用fs将其写入tmp文件夹.

You can get the image using the aws s3 api, then write it to the tmp folder using fs.

var params = {   Bucket: "BUCKET_NAME",   Key: "OBJECT_KEY" };  

s3.getObject(params, function(err, data){   if (err) {
    console.error(err.code, "-", err.message);
    return callback(err);   }

  fs.writeFile('/tmp/filename', data.Body, function(err){
    if(err)
      console.log(err.code, "-", err.message);

    return callback(err);   
  }); 
});

出于好奇,为什么您需要编写文件以附加它?将文件写入磁盘似乎有点多余,以便您随后可以从磁盘读取文件

Out of curiousity, why do you need to write the file in order to attach it? It seems kind of redundant to write the file to disk so that you can then read it from disk

这篇关于将图像从S3存储桶下载到Lambda临时文件夹(Node.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆