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.
@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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.