0
votes

I am trying to upload a file to an AWS Bucket in ap-southeast-1 region, The credentials have been verified and are correct.

The S3AsyncClientBuilder throws the null pointer exception for every region endpoint. (https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region)

A related thread at github had trouble while signing the region but that was not an issue here, Once the appropriate region was specified.

https://github.com/aws/aws-sdk-java-v2/issues/448

Here is the class that accesses the AWS Bucket, the line has been commented for clarity.

public class ObjectUploaderInterfaceImpl implements ObjectUploaderInterface {


  private String AWS_ACCESS_KEY;
  private String AWS_SECRET_KEY;
  private S3AsyncClient s3Client; // Asynchronous Client for Non Blocking IO
  private AwsCredentialsProvider credentials;

  public ObjectUploaderInterfaceImpl(String awsAccessKey, String awsSecretKey) {
    this.AWS_ACCESS_KEY = awsAccessKey;
    this.AWS_SECRET_KEY = awsSecretKey;
    this.credentials = new StaticCredentialsProvider(
        new AwsCredentials(this.AWS_ACCESS_KEY, this.AWS_SECRET_KEY));
    try {
      this.s3Client = S3AsyncClient.builder()
          .credentialsProvider(this.credentials)
          .region(Region.AP_SOUTHEAST_1)
          .endpointOverride(new URI("s3-ap-southeast-1.amazonaws.com"))
          .build(); // Throws Null pointer Exception
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  public CompletableFuture<PutObjectResponse> uploadObject(String filePath, String s3Bucket,
      String s3BucketPath) {
    CompletableFuture<PutObjectResponse> future;
    try {
      future = s3Client.putObject(
          PutObjectRequest.builder()
              .bucket(s3Bucket)
              .key(s3BucketPath)
              .build(),
          AsyncRequestProvider.fromFile(Paths.get(filePath))
      );
      future.whenComplete((resp, err) -> {
        if (resp != null) {
          System.out.println("Response from Server : " + resp);
        } else {
          err.printStackTrace();
        }
        try {
          s3Client.close();
        } catch (Exception e) {
          System.out.println(e);
        }

      });
    } catch (Exception e) {
      System.out.println(e.getMessage());
      return null;
    }
    return future;

  }
}

The Exception Stack is as follows

    java.lang.NullPointerException
        at software.amazon.awssdk.config.ImmutableClientConfiguration.lambda$schemeToProtocol$3(ImmutableClientConfiguration.java:141)
        at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:174)
        at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
        at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
        at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
        at software.amazon.awssdk.config.ImmutableClientConfiguration.schemeToProtocol(ImmutableClientConfiguration.java:141)
        at software.amazon.awssdk.config.ImmutableClientConfiguration.initializeLegacyConfiguration(ImmutableClientConfiguration.java:110)
        at software.amazon.awssdk.config.ImmutableClientConfiguration.<init>(ImmutableClientConfiguration.java:48)
        at software.amazon.awssdk.config.ImmutableAsyncClientConfiguration.<init>(ImmutableAsyncClientConfiguration.java:42)
        at software.amazon.awssdk.client.builder.DefaultClientBuilder.asyncClientConfiguration(DefaultClientBuilder.java:216)
        at software.amazon.awssdk.services.s3.DefaultS3AsyncClientBuilder.buildClient(DefaultS3AsyncClientBuilder.java:28)
        at software.amazon.awssdk.services.s3.DefaultS3AsyncClientBuilder.buildClient(DefaultS3AsyncClientBuilder.java:22)
        at software.amazon.awssdk.client.builder.DefaultClientBuilder.build(DefaultClientBuilder.java:110)
        at services.storage.Implementation.ObjectUploaderInterfaceImpl.<init>(ObjectUploaderInterfaceImpl.java:41)
        at services.storage.ObjectUploaderInterfaceTest.<init>(ObjectUploaderInterfaceTest.java:12)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
null
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[error] Test services.storage.ObjectUploaderInterfaceTest.uploadTest failed: java.lang.NullPointerException: null, took 0.008 sec
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

I am not sure what might cause this, there has been a little reference available in the documentation since AWS-SDK 2.0's recent release.

1
Given the reference to schemeToProtocol and that s3-ap-southeast-1.amazonaws.com is a hostname, with no scheme, so not quite a URI, wild speculation suggests that the correct value would be https://s3-ap-southeast-1.amazonaws.com.Michael - sqlbot

1 Answers

0
votes

We need to supply https to prevent signing troubles over the request

https://s3-ap-southeast-1.amazonaws.com

fixed it