lundi 24 août 2015

AmazonS3Client will not connect

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


http://ift.tt/1lRPa8S http://ift.tt/1NxvV5f


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("/AwsCredentials.properties"));
        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.setCatering(catering);
        newAttach = aDao.add(newAttach, getCurrentUser());

} 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?




Aucun commentaire:

Enregistrer un commentaire