]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
examples: Add Java code showing object upload to Ceph RGW bucket 45970/head
authorfaithuniterh <faithuniterh@tutanota.com>
Wed, 8 Jun 2022 20:21:03 +0000 (23:21 +0300)
committerfaithuniterh <faithuniterh@tutanota.com>
Wed, 8 Jun 2022 20:21:03 +0000 (23:21 +0300)
Signed-off-by: faithuniterh <faithuniterh@tutanota.com>
examples/java/ceph-s3-upload/README.md [new file with mode: 0644]
examples/java/ceph-s3-upload/ceph-s3-upload.txt [new file with mode: 0644]
examples/java/ceph-s3-upload/pom.xml [new file with mode: 0644]
examples/java/ceph-s3-upload/src/main/java/org/example/cephs3upload/App.java [new file with mode: 0644]
examples/java/ceph-s3-upload/src/test/java/org/example/cephs3upload/AppTest.java [new file with mode: 0644]

diff --git a/examples/java/ceph-s3-upload/README.md b/examples/java/ceph-s3-upload/README.md
new file mode 100644 (file)
index 0000000..235d436
--- /dev/null
@@ -0,0 +1,16 @@
+# Introduction
+This directory contains Java code examples on how to upload an object to an S3 bucket running on a Ceph RGW cluster.
+
+# Prerequisites
+Linux machine running an RGW Ceph cluster. Preferrably started with the ``OSD=1 MON=1 MDS=0 MGR=0 RGW=1 ../src/vstart.sh --debug --new `` command.  
+Java and Maven installed on the Linux machine.  
+
+## Workflow Procedure
+1. Install AWS CLI version 1 on your Linux machine as explained [here](https://docs.aws.amazon.com/cli/v1/userguide/install-linux.html)
+2. Create a bucket on the Ceph cluster with the command
+``
+aws --endpoint-url http://localhost:8000 s3 mb s3://sample-bucket
+``
+3. Navigate through your file system into the ``ceph-s3-upload`` folder  using your terminal. Please ensure you see the pom.xml file.
+4. Run `` mvn clean package `` to install the required Java packages on the system.
+5. Once successful, run `` java -jar target/ceph-s3-upload-1.0-SNAPSHOT-jar-with-dependencies.jar sample-bucket ceph-s3-upload.txt `` to test out Java s3 object upload on Ceph RGW cluster.
diff --git a/examples/java/ceph-s3-upload/ceph-s3-upload.txt b/examples/java/ceph-s3-upload/ceph-s3-upload.txt
new file mode 100644 (file)
index 0000000..02e6ceb
--- /dev/null
@@ -0,0 +1 @@
+This file will be uploaded to Ceph S3 storage
diff --git a/examples/java/ceph-s3-upload/pom.xml b/examples/java/ceph-s3-upload/pom.xml
new file mode 100644 (file)
index 0000000..bcd6f08
--- /dev/null
@@ -0,0 +1,67 @@
+<project
+       xmlns="http://maven.apache.org/POM/4.0.0"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+       <groupId>org.example.cephs3upload</groupId>
+       <artifactId>ceph-s3-upload</artifactId>
+       <packaging>jar</packaging>
+       <version>1.0-SNAPSHOT</version>
+       <name>ceph-s3-upload</name>
+       <url>http://maven.apache.org</url>
+       <properties>
+               <maven.compiler.source>1.8</maven.compiler.source>
+               <maven.compiler.target>1.8</maven.compiler.target>
+       </properties>
+       <dependencyManagement>
+               <dependencies>
+                       <dependency>
+                               <groupId>com.amazonaws</groupId>
+                               <artifactId>aws-java-sdk-bom</artifactId>
+                               <version>1.12.201</version>
+                               <type>pom</type>
+                               <scope>import</scope>
+                       </dependency>
+               </dependencies>
+       </dependencyManagement>
+       <dependencies>
+               <dependency>
+                       <groupId>com.amazonaws</groupId>
+                       <artifactId>aws-java-sdk-s3</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>junit</groupId>
+                       <artifactId>junit</artifactId>
+                       <version>3.8.1</version>
+                       <scope>test</scope>
+               </dependency>
+       </dependencies>
+        <build>
+            <plugins>
+                <plugin>
+                <extensions>true</extensions>
+                <artifactId>maven-assembly-plugin</artifactId>
+                
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>single</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <addClasspath>true</addClasspath>
+                            <mainClass>org.example.cephs3upload.App</mainClass>
+                        </manifest>
+                    </archive>
+                    <descriptorRefs>
+                        <descriptorRef>jar-with-dependencies</descriptorRef>
+                    </descriptorRefs>
+                </configuration>
+                </plugin>
+            </plugins>
+        </build>
+</project>
diff --git a/examples/java/ceph-s3-upload/src/main/java/org/example/cephs3upload/App.java b/examples/java/ceph-s3-upload/src/main/java/org/example/cephs3upload/App.java
new file mode 100644 (file)
index 0000000..32f334c
--- /dev/null
@@ -0,0 +1,51 @@
+package org.example.cephs3upload;
+
+import com.amazonaws.services.s3.model.AmazonS3Exception;
+import com.amazonaws.services.s3.AmazonS3;
+import com.amazonaws.services.s3.AmazonS3ClientBuilder;
+import com.amazonaws.client.builder.AwsClientBuilder;
+import com.amazonaws.auth.AWSStaticCredentialsProvider;
+import com.amazonaws.auth.BasicAWSCredentials;
+
+import java.io.File;
+import java.nio.file.Paths;
+
+public class App 
+{
+    public static void main( String[] args )
+    {
+        final String USAGE = "\n" +
+                "To run this example, supply the name of an S3 bucket and a file to\n" +
+                "upload to it.\n" +
+                "\n" +
+                "Ex: java -jar target/ceph-s3-upload-1.0-SNAPSHOT-jar-with-dependencies.jar <bucketname> <filename>\n";
+
+        if (args.length < 2) {
+            System.out.println(USAGE);
+            System.exit(1);
+        }
+
+        String bucket_name = args[0];
+        String file_path = args[1];
+        String key_name = Paths.get(file_path).getFileName().toString();
+
+        System.out.format("Uploading %s to S3 bucket %s...\n", file_path, bucket_name);
+        // Put in the CEPH RGW access and secret keys here in that order  "access key"              "secret key"
+        // Must also be specified here
+        BasicAWSCredentials credentials = new BasicAWSCredentials("0555b35654ad1656d804","h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q==");
+        // Note That the AWSClient builder takes in the endpoint and the region
+        // This has to be specified in this file
+        final AmazonS3 s3 = AmazonS3ClientBuilder
+                                .standard()
+                                .withCredentials(new AWSStaticCredentialsProvider(credentials))
+                                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://127.0.0.1:8000", "default"))
+                                .build();
+        try {
+            s3.putObject(bucket_name, key_name, new File(file_path));
+        } catch (AmazonS3Exception e) {
+            System.err.println(e.getMessage()); // raises more explicit error message than e.getErrorMessage() e.g when Bucket is not available
+            System.exit(1);
+        }
+        System.out.println("Object upload successful!");
+    }
+}
diff --git a/examples/java/ceph-s3-upload/src/test/java/org/example/cephs3upload/AppTest.java b/examples/java/ceph-s3-upload/src/test/java/org/example/cephs3upload/AppTest.java
new file mode 100644 (file)
index 0000000..1c80752
--- /dev/null
@@ -0,0 +1,38 @@
+package org.example.cephs3upload;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest 
+    extends TestCase
+{
+    /**
+     * Create the test case
+     *
+     * @param testName name of the test case
+     */
+    public AppTest( String testName )
+    {
+        super( testName );
+    }
+
+    /**
+     * @return the suite of tests being tested
+     */
+    public static Test suite()
+    {
+        return new TestSuite( AppTest.class );
+    }
+
+    /**
+     * Rigourous Test :-)
+     */
+    public void testApp()
+    {
+        assertTrue( true );
+    }
+}