亚马逊S3& C#签名与错误不匹配

时间:2014-09-23 10:06:34

标签: c# amazon-web-services amazon-s3

我是亚马逊AWS&尝试使用SDK for .NET使用如下所示的控制台应用程序将对象(在这种情况下为图像)放入(上传)到存储桶中:

namespace AwsConsoleApp1
{
    class Program
    {
        static string bucketName = "bucketName";
        static string keyName = "keyName";
        static string filePath = "filePath";

        static IAmazonS3 client;
        public static void Main(string[] args)
        {

            NameValueCollection appConfig = ConfigurationManager.AppSettings;
            string accessKeyID = appConfig["AWSAccessKey"];
            string secretAccessKeyID = appConfig["AWSSecretKey"];

            try
            {
                using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, Amazon.RegionEndpoint.EUWest1))
                {
                    Console.WriteLine("Uploading an object");
                    WritingAnObject();
                }
            }
            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine(s3Exception.Message, s3Exception.InnerException);
            }
            catch (AmazonSecurityTokenServiceException stsException)
            {
                Console.WriteLine(stsException.Message, stsException.InnerException);
            }
        }


        /// <summary>
        /// Put object to AWS bucket
        /// </summary>
        static void WritingAnObject()
        {
            try
            {
                PutObjectRequest putRequest1 = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName,
                    FilePath = filePath
                };

                PutObjectResponse response1 = client.PutObject(putRequest1);

            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                    ||
                    amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Check the provided AWS Credentials.");
                    Console.WriteLine(
                        "For service sign up go to http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine(
                        "Error occurred. Message:'{0}' when writing an object"
                        , amazonS3Exception.Message);
                }
            }
        }        
    }
}

我收到了以下错误:

  

我们计算的请求签名与您提供的签名不匹配。检查您的密钥和登录方法。

2 个答案:

答案 0 :(得分:1)

this post获得答案。

我将Key定义为"/folder/file.png",这是不正确的,因为它在Key的开头有正斜杠。

定义Key的正确方法是"folder/file.png"

答案 1 :(得分:1)

我遇到了与错误403相同的问题,并通过在我的AWS IAM控制台中创建新的访问密钥或凭据(访问密钥ID,秘密访问密钥)解决了这个问题。