3

I am trying to upload an image to amazon (AWS SDK 2.3.0) s3 by using the following code

        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
            getApplicationContext(),
            "XXXXXXX",
            // Identity Pool ID
            Regions.EU_WEST_1 // Region
    );
    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);

    TransferUtility transferUtility = new TransferUtility(s3Client, this.
            getApplicationContext());

    TransferObserver observer = transferUtility.upload("testbucketzzzz", "test.jpg",
            file);

Everytime I try to upload an Image i get the following error

UploadTask: Failed to upload: 37 due to The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256. (Service: Amazon S3; Status Code: 400; Error Code: InvalidRequest; Request ID: 9787E53737D02BA7)

I thought this would only be an issue with older versions of the AWS SDK. After countless hours of trying to fix it I found the following "trick" but this just leads me to another error and shouldn't be needed at all.

        System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, "true");
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
ntheg0d
  • 53
  • 7
  • 1
    Well I figured out what was causing the problem, my bucket was located in Frankfurt, I just tried tested it with another bucket in Ireland which worked flawless thats atleast a fix for now. – ntheg0d Sep 08 '16 at 15:35

1 Answers1

1

You can upload an Image to Amazon S3 with the following snipet (make sure you are NOT doing this in MainThread):

    BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(
          ACCESS_KEY,
          SECRET_KEY
      );
    AmazonS3Client s3Client = new AmazonS3Client(basicAWSCredentials);
      s3Client.setEndpoint(ENDPOINT);
      s3Client.setRegion(Region.getRegion(Regions.EU_WEST_1));;
    PutObjectRequest por = new PutObjectRequest(
                "YOUR_BUCKET_NAME", "YOUR_KEY", file);
    }
    s3Client.putObject(por);

If you want more information on why your approach wont work you can refer to this link

Community
  • 1
  • 1
hrskrs
  • 4,447
  • 5
  • 38
  • 52