如何解决AmazonS3Exception:Forbidden(服务:Amazon S3;状态代码:403;错误代码:403 Forbidden;请求ID:null)使用java

时间:2015-09-01 09:43:56

标签: java amazon-s3

堆栈跟踪

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)

当我在AmazonS3EncryptionClient对象中存储对象时,我收到此异常。这是我的代码

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

3 个答案:

答案 0 :(得分:2)

我有同样的问题,但在我的情况下,问题在于代理,Hhere是如何设置代理,

这是一个例子,

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

答案 1 :(得分:1)

我通过在Amazon IAM管理控制台中创建存储桶并获取endpont的主机来解决此异常。

答案 2 :(得分:0)

我有同样的问题,这是由于使用代理引起的,因为该应用程序与S3存储桶位于同一区域。对于部署在不同区域的应用程序,相同的代码可以正常工作。

在设置代理客户端配置之前,我需要能够检查应用程序是否位于同一区域。