直接从Flash 403问题上传到s3

时间:2011-08-08 15:01:48

标签: actionscript-3 amazon-s3

有很多方法可以从Flash上​​传到S3。我正在尝试在Amazon的网站http://aws.amazon.com/code/1092?_encoding=UTF8&jiveRedirect=1

上实现以下示例中的代码

我遇到的一些帖子表明亚马逊的很多字段现在都是Requiered,除非你填写它们,否则你会得到这个可怕的403错误。

我已经尝试了几件事,我希望很快就会有解决方案。我在这里使用了以下的库http://code.google.com/p/as3awss3lib/

这是我的班级处理所有上传

package com.myemma.s3uploader.main.controllers
{
    import jp.classmethod.aws.core.AWSEvent;

    import s3.flash.S3PostOptions;
    import s3.flash.S3PostRequest;

    import utils.PolicyGenerator;

    import com.myemma.s3uploader.main.model.MainDM;
    import com.myemma.s3uploader.settings.Settings;

    import flash.events.DataEvent;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.events.SecurityErrorEvent;
    import flash.external.ExternalInterface;
    import flash.net.FileReference;

    /**
     * @author Matthew Sloan Wallace - http://mattwallace.me
     */
    public class UploadFilesAction extends EventDispatcher
    {
        [Inject]
        public var dm : MainDM;
        private var service : S3PostRequest;

        [Init]
        public function onInit() : void
        {
            if (ExternalInterface.available)
                ExternalInterface.addCallback( "uploadFiles", uploadFiles );
        }

        private function uploadFiles() : void
        {
            if (dm.selectedFiles)
                upload();
        }

        private function upload() : void
        {
            if (dm.selectedFiles.length > 0)
            {
                var fileReference : FileReference = dm.selectedFiles[0];
                // var s3:AWSS3 = new AWSS3(Settings.accessKey, Settings.secret_key);
                // s3.saveObject("mattwallace", fileReference.name, "image/png", fileReference);
                // s3.addEventListener("objectSaved", onObjectSaved);

                var policy : PolicyGenerator = PolicyGenerator.getInstance( Settings.accessKey, Settings.secret_key );
                var s3Options : S3PostOptions = new S3PostOptions();
                s3Options.secure = false;
                s3Options.acl = "private";
                s3Options.contentType = "image/png";
                s3Options.filename = fileReference.name;
                s3Options.success_action_status = "201";
                s3Options.policy = policy.policy;
                s3Options.signature = policy.signature;

                service = new S3PostRequest( Settings.accessKey, Settings.bucket, Settings.secret_key, s3Options );

                service.addEventListener( Event.OPEN, function( event : Event ) : void
                {
                    trace( "Uploading..." );
                    trace( "Upload started: " + fileReference.name );
                } );
                service.addEventListener( ProgressEvent.PROGRESS, function( event : ProgressEvent ) : void
                {
                    trace( Math.floor( event.bytesLoaded / event.bytesTotal * 100 ) );
                } );
                service.addEventListener( IOErrorEvent.IO_ERROR, function( event : IOErrorEvent ) : void
                {
                    trace( "Upload error!" );
                    trace( "An IO error occurred: " + event );
                } );
                service.addEventListener( SecurityErrorEvent.SECURITY_ERROR, function( event : SecurityErrorEvent ) : void
                {
                    trace( "Upload error!" );
                    trace( "A security error occurred: " + event );
                } );
                service.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, function( event : Event ) : void
                {
                    trace( "Upload complete!" );
                    trace( "Upload completed: " + event );
                    dm.selectedFiles.splice( 0, 1 );
                } );

                try
                {
                    service.upload( fileReference );
                }
                catch(e : Error)
                {
                    trace( "Upload error!" );
                    trace( "An error occurred: " + e );
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我和s3合作过,我没有使用特殊的库(可能除了生成策略和签名)。尝试以下内容:

var policyURL = "..."; //something like "http://domain.s3.amazonaws.com/crossdomain.xml"
flash.security.Security.loadPolicyFile(policyURL);
fileReference.addEventListener(Event.SELECT, selectHandler);
fileReference.browse();

function selectHandler(e: Event) {
    fileReference = event.target;

    s3options = new flash.net.URLVariables();
    s3Options.secure = false;
    s3Options.acl = "private";
    s3Options.contentType = "image/png";
    s3Options.filename = fileReference.name;
    s3Options.success_action_status = "201";
    s3Options.policy = policy;
    s3Options.signature = signature;

    uploadURL = new flash.net.URLRequest();
    uploadURL.data = s3Options;
    fileReference.upload(uploadURL, "file", false);
}

答案 1 :(得分:0)

我设法让我的代码工作,难点是正确的政策。我正在使用elctech's library生成s3上传调用。

import com.elctech.*;

public class Uploader{
  private var _options : S3UploadOptions = new S3UploadOptions();

  // ...

  public function uploadPDF(tempFile:File):void{
     _options.FileSize          = tempFile.size.toString();
     _options.FileName          = getFileName(tempFile);
     _options.ContentType       = 'application/pdf';
     _options.key               = certificate['key'] + _options.FileName;

     _options.policy         = certificate['policy'];
     _options.signature      = certificate['signature'];
     _options.bucket         = certificate['bucket'];
     _options.AWSAccessKeyId = certificate['accessKey'];
     _options.acl            = certificate['acl'];
     _options.Expires        = certificate['expiration'];
     _options.Secure         = 'false';
     _options.successactionstatus = certificate['sas'];

     var request:S3UploadRequest = new S3UploadRequest(_options);
     configureUploadListeners(request);

     try {
        request.upload(tempFile);
     } catch(e:Error) {
        trace("An error occurred: " + e);
     }
  }

  private function getFileName(file:FileReference):String {
        var fileName:String = file.name.replace(/^.*(\\|\/)/gi, '').replace(/[^A-Za-z0-9\.\-]/gi, '_');
        return fileName;
  }
}

我通过我们的API从我们的主要Rails应用程序获得了amazon s3证书,这是政策生成的样子:

def certificate
  bucket = ENV['S3_BUCKET']
  access_key = ENV['S3_KEY']
  secret = ENV['S3_SECRET']
  key = "temp/"
  expiration = 10.hours.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
  max_filesize = 500.megabytes
  acl = 'public-read'
  sas = '201'
  policy = Base64.encode64(
    "{'expiration': '#{expiration}',
      'conditions': [
          {'bucket': '#{bucket}'},
          ['starts-with', '$key', '#{key}'],
          {'acl': '#{acl}'},
          {'Content-Type': 'application/pdf'},
          {'Content-Disposition': 'attachment'},
          ['starts-with', '$Filename', ''],
          ['eq', '$success_action_status', '201']
      ]}
    ").gsub(/\n|\r/, '')
  signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret, policy)).gsub(/\n| |\r/, '')
  { :access_key => access_key, :key => key, :policy => policy, :signature => signature, :sas => sas, :bucket => bucket, :acl => acl, :expiration => expiration }
end