gpt4 book ai didi

javascript - 使用 Ajax 请求(无 PHP)使用 HTML、javascript 和 jQuery 将图像上传到 Amazon s3

转载 作者:IT王子 更新时间:2023-10-29 02:54:04 27 4
gpt4 key购买 nike

我正在用 HTML、javascript 和 jQuery 开发一个网站。我想在 ajax 请求中将图像上传到 amazon s3 服务器。没有这样的 SDK 可以在 Javascript 中集成 s3。 PHP SDK 可用,但对我没有用。任何人都可以用 javascript 提供解决方案吗?

最佳答案

根据这篇文章使用 XMLHTTPObject 让 Amazon S3 和 CORS 在 js 和 html5 上工作 article .

1:CORS 仅适用于正确的 URL“http://localhost”。 (文件///xyz 会让你发疯)

2:确保您正确编译了 POLICY 和 Secret - 这是我的政策,这是您可以获得项目的链接,以帮助您开始使用 Signature and Policy -- 永远不要用你的 Secret 发布这个 JS!

POLICY_JSON = { "expiration": "2020-12-01T12:00:00.000Z",
"conditions": [
{"bucket": this.get('bucket')},
["starts-with", "$key", ""],
{"acl": this.get('acl')},
["starts-with", "$Content-Type", ""],
["content-length-range", 0, 524288000]
]
};


var secret = this.get('AWSSecretKeyId');
var policyBase64 = Base64.encode(JSON.stringify(POLICY_JSON));
console.log ( policyBase64 )

var signature = b64_hmac_sha1(secret, policyBase64);
b64_hmac_sha1(secret, policyBase64);
console.log( signature);

这是JS代码

function uploadFile() {

var file = document.getElementById('file').files[0];
var fd = new FormData();

var key = "events/" + (new Date).getTime() + '-' + file.name;

fd.append('key', key);
fd.append('acl', 'public-read');
fd.append('Content-Type', file.type);
fd.append('AWSAccessKeyId', 'YOUR ACCESS KEY');
fd.append('policy', 'YOUR POLICY')
fd.append('signature','YOUR SIGNATURE');

fd.append("file",file);

var xhr = new XMLHttpRequest();

xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);

xhr.open('POST', 'https://<yourbucket>.s3.amazonaws.com/', true); //MUST BE LAST LINE BEFORE YOU SEND

xhr.send(fd);
}

辅助函数

function uploadProgress(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
}
else {
document.getElementById('progressNumber').innerHTML = 'unable to compute';
}
}

function uploadComplete(evt) {
/* This event is raised when the server send back a response */
alert("Done - " + evt.target.responseText );
}

function uploadFailed(evt) {
alert("There was an error attempting to upload the file." + evt);
}

function uploadCanceled(evt) {
alert("The upload has been canceled by the user or the browser dropped the connection.");
}

然后是 HTML 表单

 <form id="form1" enctype="multipart/form-data" method="post">
<div class="row">
<label for="file">Select a File to Upload</label><br />
<input type="file" name="file" id="file" onchange="fileSelected()"/>
</div>
<div id="fileName"></div>
<div id="fileSize"></div>
<div id="fileType"></div>
<div class="row">
<input type="button" onclick="uploadFile()" value="Upload" />
</div>
<div id="progressNumber"></div>

CORS 快乐!

关于javascript - 使用 Ajax 请求(无 PHP)使用 HTML、javascript 和 jQuery 将图像上传到 Amazon s3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11240127/

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