]> git.proxmox.com Git - ceph.git/blob - ceph/examples/rgw/java/ceph-s3-upload/src/main/java/org/example/cephs3upload/App.java
update ceph source to reef 18.1.2
[ceph.git] / ceph / examples / rgw / java / ceph-s3-upload / src / main / java / org / example / cephs3upload / App.java
1 package org.example.cephs3upload;
2
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;
9
10 import java.io.File;
11 import java.nio.file.Paths;
12
13 public class App
14 {
15 public static void main( String[] args )
16 {
17 final String USAGE = "\n" +
18 "To run this example, supply the name of an S3 bucket and a file to\n" +
19 "upload to it.\n" +
20 "\n" +
21 "Ex: java -jar target/ceph-s3-upload-1.0-SNAPSHOT-jar-with-dependencies.jar <bucketname> <filename>\n";
22
23 if (args.length < 2) {
24 System.out.println(USAGE);
25 System.exit(1);
26 }
27
28 String bucket_name = args[0];
29 String file_path = args[1];
30 String key_name = Paths.get(file_path).getFileName().toString();
31
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
39 .standard()
40 .withCredentials(new AWSStaticCredentialsProvider(credentials))
41 .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://127.0.0.1:8000", "default"))
42 .build();
43 try {
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
47 System.exit(1);
48 }
49 System.out.println("Object upload successful!");
50 }
51 }