使用Uploadify直接POST到Amazon S3

时间:2009-11-05 19:02:27

标签: amazon uploadify amazon-s3

有谁能告诉我如何使用Uploadify直接上传到Amazon S3?

我的代码如下:

$('#fileInput').uploadify({
  'fileDataName' : 'file',
  'uploader'  : 'uploadify.swf',
  'script'    : 'http://BUCKET-NAME-GOES-HERE.s3.amazonaws.com/',
  'cancelImg' : 'cancel.png',
  'method'    : 'post',
  'auto'      : true,
  'onError': function (a, b, c, d) {
    alert('error '+d.type+": "+d.info + ' name: ' + c.name + ' size: ' + c.size);
  },
  'scriptData' : {
    'AWSAccessKeyId': "KEY-GOES-HERE",
    'key': "${filename}",
    'acl': "public-read",
    'policy': "POLICY-STRING-GOES-HERE",
    'signature': "SIGNATURE-GOES-HERE",
    'success_action_status': '200'
  }
}); 

我的(未编码的)策略字符串如下所示:

{
  "expiration": "2100-12-01T12:00:00.000Z",
  "conditions": [
    {"acl": "public-read"},
    {"bucket": "BUCKET-NAME-GOES-HERE"},
    {"success_action_status" : 200},
    ["starts-with", "$filename", ""],
    ["starts-with", "$folder", ""],
    ["starts-with", "$key", ""],
    ["content-length-range", 1, 209715200]
  ]
}

使用上面的代码实际上允许我选择一个文件,然后它可以上传(某处),但我的S3存储桶中没有任何内容,并且没有错误返回给JS控制台。

使用常规HTML表单将文件发布到S3存储桶工作正常。

有什么建议吗?

3 个答案:

答案 0 :(得分:3)

来自thread uploadify论坛:

<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript" src="jquery.uploadify.v2.1.0.js"></script>
<link rel="stylesheet" href="uploadify.css" type="text/css" media="screen" />
</head>

<body>
<form>
<input id="fileInput" name="fileInput" type="file" />
</form>
<script type="text/javascript">// <![CDATA[
   $(document).ready(function() {
      $('#fileInput').uploadify({
         'fileDataName' : 'file',
          'uploader'  : 'uploadify.swf',
          'script'    : 'http://UPLOADBUCKET/',
          'cancelImg' : 'cancel.png',
          'auto'      : true,
          'onError' : function(errorObj, q, f, err) { console.log(err); },

          'scriptData' : {
              AWSAccessKeyId: "ACCESS_KEY",
             key: "foo/${filename}",
             acl: "public-read",
             policy: "POLICY STRING"
             signature: "SIGNATURE,
                success_action_status: '200'
             }

          });
    });
// ]]></script>
</body>

</html>

策略字符串的基础如下:

{ "expiration": "2100-12-01T12:00:00.000Z",
"conditions": [
  {"acl": "public-read" },
  {"bucket": "UPLOADBUCKET" },
  {"success_action_status" : '200'},
    ["starts-with", "$filename", "" ],
    ["starts-with", "$folder", "" ],
    ["starts-with", "$key", "foo"],
    ["content-length-range", 1, 209715200]
  ]
}

答案 1 :(得分:1)

我设法通过进度条直接上传到Amazon S3,我在这里有一个工作演示。

如果有人有兴趣,它会支持存储桶中的存储桶和文件夹,以便为此生成wordpress插件。

这是使用swfupload,当它完成时,你可以使用uploadify ill post。

http://www.isimpledesign.co.uk/blog/amazon-s3-direct-multiple-file-upload-progress-bar

答案 2 :(得分:0)

这是将图像保存到s3cloud的java代码。 将此代码添加到uploadscript文件(如uploadify.php)中,您将拥有自己的uploadify脚本文件。

AWSCredentials凭据=新的AWSCredentials(_ACCESS_KEY,                 _SECRET_KEY);

    log.info("oovfilepath : " + oovfilepath);
    log.info("name : " + name);
    S3Service s3Service = new RestS3Service(credentials);
    S3Bucket s3Bucket = s3Service.createBucket(_BUCKET_NAME);
    AccessControlList bucketAcl = s3Service.getBucketAcl(s3Bucket);
    bucketAcl.grantPermission(GroupGrantee.ALL_USERS,
            Permission.PERMISSION_READ);

    InputStream input = new FileInputStream(oovfilepath);

    S3Object s3Object = new S3Object(s3Bucket, name);
    log.info("s3Object:" + s3Object);

    s3Object.setAcl(bucketAcl);
    s3Object.setDataInputStream(input);

    log.info("s3Object:" + s3Object);

    s3Service.putObject(s3Bucket, s3Object);