Amazon s3 Lambda文件写入S3
在过去的六个月里,我一直在下载NASA的APOD,并使用Lambda函数保存到S3存储桶中。截至2016年12月23日,所有设备均按预期工作。现在,当我检查我的存储桶时,图像在那里,但大小为0字节。我在下面包含了我的代码。有人知道有没有变化吗?谢谢Amazon s3 Lambda文件写入S3,amazon-s3,lambda,Amazon S3,Lambda,在过去的六个月里,我一直在下载NASA的APOD,并使用Lambda函数保存到S3存储桶中。截至2016年12月23日,所有设备均按预期工作。现在,当我检查我的存储桶时,图像在那里,但大小为0字节。我在下面包含了我的代码。有人知道有没有变化吗?谢谢 var AWS = require("aws-sdk"); var https = require('https'); var http = require('http'); var fs = require('fs'); // Incoming
var AWS = require("aws-sdk");
var https = require('https');
var http = require('http');
var fs = require('fs');
// Incoming Handler
// <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
exports.handler = (event, context, callback) => {
GetAPOD();
};
// <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
// Functions
// <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
function GetAPOD() {
var nasa_api_key = 'MY KEY GOES HERE'
, nasa_api_path = '/planetary/apod?api_key=' + nasa_api_key;
var options = {
host: 'api.nasa.gov',
port: 443,
path: nasa_api_path,
method: 'GET'
};
// Connect to the NASA API and get the APOD.
var req = https.request(options, function (res) {
console.log('Open connection to NASA.');
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_url = responseObject['url']
, image_hdurl = responseObject['hdurl']
, media_type = responseObject['media_type'];
if (media_type == 'image') {
var image_name = image_date + '.jpg';
var s3 = new AWS.S3();
var s3Bucket = new AWS.S3( { params: {Bucket: 'nasa-apod'} } );
// Check to see if the image already exists in the S3 bucket.
// If not we will upload the image to S3.
var head_data = {Key: image_name};
s3Bucket.headObject(head_data, function(err, output_head_data) {
if (output_head_data) {
console.log("Image exists on S3.");
}
else {
console.log("Image does not exists on S3.");
// Image has not been uploaded to S3, open a stream and download the image to the /tmp folder.
var file = fs.createWriteStream("/tmp/" + image_name);
var request = http.get(image_url, function(response) {
console.log("Opening file stream.");
// Pipe the data into the file stream and save to disk.
response.pipe(file);
response.on('end', function () {
// File is written to disk, we are going to check that it exists.
var fileName = "/tmp/" + image_name;
fs.exists(fileName, function(exists) {
if (exists) {
console.log("File exits in /tmp folder.");
// Get the stats for the image, will need this for the ContentLength
fs.stat(fileName, function(error, stats) {
if (error) {
console.log("Stat Error: " + error);
}
else {
console.log("Opening file stream.");
var image_stream = fs.createReadStream(fileName);
// Begin the upload process to S3.
var param_data = {Key: image_name, Body: image_stream, ContentType: "image/jpeg", ContentLength: stats.size, ACL: "public-read"};
s3Bucket.putObject(param_data, function(err, output_data) {
if (err) {
console.log('Error uploading data to S3: ' + err);
}
else {
console.log('Image successfully uploaded.');
}
});
}
});
}
else {
console.log('File does not exist in the /tmp folder.');
}
});
});
});
}
});
}
else {
console.log("Media Type: " + media_type);
}
});
});
req.on('error', function (e) {
console.error('HTTP error: ' + e.message);
});
req.end();
}
// <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
var AWS=require(“AWS sdk”);
var https=require('https');
var http=require('http');
var fs=需要('fs');
//传入处理程序
//
exports.handler=(事件、上下文、回调)=>{
GetAPOD();
};
//
//功能
//
函数GetAPOD(){
var nasa_api_key='我的钥匙在这里'
,nasa_api_路径='/行星/apod?api_键='+nasa_api_键;
变量选项={
主持人:“api.nasa.gov”,
港口:443,
路径:nasa_api_路径,
方法:“获取”
};
//连接NASA API并获取APOD。
var req=https.request(选项、函数(res){
console.log(“打开与NASA的连接”);
res.setEncoding('utf-8');
var responseString='';
res.on('数据'),函数(数据){
responseString=数据;
});
res.on('end',function(){
log('API响应:'+响应字符串);
var responseObject=JSON.parse(responseString)
,image_date=responseObject['date']
,image_url=responseObject['url']
,image_hdurl=responseObject['hdurl']
,media_type=responseObject['media_type'];
如果(媒体类型==“图像”){
var image_name=image_date+'.jpg';
var s3=新的AWS.s3();
var s3Bucket=new AWS.S3({params:{Bucket:'nasa apod'}});
//检查S3存储桶中是否已经存在该映像。
//如果没有,我们将上传图像到S3。
var head_data={Key:image_name};
s3Bucket.headObject(head\u数据,函数(err,output\u head\u数据){
if(输出头数据){
log(“映像存在于S3上”);
}
否则{
log(“S3上不存在映像”);
//图像尚未上载到S3,请打开一个流并将图像下载到/tmp文件夹。
var file=fs.createWriteStream(“/tmp/”+图像名称);
var request=http.get(图像\ url,函数(响应){
log(“打开文件流”);
//将数据导入文件流并保存到磁盘。
管道(文件);
response.on('end',function(){
//文件已写入磁盘,我们将检查它是否存在。
var fileName=“/tmp/”+图像名称;
存在(文件名,函数(存在){
如果(存在){
log(“文件在/tmp文件夹中退出”);
//获取图像的统计信息,将需要这个作为ContentLength
fs.stat(文件名,函数(错误,统计信息){
如果(错误){
日志(“统计错误:+错误”);
}
否则{
log(“打开文件流”);
var image_stream=fs.createReadStream(文件名);
//开始上传到S3的过程。
var param_data={Key:image_name,Body:image_stream,ContentType:“image/jpeg”,ContentLength:stats.size,ACL:“public read”};
s3Bucket.putObject(参数数据,函数(错误,输出数据){
如果(错误){
log('将数据上载到S3时出错:'+err);
}
否则{
log('图像已成功上载');
}
});
}
});
}
否则{
log(“/tmp文件夹中不存在文件”);
}
});
});
});
}
});
}
否则{
console.log(“媒体类型:+媒体类型”);
}
});
});
请求开启('错误',功能(e){
console.error('HTTP错误:'+e.message);
});
请求结束();
}
//
发现NASA APOD API现在对图像使用https而不是http。我不得不调整我的代码以使用https作为图像路径