繁体   English   中英

s3存储桶上传不起作用

[英]s3 bucket upload not working

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的建议。上传完全无效。 你能帮忙吗? 谢谢。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM