3

My current code for a REST based access to copying objects on my S3 bucket does not work. My S3 bucket is configured for https access and also has sse enabled. How would I modify the code below to make it work? Currently, I just get an error message 'Caught an AmazonClientException, which means the client encountered an error while trying to communicate with S3, such as not being able to access the network. Error Message: 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());
        }
    }
everydaylearn
  • 143
  • 3
  • 14

1 Answers1

2

Solved it. This ended up being a proxy issue. I had to do the following to overcome this. When I overcame the network issue, I had another exception asking me to set the S3 Endpoint. Code changes below.

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));
everydaylearn
  • 143
  • 3
  • 14
  • 2
    since it contains the solution, you might want to accept your own answer so that this question does not appear as unanswered anymore. – Henrik Opel Jun 29 '16 at 12:24