It is a memo of uploading and downloading from java to S3. In the case of proxy environment, if proxy is not set, it will time out. Other than that, it was made with the official sample.
I would like to investigate the details and add them.
pom.xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.112</version>
</dependency>
java
static final String S3_ACCESS_KEY = "";
static final String S3_SECRET_KEY = "";
static final String S3_SERVICE_END_POINT = "";
static final String S3_REGION = "";
static final String S3_BUCKET_NAME = "";
java
//Authentication process
private static AmazonS3 auth(){
System.out.println("auth start");
//AWS credentials
AWSCredentials credentials = new BasicAWSCredentials(S3_ACCESS_KEY, S3_SECRET_KEY);
//Client settings
ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProxyHost("[proxyHost]");
clientConfig.setProxyPort([portNo]);
//Endpoint setting
EndpointConfiguration endpointConfiguration = new EndpointConfiguration(S3_SERVICE_END_POINT, S3_REGION);
//Generate S3 access client
AmazonS3 client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials))
.withClientConfiguration(clientConfig)
.withEndpointConfiguration(endpointConfiguration).build();
System.out.println("auth end");
return client;
}
java
//Upload processing
private static void upload() throws Exception{
System.out.println("upload start");
//Authentication process
AmazonS3 client = auth();
File file = new File("[Upload file path]");
FileInputStream fis = new FileInputStream(file);
ObjectMetadata om = new ObjectMetadata();
om.setContentLength(file.length());
final PutObjectRequest putRequest = new PutObjectRequest(S3_BUCKET_NAME, file.getName(), fis, om);
//Permission settings
putRequest.setCannedAcl(CannedAccessControlList.PublicRead);
//upload
client.putObject(putRequest);
fis.close();
System.out.println("upload end");
}
java
//Download processing
private static void download() throws Exception{
System.out.println("download start");
//Authentication process
AmazonS3 client = auth();
final GetObjectRequest getRequest = new GetObjectRequest(S3_BUCKET_NAME, "[Download file name");
S3Object object = client.getObject(getRequest);
FileOutputStream fos = new FileOutputStream(new File("[Output destination path]"));
IOUtils.copy(object.getObjectContent(), fos);
fos.close();
System.out.println("download end");
}
Recommended Posts