<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.246</version>
</dependency>
@Slf4j
@Component
public class AwsClientImpl {
public void uploadToS3(Map<String, String> chronoMap, String fileName,
InputStream contentStream) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.addUserMetadata("transmitMessages", "False");
objectMetadata.setContentType("binary/octet-stream");
String s3Urinew = "s3://<bucket-name>/mytest/{day}";
AmazonS3URI s3Uri = new AmazonS3URI(s3Urinew);
String objectKey = StringSubstitutor.replace(s3Uri.getKey(), chronoMap, "{", "}")
.concat(fileName);
PutObjectRequest putObjectRequest = new PutObjectRequest(s3Uri.getBucket(), objectKey, contentStream,
objectMetadata);
AmazonS3 s3Client = this.buildAmazonS3Client();
try {
s3Client.putObject(putObjectRequest);
} catch (AmazonServiceException ase) {
String reason = String.format(
"Encountered AmazonServiceException: \n"
+ "HTTP Status Code: %d \n"
+ "Error Message: %s \n"
+ "AWS Error Code: %s /n"
+ "Error Type: %s \n"
+ "Request ID: %s \n",
ase.getStatusCode(),
ase.getMessage(),
ase.getErrorCode(),
ase.getErrorType().toString(),
ase.getRequestId());
log.error(reason, ase);
} catch (AmazonClientException ace) {
log.error("Encountered AmazonClientException: ", ace);
}
}
public AmazonS3 buildAmazonS3Client() {
String accessKeyId = "<accessId>";
String secretAccessKey = "<secretAccesskey>";
return AmazonS3ClientBuilder.standard().withRegion(Regions.EU_CENTRAL_1)
.withCredentials(
new AWSStaticCredentialsProvider(new BasicAWSCredentials(
accessKeyId,
secretAccessKey)))
.build();
}
}
Few of my other blogs:-
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
22 | |
16 | |
11 | |
10 | |
9 | |
9 | |
7 | |
7 | |
7 | |
5 |