【问题标题】:How to upload file to aws s3 using xmlhttprequest如何使用 xmlhttprequest 将文件上传到 aws s3
【发布时间】:2017-12-21 07:29:56
【问题描述】:

如何使用 xmlhttprequest 将文件上传到 s3。我尝试了以下方式,但出现了类似

的错误

'此操作禁止匿名访问'。

这是我的代码:

const xhr = new XMLHttpRequest();
xhr.open('PUT', "https://region.amazonaws.com/bucket-name/");
xhr.setRequestHeader('X-Amz-ACL', 'public-read');
xhr.setRequestHeader('Content-Type', 'image/png');
xhr.send({
    file: file,
    type: 'image/png',
    name: "myfile_9898_0",
    acl: 'public-read',
    AWSAccessKeyId: 'aws key',
    AWSSecreKeyId: 'scret_key'
});

这段代码有什么问题吗?
谁能帮帮我?

【问题讨论】:

  • 您是否将 CORS for (POST,PUT) 配置为存储桶上的允许方法?
  • 是的,我已经为(POST,PUT)配置了 CORS。但我得到以下响应,例如“此操作禁止匿名访问”
  • 这个link 可能会帮助你。

标签: javascript amazon-s3


【解决方案1】:

你把 SecretKey 拼错了。

但是,任何查看此页面的人(假设它位于 html 页面中)都可以窃取您的凭据!将 secretKey 放在任何地方都是一个非常糟糕的主意 - 您最好在服务器上使用您的秘密生成一个“预签名密钥”。您还需要在服务器上设置 CORS 以允许交叉发布。

【讨论】:

猜你喜欢
  • 2018-07-07
  • 2019-12-06
  • 2021-05-14
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
相关资源
最近更新 更多