1 package org
.example
.cephs3upload
;
3 import com
.amazonaws
.services
.s3
.model
.AmazonS3Exception
;
4 import com
.amazonaws
.services
.s3
.AmazonS3
;
5 import com
.amazonaws
.services
.s3
.AmazonS3ClientBuilder
;
6 import com
.amazonaws
.client
.builder
.AwsClientBuilder
;
7 import com
.amazonaws
.auth
.AWSStaticCredentialsProvider
;
8 import com
.amazonaws
.auth
.BasicAWSCredentials
;
11 import java
.nio
.file
.Paths
;
15 public static void main( String
[] args
)
17 final String USAGE
= "\n" +
18 "To run this example, supply the name of an S3 bucket and a file to\n" +
21 "Ex: java -jar target/ceph-s3-upload-1.0-SNAPSHOT-jar-with-dependencies.jar <bucketname> <filename>\n";
23 if (args
.length
< 2) {
24 System
.out
.println(USAGE
);
28 String bucket_name
= args
[0];
29 String file_path
= args
[1];
30 String key_name
= Paths
.get(file_path
).getFileName().toString();
32 System
.out
.format("Uploading %s to S3 bucket %s...\n", file_path
, bucket_name
);
33 // Put in the CEPH RGW access and secret keys here in that order "access key" "secret key"
34 // Must also be specified here
35 BasicAWSCredentials credentials
= new BasicAWSCredentials("0555b35654ad1656d804","h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q==");
36 // Note That the AWSClient builder takes in the endpoint and the region
37 // This has to be specified in this file
38 final AmazonS3 s3
= AmazonS3ClientBuilder
40 .withCredentials(new AWSStaticCredentialsProvider(credentials
))
41 .withEndpointConfiguration(new AwsClientBuilder
.EndpointConfiguration("http://127.0.0.1:8000", "default"))
44 s3
.putObject(bucket_name
, key_name
, new File(file_path
));
45 } catch (AmazonS3Exception e
) {
46 System
.err
.println(e
.getMessage()); // raises more explicit error message than e.getErrorMessage() e.g when Bucket is not available
49 System
.out
.println("Object upload successful!");