2017-07-30 73 views
0
var async = require('async'); 
var AWS = require('aws-sdk'); 


var util = require('util'); 
var im = require('imagemagick'); 
var fs = require('fs'); 

// constants 
var MAX_WIDTH = 100; 
var MAX_HEIGHT = 100; 

var s3 = require('s3'); 

var client = s3.createClient({ 
    maxAsyncS3: 20,  // this is the default 
    s3RetryCount: 3, // this is the default 
    s3RetryDelay: 1000, // this is the default 
    multipartUploadThreshold: 20971520, // this is the default (20 MB) 
    multipartUploadSize: 15728640, // this is the default (15 MB) 
    s3Options: { 
    accessKeyId: "xx", 
    secretAccessKey: "xx", 

    }, 
}); 


exports.handler = function(event, context, callback) { 
    // Read options from the event. 
    console.log("Reading options from event:\n", util.inspect(event, {depth: 5})); 
    var srcBucket = event.Records[0].s3.bucket.name; 
    // Object key may have spaces or unicode non-ASCII characters. 
    var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")); 
    var dstBucket = srcBucket + "resized"; 
    var dstKey = "resized-" + srcKey; 

    // Sanity check: validate that source and destination are different buckets. 
    if (srcBucket == dstBucket) { 
     callback("Source and destination buckets are the same."); 
     return; 
    } 

    // Infer the image type. 
    var typeMatch = srcKey.match(/\.([^.]*)$/); 
    if (!typeMatch) { 
     callback("Could not determine the image type."); 
     return; 
    } 
    var imageType = typeMatch[1]; 
    if (imageType != "jpg" && imageType != "png") { 
     callback('Unsupported image type: ${imageType}'); 
     return; 
    } 

    // Download the image from S3, transform, and upload to a different S3 bucket. 
    async.waterfall([ 
     function download(next) { 

      var params = { 
       localFile: "/tmp/"+srcKey, 

       s3Params: { 
       Bucket: srcBucket, 
       Key: srcKey, 

       }, 
      }; 
      var downloader = client.downloadFile(params); 
      downloader.on('error', function(err) { 
       console.error("unable to download:", err.stack); 
      }); 
      downloader.on('progress', function() { 
       console.log("progress", downloader.progressAmount, downloader.progressTotal); 
      }); 
      downloader.on('end', function() { 
       console.log("done downloading"); 
      }); 

      //upload a file 
      var uploadparams = { 
       localFile: "/tmp/"+srcKey, 

       s3Params: { 
       Bucket: dstBucket, 
       Key: dstKey, 
       }, 
      }; 
      var uploader = client.uploadFile(uploadparams); 
      uploader.on('error', function(err) { 
       console.error("unable to upload:", err.stack); 
      }); 
      uploader.on('progress', function() { 
       console.log("progress", uploader.progressMd5Amount, 
         uploader.progressAmount, uploader.progressTotal); 
      }); 
      uploader.on('end', function() { 
       console.log("done uploading"); 
      }); 
     } 

    ], function (err) { 
      if (err) { 
       console.error(
        'Unable to resize ' + srcBucket + '/' + srcKey + 
        ' and upload to ' + destBucket + '/' + destKey + 
        ' due to an error: ' + err 
       ); 
      } else { 
       console.log(
        'Successfully resized ' + srcBucket + '/' + srcKey + 
        ' and uploaded to ' + destBucket + '/' + destKey 
       ); 
      } 
     } 
    );   
}; 

我試圖從s3桶下載文件並將其上傳到不同的s3存儲桶。我需要在上傳之前對其他轉換進行一些操作。所以,只想嘗試下載並首先上傳。在執行時,它表示完成了下載。但我無法上傳文件。不確定是什麼問題。我遵循https://github.com/andrewrk/node-s3-client/blob/master/README.md 的建議上傳並不奏效。你能幫忙嗎?謝謝。s3桶上傳不起作用

回答

0

正試圖在你下載的同時上傳...

你需要調用內部downloader.on('end',方法

+0

這個答案對我來說確實管用上傳。 Thx UXDart – Chandramohan