AmazonS3Client will not connect

11,560

Solution 1

It looks like your project is missing some dependencies.

You clearly have the aws-java-sdk-s3 jar configured in your project since it's resolving AmazonS3Client, but this jar also depends on aws-java-sdk-core. You need to add the core jar to your classpath.

Solution 2

This is totally weird, since aws-java-sdk-s3 explicitly depends on aws-java-sdk-core (see the pom.xml). Something is fishy here.
For me, it turned out it was a clash of apache httpclient versions (I had older version in one of my POMs than the one amazon library uses).
I've heard from others of similar clashes, e.g. jackson.

So for anyone in this situation, I suggest that you check out Dependency hierarchy when you open a POM.xml in Eclipse (or use mvn dependency:tree. See here for more info).

Also, check the first error message that the AWS throws. It seems that it's not linked as the cause in all further stack traces, which only tell you something like java.lang.NoClassDefFoundError: Could not initialize class com.amazonaws.http.AmazonHttpClient.

Share:
11,560
rawkfist0215
Author by

rawkfist0215

experience with Java RoR AngularJS Angular Typescript JQuery SASS Gulp ActionScript

Updated on June 13, 2022

Comments

  • rawkfist0215
    rawkfist0215 almost 2 years

    I am trying to connect to my AWS S3 bucket to upload a file per these links' instructions.


    http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html#credentials-specify-provider


    For some reason when it tries to instantiate the AmazonS3Client object it throws an exception that's being swallowed and it exits my Struts Action. Because of this, I don't have much information to debug off of.

    I've tried both the The default credential profiles file (~/.aws/credentials) approach and the explicit secret and access key (new BasicAWSCredentials(access_key_id, secret_access_key)

    /**
     * Uses the secret key and access key to return an object for accessing AWS features
     * @return BasicAWSCredentials
     */
    public static BasicAWSCredentials getAWSCredentials() {
        final Properties props = new Properties();
        try {
            props.load(Utils.class.getResourceAsStream("/somePropFile"));
            BasicAWSCredentials credObj = new BasicAWSCredentials(props.getProperty("accessKey"), 
                    props.getProperty("secretKey"));
            return credObj;
        }  catch (IOException e) {
            log.error("getAWSCredentials IOException" + e.getMessage());
            return null;
        }
        catch (Exception e) {
            log.error("getAWSCredentials Exception: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
    

    ********* Code attempting S3 Access **********

    try {
            AmazonS3 s3client = new AmazonS3Client(Utils.getAWSCredentials());
            //AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
            String fileKey = "catering/" + catering.getId() + fileUploadsFileName.get(i);
    
            System.out.println("Uploading a new object to S3 from a file\n");
            s3client.putObject(new PutObjectRequest(
                                                     Utils.getS3BucketName(), 
                                                     fileKey, file));
            // Save Attachment record
            Attachment newAttach = new Attachment();
            newAttach.setFile_key(fileKey);
            newAttach.setFilename(fileUploadsFileName.get(i));
            newAttach.setFiletype(fileUploadsContentType.get(i));
            newAttach = aDao.add(newAttach);
    
    } 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());
            fileErrors.add(fileUploadsFileName.get(i));
        } 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());
            fileErrors.add(fileUploadsFileName.get(i));
        } catch(Exception e) {
        System.out.println("Error Message: " + e.getMessage());
    }
    

    It never makes it past the AmazonS3 s3client = new AmazonS3Client(Utils.getAWSCredentials()); line. I've verified that the BasicAWSCredentials object contains the correct field values. Based on this information what might be going wrong to prevent the S3 client from connecting?

    ** EDIT **


    I found this in the resulting stack trace that seems like useful information:

    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.amazonaws.ClientConfiguration at com.amazonaws.services.s3.AmazonS3Client.(AmazonS3Client.java:384) at gearup.actions.CateringController.assignAttachments(CateringController.java:176) at gearup.actions.CateringController.update(CateringController.java:135)

    Earlier I tried following a demo that created a ClientConfiguration object and set the protocol to HTTP. However I ran into an issue where invoking the new ClientConfiguration(); constructor threw a NullPointerException. Am I missing some requirement here?