【问题标题】:Amazon S3 REST API 403 error c#Amazon S3 REST API 403 错误 c#
【发布时间】:2013-11-10 10:52:56
【问题描述】:

我正在尝试让一些代码通过 C# 使用 REST API 从 S3 获取文件。我见过其他人做类似的事情,但由于某种原因,我不断收到 403 错误。我尝试使用适用于 .Net 的 AWS 开发工具包做同样的事情,它可以工作,所以我认为这是我创建授权标头的方式。

请问有大神能解释一下吗?

string awsAccessId = "***";
string awsSecretKey = "***";
string bucketName = "thebucket";

string httpDate = DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss +0000\n");
                string canonicalString = "GET\n"
                                        + "\n"
                                        + "\n"
                                        + "x-amz-date:" + httpDate + "\n"
                                        + "/" + bucketName + "/readme.txt";

                // now encode the canonical string
                Encoding ae = new UTF8Encoding();
                // create a hashing object
                HMACSHA1 signature = new HMACSHA1();
                // secretId is the hash key
                signature.Key = ae.GetBytes(awsSecretKey);
                byte[] bytes = ae.GetBytes(canonicalString);
                byte[] moreBytes = signature.ComputeHash(bytes);
                // convert the hash byte array into a base64 encoding
                string encodedCanonical = Convert.ToBase64String(moreBytes);

                // Send the request
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://" + bucketName +".s3.amazonaws.com/readme.txt");
                request.Headers.Add("x-amz-date", httpDate);
                request.Headers.Add("Authorization", "AWS " + awsAccessId + ":" + encodedCanonical);
                request.Method = "GET";

                // Get the response
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Console.WriteLine(response.StatusCode);
                Console.Read();

【问题讨论】:

  • 如果 sdk 有效,为什么不使用?
  • 我希望将其放入 SSIS 脚本任务中

标签: c# rest amazon-s3


【解决方案1】:

我测试了您的代码,它有效!你只需要一个额外的 \n 加上将 http 更改为 https 就可以了。

 string stringToConvert = "GET\n"
                          + "\n"
                          + "\n"
                          + "\n"
                          + "x-amz-date:" + timeStamp + "\n"  
                          + "/" + bucketName + "/" + FileName;

Amazon Rest API 没有很好的文档,缺乏示例让大家转而使用 SDK。

【讨论】:

    【解决方案2】:

    我不知道这是否是唯一的问题,但它看起来像一个明确的问题:

    + "x-amz-date:" + httpDate + "\n"
    

    x-amz-date 是 HTTP 请求本身中取代 Date: 标头的标头,但在要签名的字符串中,您只需输入日期,不带“x-amz-date:”或前面的任何内容,根据例子:

    GET\n
    \n
    \n
    Tue, 27 Mar 2007 19:36:42 +0000\n
    /johnsmith/photos/puppy.jpg
    

    http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationRequestCanonicalization

    对于一个请求,只能生成一个正确的签名。 S3 将生成该签名,并将其与您发送的签名进行比较,因此要签名的字符串中没有一个字节的错误空间。

    【讨论】:

    • 感谢您的建议。我做了修改,但不幸的是它仍然不起作用。
    • 403 消息是否返回错误,为您提供要签名的字符串并告诉您签名不匹配,或者不匹配?错误说明了什么? (在响应正文中)
    • 错误信息提示签名不匹配:SignatureDoesNotMatch我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2013-03-16
    • 2014-07-13
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多