2017-01-06 4 views
0

In den letzten sechs Monaten habe ich die NASA-APOD heruntergeladen und mit einer Lambda-Funktion in einem S3-Bucket gespeichert. Bis zum 23.12.2016 funktionierte alles wie erwartet. Jetzt wenn ich meinen Eimer überprüfe, sind die Bilder dort aber Größe 0 Bytes. Ich habe meinen Code unten eingefügt. Weiß jemand, ob es eine Änderung gegeben hat? Vielen Dank!Lambda-Datei Schreiben an S3

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(); 
    } 
// <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> 

Antwort

0

herausgefunden, dass NASA APOD API jetzt HTTPS und nicht http für Bilder verwendet. Ich musste meinen Code anpassen, um https für den Bildpfad zu verwenden.