gpt4 book ai didi

javascript - 无法在phonegap中将文件上传到Amazon S3

转载 作者:行者123 更新时间:2023-11-28 08:20:39 26 4
gpt4 key购买 nike

当我将文件上传到 Amazon S3 时,我无法调用上传函数,我不知道为什么,请帮助我。这是我的代码

Blockquote

var s3Uploader = (函数() { var crypto = require('crypto'),secret = "My_AWSSecretKey",policy,policyBase64,signature;

policy = {
"expiration" : "2020-12-31T12:00:00.000Z",
"conditions" : [ {
"bucket" : "MobileCommerce"
}, [ "starts-with", "$key", "" ], {
"acl" : 'public-read'
}, [ "starts-with", "$Content-Type", "" ],
[ "content-length-range", 0, 524288000 ] ]
};

policyBase64 = new Buffer(JSON.stringify(policy), 'utf8').toString('base64');
console.log("Policy Base64:");
console.log(policyBase64);
signature = crypto.createHmac('sha1',secret).update(policyBase64).digest('base64');
console.log("Signature:");
console.log(signature);

var s3URI = encodeURI("http://MobileCommerce.s3.amazonaws.com/"),
awsKey = 'My_AWSAccessKey',
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
}

}());

Blockquote

这是拍摄照片并将照片发送到 Amazon S3 的功能

Blockquote

(函数(){

var $img = $('img', '.scroller'),

// Take a picture using the camera
takePicture = function(e) {
var options = {
quality : 45,
targetWidth : 1000,
targetHeight : 1000,
destinationType : Camera.DestinationType.FILE_URI,
encodingType : Camera.EncodingType.JPEG,
sourceType : Camera.PictureSourceType.CAMERA,
};

navigator.camera.getPicture(function(imageURI) {
$img.attr('src', imageURI);
var fileName = "" + (new Date()).getTime() + ".jpg";
// consider a more reliable way to generate unique ids
s3Uploader.upload(imageURI, fileName).done(function() {
alert("S3 upload succeeded");
}).fail(function() {
alert("S3 upload failed");
});
}, function(message) {
alert('abc');
// We typically get here because the use canceled the photo
// operation. Fail silently.
}, options);

return false;

};

$('.camera-btn').on('click', takePicture);

}());

Blockquote

我使用 lib cordova-2.5.0.js 。在html代码中

最佳答案

亚马逊刚刚允许跨源资源共享,理论上它允许您的用户直接上传到 S3,而不使用您的服务器(和 PHP)作为代理。

所以这是使用 CORS 和 javascript 在 Amazon S3 上进行可恢复文件上传的示例

refer

另请参阅 here了解更多详情

关于javascript - 无法在phonegap中将文件上传到Amazon S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23006482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com