Amazon S3 client connects through proxy - putObject gets a NullPointerException

I have this simple piece of code that is trying to upload a file to Amazon S3 through a proxy. This is the code:

BasicAWSCredentials basicCred = new BasicAWSCredentials("my_access_key", "my_secret_key"); ClientConfiguration clientCfg = new ClientConfiguration(); clientCfg.setProtocol(Protocol.HTTP); //setup proxy connection: clientCfg.setProxyHost("192.168.2.12"); clientCfg.setProxyPort(80); AmazonS3 s3 = new AmazonS3Client(basicCred, clientCfg); String bucketName = "mybucket"; String key = "/test/Capture.JPG"; File file = new File("d:/Test_Data/Capture.JPG"); System.out.println("Uploading a new object to S3 from a file"); s3.putObject(new PutObjectRequest(bucketName, key, file)); 

However, this is the error I received from running the program:

 Exception in thread "main" java.lang.NullPointerException at com.amazonaws.util.BinaryUtils.fromHex(BinaryUtils.java:69) at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1066) at javaapplication8.JavaApplication8.main(JavaApplication8.java:48) Java Result: 1 

I am using the latest aws 1.3.8 sdk from amazon. The proxy server is configured on another computer next to me, and it's just a Javascript proxy (http://www.catonmat.net/http-proxy-in-nodejs/)

I can’t understand why. Can anyone help me please?

+7
source share
4 answers

here is another approach

 ClientConfiguration config = new ClientConfiguration(); config.setProtocol(Protocol.HTTPS); config.setProxyHost("YOUR_PROXY_IP"); config.setProxyPort(YOUR_PROXY_PORT); BasicAWSCredentials creds = new BasicAWSCredentials("YOUR_KEY", "YOUR_SECRET"); s3Client = AmazonS3ClientBuilder.standard() .withClientConfiguration(config) .withRegion(Regions.US_EAST_2) .withCredentials(new AWSStaticCredentialsProvider(creds)) .build(); 
+4
source

I ran into this problem. I went through everything in the debugger and found that the cause of the problem was the Amazon S3 client code, which was expecting a header tag named "ETag" , but the response contained "ETag" , with a lowercase "t" .

Headers are placed in Map<String,String> , so when the Amazon S3 client code calls get("ETag") , it returns null , which is then hexData() method and explodes.

This seems to be happening with a number of people using Amazon S3 services or accessing Amazon AWS S3 service through a web proxy.

I don’t know about the fix except to capture the source of the Amazon client, changing the value of Headers.ETAG to "ETag" and creating the JAR myself.

+3
source

Change the protocol to HTTPS.

$ clientCfg.setProtocol (Protocol.HTTPS);

0
source

For the Nodejs AWS SDK, you can consider this solution, similar to the one described above, as well as the AWS documentation mentioned here .

Code for Node.js

 npm install proxy-agent --save 

implementation in AWS SDK configuration object

 const proxy = require('proxy-agent'); AWS.config.update({ httpOptions: { agent: proxy('http://internal.proxy.com') } }); 
0
source

Source: https://habr.com/ru/post/915263/


All Articles