简体   繁体   English

使用纯 Javascript(无 SDK)对 AWS S3 进行 REST 调用

[英]Making REST calls to AWS S3 using Pure Javascript (No SDK)

I am trying to make a GET request to AWS S3 using pure Javascript.我正在尝试使用纯 Javascript 向 AWS S3 发出 GET 请求。 This is because I am unfortunately no longer able to use the SDK for all of my requests.这是因为不幸的是,我无法再将 SDK 用于我的所有请求。 I have been attempting to follow the documentation provided by Amazon, however I have made very little progress.我一直在尝试遵循 Amazon 提供的文档,但进展甚微。 So far, I have only been able to generate my signature key.到目前为止,我只能生成我的签名密钥。 I would be enthused if someone could post an example of pure Javascript that makes a simple call to retrieve an object or even lists all of the objects with a specific prefix.如果有人可以发布一个纯 Javascript 的示例,该示例可以通过简单的调用来检索一个对象,甚至列出具有特定前缀的所有对象,我会很高兴。 I am, to be perfectly honest, completely lost reading their documentation.老实说,我完全无法阅读他们的文档。 It seems like it is only useful for people who are intimately familiar with making these calls.似乎它只对非常熟悉拨打这些电话的人有用。 #1 and #2 on this image here are what I'm struggling with. 这张图片上的#1 和 #2 是我正在努力解决的问题。 I think I sort of understand what they are wanting but I don't know how to fully translate it into an actual request.我想我有点了解他们想要什么,但我不知道如何将其完全转化为实际请求。 Unfortunately the code examples on their docs are very few and far between - and a lot of them are just pseudocode/small fractions of the whole thing不幸的是,他们文档中的代码示例非常少,而且相距甚远-其中很多只是伪代码/整个过程的一小部分

edit: Hello is anyone even reading this编辑:你好,有人读过这个吗

edit2: Here's some stuff that isn't working that I'm trying to figure out how to do编辑2:这里有一些不起作用的东西,我正试图弄清楚该怎么做

var signingKey = getSigningKey(dateStamp, secretKey, regionName, serviceName);


    var time = new Date();
    //fullURL is something like https://s3.amazon.aws.com/{bucketName}/{imageName}
    time = time.toISOString();
    time = time.replace(/:/g, '').replace(/-/g,'');
    time = time.substring(0,time.indexOf('.'))+"Z";

    var request = new XMLHttpRequest();
    var canonString = "GET\n"+
                        encodeURI(fullURL)+"\n"+
                        encodeURI("Key=asd.jpeg")+"\n"+
                        "host:s3.amazonaws.com\n"+
                        "x-amz-content-sha256:"+CryptoJS.SHA256("").toString()+"\n"+
                        "host;x-amz-content-sha256\n"+
                        CryptoJS.SHA256("").toString();


    var stringToSign = "AWS4-HMAC-SHA256\n"+
                        time+"\n"+
                        "20181002/us-east-1/s3/aws4_request\n"+
                        CryptoJS.SHA256(canonString).toString();


    var authString = CryptoJS.HmacSHA256(signingKey, stringToSign).toString();

    var queryString = "GET https://s3.amazonaws.com/?Action=GetObject&Version=2010-05-08 HTTP/1.1\n"+
                        "Authorization: AWS4-HMAC-SHA256 Credential="+accessKey+"/20181002/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-date, Signature="+authString+"\n"+
                        "host: s3.amazonaws.com\n"+
                        "x-amz-date: "+time+"\n";

    request.open("GET", "https://s3.amazonaws.com/?Action=GetObject&Version=2010-05-08", false);
    request.setRequestHeader("Authorization", "AWS4-HMAC-SHA256 Credential="+accessKey+"/20181002/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-date, Signature="+authString);
    request.setRequestHeader("host", "s3.amazonaws.com");
    request.setRequestHeader("x-amz-date", time);
    request.send();

edit3: Here are a bunch of errors I get, presumably because I have no idea what I'm doing.编辑3:这是我遇到的一堆错误,大概是因为我不知道自己在做什么。

index.js:61 Refused to set unsafe header "host"
index.js:63 OPTIONS https://s3.amazonaws.com/?Action=GetObject&Version=2010-05-08 403 (Forbidden)
index.js:63 Failed to load https://s3.amazonaws.com/?Action=GetObject&Version=2010-05-08: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
index.js:63 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://s3.amazonaws.com/?Action=GetObject&Version=2010-05-08'.

You might want to use the SDK, combined with the browser debugger to figure out how the SDK formats the request.您可能希望将 SDK 与浏览器调试器结合使用,以确定 SDK 如何格式化请求。 In the Chrome debugger Network tab, you can copy the request as a javascript fetch.在 Chrome 调试器网络选项卡中,您可以将请求复制为 javascript 提取。 This will show all the headers you need to set.这将显示您需要设置的所有标题。 You can then use this as a basis for your non-SDK code.然后,您可以将其用作非 SDK 代码的基础。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM