I am trying to write a lambda script that can pull an image from a site and store it in S3. The problem I'm having is what kind of object to pass as the Body attribute into the S3.putObject
method. In the documentation here it says it should be either new Buffer('...') || 'STRING_VALUE' || streamObject
, but I'm not sure how to convert the https response into one of those. Here is what I've tried:
var AWS = require('aws-sdk');
var https = require('https');
var Readable = require('stream').Readable;
var s3 = new AWS.S3();
var fs = require('fs');
var url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1d/AmazonWebservices_Logo.svg/500px-AmazonWebservices_Logo.svg.png';
exports.handler = function(event, context) {
https.get(url, function(response) {
var params = {
Bucket: 'example',
Key: 'aws-logo.png',
Body: response // fs.createReadStream(response); doesn't work, arg should be a path to a file...
// just putting response errors out with "Cannot determine length of [object Object]"
};
s3.putObject(params, function(err, data) {
if (err) {
console.error(err, err.stack);
} else {
console.log(data);
}
});
});
};
As indicated in the comments, Lambda allows to save files in /tmp
. But you don't really need it...
response
does not contain the content of the file, but the http response (with its status code and headers).
You could try something like this:
var AWS = require('aws-sdk');
var https = require('https');
var s3 = new AWS.S3();
var url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1d/AmazonWebservices_Logo.svg/500px-AmazonWebservices_Logo.svg.png';
exports.handler = function(event, context) {
https.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
// Agregates chunks
body += chunk;
});
res.on('end', function() {
// Once you received all chunks, send to S3
var params = {
Bucket: 'example',
Key: 'aws-logo.png',
Body: body
};
s3.putObject(params, function(err, data) {
if (err) {
console.error(err, err.stack);
} else {
console.log(data);
}
});
});
});
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With