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

How to Use Javascript and AJAX to Upload into Wasabi Storage

我如何使用客户端浏览器 Javascript 和 AJAX 将文件上传到 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>