s3桶上传不起作用

时间:2017-07-30 16:45:37

标签: node.js aws-lambda

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的建议 上传根本不起作用。你能帮忙吗? THX。

1 个答案:

答案 0 :(得分:0)

您正在尝试在下载的同时上传...

您需要在downloader.on('end',方法

中调用上传