我当前在S3存储桶上复制对象的基于REST的访问代码不起作用。我的S3存储桶配置为https访问,并且还启用了sse。我如何修改下面的代码才能让它工作呢?目前,我刚收到一条错误消息‘捕获AmazonClientException,这意味着客户端在尝试与S3通信时遇到错误,例如无法访问网络。错误消息: Unable to execute HTTP request: mybucket.s3.amazonaws.com’。
@PUT
@Path("/copy/{bucketName}")
public void copyObject(@PathParam("bucketName") String bucketName, @QueryParam("fromPath") String fromPath,
@QueryParam("toPath") String toPath) throws AmazonClientException {
AmazonS3 s3client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());
try {
CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName, fromPath, bucketName, toPath);
System.out.println("Copying object.");
s3client.copyObject(copyObjRequest);
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, " + "which means your request made it "
+ "to Amazon S3, but was rejected with an error " + "response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, " + "which means the client encountered "
+ "an internal error while trying to " + " communicate with S3, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
}发布于 2016-06-22 05:33:46
解决了它。这最终成为了一个代理问题。为了克服这个问题,我必须做以下工作。当我克服网络问题时,我遇到了另一个异常,要求我设置S3端点。下面的代码发生了变化。
public static final String PROXY_HOST = "<my proxy hostname>";
public static final int PROXY_PORT = <my_proxy_port>;
public static final String S3_ENDPOINT = "https://s3.amazonaws.com";
ClientConfiguration clientCfg = new ClientConfiguration();
clientCfg.setProtocol(Protocol.HTTP);
clientCfg.setProxyHost(PROXY_HOST);
clientCfg.setProxyPort(PROXY_PORT);
AmazonS3 s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain(), clientCfg);
s3Client.setEndpoint(S3_ENDPOINT);
s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));https://stackoverflow.com/questions/37950672
复制相似问题