Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 client connect through proxy - putObject getting NullPointerException

I've got this simple piece of code which is trying to upload a file to Amazon S3 via 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 got 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'm using the latest aws 1.3.8 sdk from amazon. The proxy is set up in another PC next to me and it's just a simple Javascript proxy (http://www.catonmat.net/http-proxy-in-nodejs/)

I can't figure it out why. Somebody can help me, please?

like image 768
Eugene Avatar asked May 09 '12 08:05

Eugene


People also ask

What is S3 proxy?

S3 proxy middleware for returning S3 objects Express apps. Useful for streaming media files and data files from S3 without having to configure web hosting on the entire origin bucket. You can explicitly override the cache headers of the underlying S3 objects. Added an option to remove bucket name from url.

Does S3 PutObject overwrite?

@XiongChiamiov - the S3 ´PutObject´ action indeed implies overwriting, it's simply how S3 works by default. If you need protection against accidental deletion, you might want to look into Using versioning to preserve, retrieve, and restore every version of every object stored in your Amazon S3 bucket.

Is S3 protocol https?

The default download protocol is HTTP, and the S3 API also supports HTTPS.

Could not find S3 endpoint or NAT gateway for subnet?

Error: Could not find S3 endpoint or NAT gateway for subnetId in VPC. Check the subnet ID and VPC ID in the message to help you diagnose the issue. Check that you have an Amazon S3 VPC endpoint set up, which is required with AWS Glue. In addition, check your NAT gateway if that's part of your configuration.


1 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();
like image 148
Michieru Avatar answered Nov 16 '22 02:11

Michieru