0

I'm trying to upload files to S3 storage using the .Net SDK, and am seeing the following error response when PUTing to the pre signed URL provided by AWS:

The bucket you are attempting to access must be addressed using the specified endpoint

Here's the .Net code - I've confirmed my bucket is located in USWest2.

var amazonS3Client = new AmazonS3Client(accessKey, secretKey, RegionEndpoint.USWest2);
    // Generate a pre-signed URL.
    string folderName = "MyFolder";
    string key = "TestKey";
    GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
    {
      BucketName = _bucketName,
      Key = folderName + "/" + key,
      Verb = HttpVerb.PUT,
      Expires = DateTime.Now.AddMinutes(5)
    };
    var uri = Uri(_amazonS3Client.GetPreSignedURL(request));

    // Upload a file using the pre-signed URL.
    Stream stream = ... // This is set elsewhere, contains a data stream.
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
    HttpWebRequest httpRequest = WebRequest.Create(uri) as HttpWebRequest;
    httpRequest.Method = "PUT";
    using (Stream dataStream = httpRequest.GetRequestStream())
    {
      byte[] buffer = new byte[8000];
      int bytesRead = 0;
      while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
      {
        dataStream.Write(buffer, 0, bytesRead);
      }
    }
    HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;
    if(response.StatusCode != HttpStatusCode.OK)
      throw new Exception("Error uploading to AWS: " + response.StatusDescription);

1 Answer 1

0

This was ultimately caused because the URL returned by the call:

var uri = Uri(_amazonS3Client.GetPreSignedURL(request));

Was of the format:

https://s3-us-west-2.amazonaws.com/....

But AWS redirected this to:

https://s3.amazonaws.com/....

By using the latter URL as the beginning of my "pre signed URL", the files now upload correctly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.