我正在开发一个包含在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/)
我有两个问题: