AmazonS3Builder produces valid AmazonS3 object but ultimately returns null

I am using AWS Java SDK v1 and I am trying to instantiate AmazonS3 using the builder. I am following the tutorial in the AWS documentation here and passing explicit credentials using the BasicAWSCredentials object. When the builder builds, I am returning the null client back to s3Client

where I am storing the instance and I am not sure why. No errors thrown or caught by try-catch blocks. This is how I make the link:

    BasicAWSCredentials awsCreds = null;
    try {
        awsCreds = new BasicAWSCredentials(settings.getAccessKey(), settings.getSecretKey());
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }

    try {
        AmazonS3 s3Client = AmazonS3Client.builder()
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                .withRegion(Regions.US_EAST_1)
                .build();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(s3Client);

      

I walked through it using the Eclipse debugger and it looks like the call is build()

actually returning a valid AmazonS3Client instance, but then before that reference is returned to a variable s3Client

, a step appears where checkPackageAccess()

, and that return value is what is returned by some reason. Upon further checking, I found that checkPackageAccess()

is a method in java.lang.ClassLoader that is called by the JVM and has a void return type. My application doesn't seem to have a default SecurityManager installed, so there is no other function inside the ClassLoader method checkPackageAccess()

.

I am a little confused about this behavior. My JRE is 1.8. As I understand it, ClassLoader is always called when looking for a class definition, but why will it be called after the class has already been instantiated, and why is the original return value of the function build()

not going back to the caller context? In this case, the debugger shows a valid AmazonS3Client object and is returned by the call build()

even before checkPackageAccess is called.

I made an instance of AmazonRedshift with almost identical code in the same project before and it worked fine, so I'm pretty sure the problem is with the AmazonS3 class or the creator, but I'm not sure where and I don't see any errors or strange prints.

Code used to create a similar connection to Amazon Redshift:

        BasicAWSCredentials awsCreds = null;
    try {
        awsCreds = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
    try {
        client = AmazonRedshiftClientBuilder.standard()
                                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                                .withRegion(Regions.US_EAST_1)
                                .build();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(client);

      

Has anyone debugged similar issues before? What can I do to resolve this issue and return a valid instance?

+3


source to share


2 answers


The problem turned out to be much more fundamental. I am very ashamed to write this. I had to get a second pair of eyes at my code to find it, but I was updating s3Client in the try block.

private AmazonS3 s3Client = null;

      

...



BasicAWSCredentials awsCreds = null;
try {
    awsCreds = new BasicAWSCredentials(settings.getAccessKey(), settings.getSecretKey());
} catch (Exception e) {
    e.printStackTrace();
    throw new IOException(e.getMessage());
}

try {
    AmazonS3 s3Client = AmazonS3Client.builder()
            .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
            .withRegion(Regions.US_EAST_1)
            .build();
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println(s3Client);

      

By the time he reached the code that actually used the AmazonS3 object, the local variable holding the reference to it had already lost its limits. I can't believe I didn't get it.

0


source


Are you ready to build your builder from your own class and not from the S3Client method builder()

? The following code, which is parallel to your Redshift corpus, works well for me.

    static final String BUCKET_NAME = "hello-world";

    /** Creates bucket
    @param args Command line arguments*/
    public static void main(final String[] args) {

            AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();

            CreateBucketRequest request = new
                    CreateBucketRequest(BUCKET_NAME);

            Bucket bucket = s3.createBucket(request);

    System.out.println("S3 Hello World completed.");

      



For what it's worth, my compiler can't find the static method builder()

even though it's actually in the API.

0


source







All Articles