Phonegap:上传到Amazon S3,使用FileTransfer还是AWS SDK?

时间:2015-09-02 07:52:28

标签: angularjs cordova amazon-web-services amazon-s3 phonegap-plugins

我正在开发一个包含在Phonegap中的Web应用程序,用于将录制的视频上传到Amazon S3。我做了一些研究,并得出结论有两种方法可以做到这一点:使用Amazon AWS JavaScript SDK,或使用Phonegap FileTransfer API(以及Amazon S3 REST API)。

当您在PhoneGap应用程序中捕获视频时,只能请求视频文件的URI(而不是文件内容本身)。您可以在FileTransfer中使用此URI来发布到S3,如下所示:

var s3Uploader = (function () {

    var s3URI = encodeURI("https://YOUR_S3_BUCKET.s3.amazonaws.com/"),
        policyBase64 = "YOUR_BASE64_ENCODED_POLICY_FILE",
        signature = "YOUR_BASE64_ENCODED_SIGNATURE",
        awsKey = 'YOUR_AWS_USER_KEY',
        acl = "public-read";

    function upload(imageURI, fileName) {

        var deferred = $.Deferred(),
            ft = new FileTransfer(),
            options = new FileUploadOptions();

        options.fileKey = "file";
        options.fileName = fileName;
        options.mimeType = "image/jpeg";
        options.chunkedMode = false;
        options.params = {
            "key": fileName,
            "AWSAccessKeyId": awsKey,
            "acl": acl,
            "policy": policyBase64,
            "signature": signature,
            "Content-Type": "image/jpeg"
        };

        ft.upload(imageURI, s3URI,
            function (e) {
                deferred.resolve(e);
            },
            function (e) {
                deferred.reject(e);
            }, options);

        return deferred.promise();

    }

    return {
        upload: upload
    }

}());

(来自http://coenraets.org/blog/2013/09/how-to-upload-pictures-from-a-phonegap-app-to-amazon-s3/

使用AWS开发工具包时,您可以上传如下文件:

$scope.creds = {
  bucket: 'your_bucket',
  access_key: 'your_access_key',
  secret_key: 'your_secret_key'
}

$scope.upload = function() {
  // Configure The S3 Object 
  AWS.config.update({ accessKeyId: $scope.creds.access_key, secretAccessKey: $scope.creds.secret_key });
  AWS.config.region = 'us-east-1';
  var bucket = new AWS.S3({ params: { Bucket: $scope.creds.bucket } });

  if($scope.file) {
    var params = { Key: $scope.file.name, ContentType: $scope.file.type, Body: $scope.file, ServerSideEncryption: 'AES256' };

    bucket.putObject(params, function(err, data) {
      if(err) {
        // There Was An Error With Your S3 Config
        alert(err.message);
        return false;
      }
      else {
        // Success!
        alert('Upload Done');
      }
    })
    .on('httpUploadProgress',function(progress) {
          // Log Progress Information
          console.log(Math.round(progress.loaded / progress.total * 100) + '% done');
        });
  }
  else {
    // No File Selected
    alert('No File Selected');
  }
}

(来自http://www.cheynewallace.com/uploading-to-s3-with-angularjs/

我有两个问题:

  1. 最好使用Phonegap API或AWS SDK来实现这一目标吗?文件传输是可靠的非常重要,并且在失败时重试。上传也应该在后台继续(当应用程序未显示时)。据我所知,Phonegap使用iOS / Android的核心API函数作为其FileTransfer API。这使我倾向于这种方法。另一方面,AWS SDK是由亚马逊编写的,可能会更好地处理错误。
  2. 在第二个示例中,文件正文本身用于发布到S3。是否可以使用文件URI发布到S3?例如。通过阅读带有某些功能的文件?

0 个答案:

没有答案