本文介绍了Amazon S3的REST API 403错误C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一些code工作通过C#使用REST API来从S3文件。我见过的其他人在做类似的事情,但由于某种原因,我总是收到一个403错误。我试图做同样的事情,与AWS SDK的.Net和它的作品,所以我认为这是我创建的授权标头的方式。

是任何人都能够阐明这个任何光线好吗?

 字符串awsAccessId =***;
字符串awsSecretKey =***;
字符串bucketName =thebucket;

字符串httpDate = DateTime.Now.ToString(DDD,DD MMM YYYY HH:MM:SS +0000 \ N);
                字符串canonicalString =GET \ N
                                        +\ N
                                        +\ N
                                        +X-AMZ-日期:+ httpDate +\ N
                                        +/+ bucketName +/readme.txt;

                //现在EN $ C C的规范化字符串$
                编码AE =新UTF8Encoding();
                //创建一个散列对象
                HMACSHA1签名=新HMACSHA1();
                // secretId是哈希键
                signature.Key = ae.GetBytes(awsSecretKey);
                字节[]字节= ae.GetBytes(canonicalString);
                byte []的moreBytes = signature.ComputeHash(字节);
                //将哈希字节数组成base64编码
                字符串连接codedCanonical = Convert.ToBase64String(moreBytes);

                //发送请求
                HttpWebRequest的要求=(HttpWebRequest的)HttpWebRequest.Create(HTTP://+ bucketName +。s3.amazonaws.com/readme.txt);
                request.Headers.Add(X-AMZ-日期,httpDate);
                request.Headers.Add(授权,AWS+ awsAccessId +:+ EN codedCanonical);
                request.Method =GET;

                //得到响应
                HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
                Console.WriteLine(response.Status code);
                Console.Read();
 

解决方案

我测试了code,它的作品!你只需要一个额外的\ n与变化HTTP到HTTPS,就大功告成了。

 字符串stringToConvert =GET \ N
                          +\ N
                          +\ N
                          +\ N
                          +X-AMZ-日期:+的timeStamp +\ N
                          +/+ bucketName +/+文件名;
 

亚马逊的REST API不具备良好的文档,缺少的例子让大家去的SDK代替。

I'm trying to get some code working to fetch a file from S3 using the REST API via C#. I've seen other people doing similar things but for some reason I keep getting a 403 error. I've tried to do the same thing with the AWS SDK for .Net and it works so I assume it's the way I'm creating the authorization header.

Is anyone able to shed any light on this please?

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();
解决方案

I tested your code, it works! you just need an extra \n plus change http to https and you're done.

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

Amazon Rest API don't have a good documentation, the lack of examples makes everyone go to the SDK instead.

这篇关于Amazon S3的REST API 403错误C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:30