简体   繁体   English

AWS Lambda获取图像并上传到S3

[英]AWS Lambda Get Image and Upload to S3

I am working in a AWS Lambda function. 我正在使用AWS Lambda函数。 I am successfully making an API call to the NASA APOD and getting back the values. 我已经成功地对NASA APOD进行了API调用,并获取了这些值。 I want to take the url for the image and download that image and then upload into S3. 我要获取图像的URL,然后下载该图像,然后上传到S3。 I am getting an error when I try to access the "test.jpg" image, "Error: EACCES: permission denied, open 'test.jpg'". 尝试访问“ test.jpg”图像时出现错误:“错误:EACCES:权限被拒绝,请打开'test.jpg'”。 If I move the S3bucket.putObject outside the http.request, I get data is equal to null. 如果我将S3bucket.putObject移到http.request之外,我得到的数据等于null。 I know I am missing something simple. 我知道我缺少一些简单的东西。 Thought? 思想?

function GetAPOD(intent, session, callback) {
    var nasa_api_key = 'demo-key'
    ,   nasa_api_path = '/planetary/apod?api_key=' + nasa_api_key;

    var options = {
        host: 'api.nasa.gov',
        port: 443,
        path: nasa_api_path,
        method: 'GET'
    };

    var req = https.request(options, function (res) {
        res.setEncoding('utf-8');

        var responseString = '';
        res.on('data', function (data) {
            responseString += data;
        });

        res.on('end', function () {
            console.log('API Response: ' + responseString);

            var responseObject = JSON.parse(responseString)
            ,   image_date = responseObject['date']
            ,   image_title = responseObject['title']
            ,   image_url = responseObject['url']
            ,   image_hdurl = responseObject['hdurl']
            ,   image_desc = responseObject['explanation'];

            var s3Bucket = new AWS.S3( { params: {Bucket: 'nasa-apod'} } );

            var fs = require('fs');

            var file = fs.createWriteStream("test.jpg");
            var request = http.get(image_url, function(response) {
                response.pipe(file);

                var data = {Key: "test.jpg", Body: file};
                s3Bucket.putObject(data, function(err, data) {
                    if (err) {
                        console.log('Error uploading data: ', data); 
                    }
                    else {
                        console.log('succesfully uploaded the image!');
                    }
                });
            });

        });
    });

    req.on('error', function (e) {
        console.error('HTTP error: ' + e.message);
    });

    //req.write();
    req.end();
}

You need to be writing the file to /tmp . 您需要将文件写入/tmp That's the only directory in the Lambda environment that you will have write access to. 那是Lambda环境中您唯一具有写访问权的目录。

I got it!! 我知道了!! Thank you Mark B for the help. 谢谢Mark B的帮助。 I was able to get the data from the stream without saving it locally and then writing to the bucket. 我能够从流中获取数据,而无需将其保存到本地,然后再写入存储桶。 I did have to change my IAM role to allow the putObject for S3. 我确实必须更改IAM角色以允许putObject用于S3。

function GetAPOD(intent, session, callback) {
    var nasa_api_key = 'demo-key'
    ,   nasa_api_path = '/planetary/apod?api_key=' + nasa_api_key;

    var options = {
        host: 'api.nasa.gov',
        port: 443,
        path: nasa_api_path,
        method: 'GET'
    };

    var req = https.request(options, function (res) {
        res.setEncoding('utf-8');

        var responseString = '';
        res.on('data', function (data) {
            responseString += data;
        });

        res.on('end', function () {
            // console.log('API Response: ' + responseString);

            var responseObject = JSON.parse(responseString)
            ,   image_date = responseObject['date']
            ,   image_title = responseObject['title']
            ,   image_url = responseObject['url']
            ,   image_hdurl = responseObject['hdurl']
            ,   image_desc = responseObject['explanation'];

            var image_name = image_date + '.jpg';

            var s3 = new AWS.S3();
            var s3Bucket = new AWS.S3( { params: {Bucket: 'nasa-apod'} } );

            var request = http.get(image_url, function(response) {
                var image_stream = null;
                response.on('data', function (data) {
                    image_stream = data;
                });

                response.on('end', function () {
                    var param_data = {Key: image_name, Body: image_stream, ContentType: "image/jpeg", ContentLength: response.headers['content-length']};
                    s3Bucket.putObject(param_data, function(err, output_data) {
                        if (err) {
                            console.log('Error uploading data to S3: ' + err); 
                        }
                    });
                });
            });
            request.end();

        });
    });

    req.on('error', function (e) {
        console.error('HTTP error: ' + e.message);
    });

    req.end();
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM