gpt4 book ai didi

javascript - 如何使用 Javascript 和 AJAX 上传到 Wasabi 存储

转载 作者:行者123 更新时间:2023-12-02 00:07:08 25 4
gpt4 key购买 nike

我如何使用带有 AJAX 的客户端浏览器 Javascript 将文件上传到 Wasabi 存储?

最佳答案

Wasabi 技术支持部门的 Pankaj 回复我说这个代码片段可以正常工作。他们推荐下载并使用 Amazon AWS SDK for S3因为 Wasabi 符合 S3 API。

<!DOCTYPE html>
<html>
<head>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.619.0.min.js"></script>
</head>
<body>

<h1>Wasabi Upload Test</h1>
<input type="file" id="wasabiupload" onchange="handleFile()" />

<script>

function handleFile() {
// console.log("handle file - " + JSON.stringify(event, null, 2));
var files = document.getElementById('wasabiupload').files;
if (!files.length) {
return alert('Please choose a file to upload first.');
}
var f = files[0];
var fileName = f.name;

const s3 = new AWS.S3({
correctClockSkew: true,
endpoint: 'https://s3.wasabisys.com', //use appropriate endpoint as per region of the bucket
accessKeyId: 'Wasabi-Access-keys',
secretAccessKey: 'Wasabi-Secret-Access-key',
region: 'us-east-1'
,logger: console
});

console.log('Loaded');
const uploadRequest = new AWS.S3.ManagedUpload({
params: { Bucket: 'bucket-name', Key: 'file-name', Body: f },
service: s3
});

uploadRequest.on('httpUploadProgress', function(event) {
const progressPercentage = Math.floor(event.loaded * 100 / event.total);
console.log('Upload progress ' + progressPercentage);
});

console.log('Configed and sending');

uploadRequest.send(function(err) {
if (err) {
console.log('UPLOAD ERROR: ' + JSON.stringify(err, null, 2));
} else {
console.log('Good upload');
}
});

}
</script>
</body>
</html>

关于javascript - 如何使用 Javascript 和 AJAX 上传到 Wasabi 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60221575/

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