how to upload a file to a public AWS S3 bucket with Java Apache HttpClient

1.3k views Asked by At

Given: AWS S3 Bucket named YOUR_BACKET_NAME with public access READ/WRITE

Need to upload a file to a public S3 bucket.

Using only basic most popular Java Libs like Apache HTTP Client lib.

Should not use AWS SDK.

1

There are 1 answers

0
Boris Daich On BEST ANSWER
    @Test
public void upload_file_to_public_s3_with_httpClient() throws Exception {

    String fileName = "test.txt";
    String bucketName = "YOUR_BACKET_NAME";
    String s3url = "https://" + bucketName + ".s3.amazonaws.com/" + fileName;
    String body = "BODY OF THE FILE";

    HttpEntity entity = MultipartEntityBuilder
            .create()
            .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
            .addBinaryBody("file", body.getBytes())
            .build();

    HttpResponse returnResponse = Request.Put(s3url).body(entity).execute().returnResponse();
    StatusLine statusLine = returnResponse.getStatusLine();
    String responseStr = EntityUtils.toString(returnResponse.getEntity());
    log.debug("response from S3 : line: {}\n body {}\n ", statusLine, responseStr);
}