'use strict';
var https = require('http');
var aws = require('aws-sdk');
var fs = require('fs');
aws.config.update({
accessKeyId: 'id',
secretAccessKey: 'key',
region: 'ap-northeast-1'
});
var s3bucket = new aws.S3();
var bucketName = "bukcetName"
exports.handler = (event, context, callback) => {
https.get("URL.jpg", function(res, body){
body = new Buffer(res.body, 'binary');
var params = {
Bucket:bucketName,
Key: "testLambda",
Body: body,
ACL: 'public-read'
};
s3bucket.upload(params, function(err, data) {
context.done(null, 'Finished UploadObjectOnS3');
});
});
};
APIGateway ---> lambda
我想从lambda中的URL下载 那个内容放了S3。
我无法从URL获取二进制数据 但我可以将任何数据放入S3中的任何目录 谢谢你的帮助
答案 0 :(得分:4)
我对您的代码进行了一些更改,以使其更好地工作
- 你不需要'fs'
- 正确的方法是'putObject'而不是'upload'
谢谢!
'use strict';
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
var http = require('http');
aws.config.update({
accessKeyId: 'id',
secretAccessKey: 'key',
region: 'ap-northeast-1'
});
var s3bucket = new AWS.S3();
var bucketName = "bukcetName";
exports.handler = (event, context, callback) => {
http.get("URL.jpg", function(res){
var imageData = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imageData += chunk;
});
res.on('end', function(){
var params = {
Bucket: bucketName,
Key: "testLambda",
Body: new Buffer(imageData, 'binary'),
ACL: 'public-read'
};
s3.putObject(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
context.done(null, 'Finished UploadObjectOnS3');
});
});
});
};