Search code examples
c#amazon-web-servicesamazon-s3

Amazon S3 & C# Signature does not match error


I'm new to Amazon AWS & trying to put (upload) the object (image in this case) to the bucket using the SDK for .NET using a console application shown below:

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);
                }
            }
        }        
    }
}

I'm getting following error:

The request signature we calculated does not match the signature you provided.Check your key and signin method.


Solution

  • Got my answer from this post.

    I was defining Key as "/folder/file.png",which is not correct as it has a forward-slash in the beginning of the Key.

    The correct way of defining the Key is "folder/file.png".