user3898783
user3898783

Reputation: 207

How to resolve AmazonS3Exception: Forbidden (Service: Amazon S3; Status Code: 403; Error Code: 403 Forbidden; Request ID: null) using java

Stack trace

Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: Forbidden (Service: Amazon S3; Status Code: 403; Error Code: 403 Forbidden; Request ID: null), S3 Extended Request ID: null
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1182)
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:770)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:489)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:310)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3604)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3557)
    at com.amazonaws.services.s3.AmazonS3Client.getS3AccountOwner(AmazonS3Client.java:689)
    at com.amazonaws.services.s3.AmazonS3Client.getS3AccountOwner(AmazonS3Client.java:681)
    at testKMSkeyUploadObject.main(testKMSkeyUploadObject.java:101)

I am getting this exception when I am storing object in AmazonS3EncryptionClient object. Here is my code

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(plaintext.length);
                objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); 

    AmazonS3EncryptionClient s3 = new AmazonS3EncryptionClient(credentials,materialProvider).withRegion(Region.getRegion(Regions.US_EAST_1));;

    PutObjectRequest putRequest = new PutObjectRequest(
                        bucket, keyId, new ByteArrayInputStream(plaintext), objectMetadata);
    putRequest.setRequestCredentials(credentials);

    s3.setEndpoint("https://kms.us-east-1.amazonaws.com");

Upvotes: 5

Views: 16053

Answers (3)

Dio
Dio

Reputation: 704

I had the same issue and was caused by the usage of proxy because the application was living in the same region as the S3 bucket. For the applications that were deployed in a different region the same code worked fine.

I needed to be able to check whether the application is at the same region before setting the proxy client configuration.

Upvotes: 1

Michieru
Michieru

Reputation: 194

I have the same issue, but in my case the problem was with the proxy, Hhere is how you can set the proxy,

here is an example,

ClientConfiguration config = new ClientConfiguration();
config.setProtocol(Protocol.HTTPS);
config.setProxyHost("YOUR_PROXY_IP");
config.setProxyPort(YOUR_PROXY_PORT);


BasicAWSCredentials creds = new BasicAWSCredentials("YOUR_KEY", "YOUR_SECRET"); 
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
    .withClientConfiguration(config)
    .withRegion(Regions.US_EAST_2)
    .withCredentials(new AWSStaticCredentialsProvider(creds))                   
    .build();

Upvotes: 3

user3898783
user3898783

Reputation: 207

I resolved this exception by creating bucket in Amazon IAM Management Console and getting host for endpont.

Upvotes: 0

Related Questions