From 73f331df84d2b2ff1fff8cb87eae1edfbe983c2d Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 2 Sep 2018 14:19:24 -0700 Subject: [PATCH] add java example of copying to weed s3 end point --- other/java/s3copy/copier/pom.xml | 35 ++++++++++ .../main/java/com/seaweedfs/s3/S3Copy.java | 66 +++++++++++++++++++ .../java/com/seaweedfs/s3/S3CopyTest.java | 38 +++++++++++ 3 files changed, 139 insertions(+) create mode 100644 other/java/s3copy/copier/pom.xml create mode 100644 other/java/s3copy/copier/src/main/java/com/seaweedfs/s3/S3Copy.java create mode 100644 other/java/s3copy/copier/src/test/java/com/seaweedfs/s3/S3CopyTest.java diff --git a/other/java/s3copy/copier/pom.xml b/other/java/s3copy/copier/pom.xml new file mode 100644 index 000000000..f8cb9e91c --- /dev/null +++ b/other/java/s3copy/copier/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + com.seaweedfs.s3 + copier + jar + 1.0-SNAPSHOT + copier + http://maven.apache.org + + + + + com.amazonaws + aws-java-sdk-bom + 1.11.327 + pom + import + + + + + + + com.amazonaws + aws-java-sdk-s3 + + + junit + junit + 3.8.1 + test + + + diff --git a/other/java/s3copy/copier/src/main/java/com/seaweedfs/s3/S3Copy.java b/other/java/s3copy/copier/src/main/java/com/seaweedfs/s3/S3Copy.java new file mode 100644 index 000000000..f00a0ad0a --- /dev/null +++ b/other/java/s3copy/copier/src/main/java/com/seaweedfs/s3/S3Copy.java @@ -0,0 +1,66 @@ +package com.seaweedfs.s3; + +import com.amazonaws.AmazonServiceException; +import com.amazonaws.ClientConfiguration; +import com.amazonaws.SdkClientException; +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.client.builder.AwsClientBuilder; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import com.amazonaws.services.s3.model.ObjectMetadata; +import com.amazonaws.services.s3.model.PutObjectRequest; + +import java.io.File; + +/** + * Hello world! + */ +public class S3Copy { + public static void main(String[] args) { + + AWSCredentials credentials = new BasicAWSCredentials("ANY-ACCESSKEYID", "ANY-SECRETACCESSKEY"); + ClientConfiguration clientConfiguration = new ClientConfiguration(); + clientConfiguration.setSignerOverride("AWSS3V4SignerType"); + + AmazonS3 s3Client = AmazonS3ClientBuilder + .standard() + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration( + "http://localhost:8333", Regions.US_WEST_1.name())) + .withPathStyleAccessEnabled(true) + .withClientConfiguration(clientConfiguration) + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .build(); + + String bucketName = "javabucket"; + String stringObjKeyName = "strObject2"; + String fileObjKeyName = "fileObject2"; + String fileName = args[0]; + + try { + + // Upload a text string as a new object. + s3Client.putObject(bucketName, stringObjKeyName, "Uploaded String Object v3"); + + // Upload a file as a new object with ContentType and title specified. + PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName)); + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentType("plain/text"); + metadata.addUserMetadata("x-amz-meta-title", "someTitle"); + request.setMetadata(metadata); + s3Client.putObject(request); + } catch (AmazonServiceException e) { + // The call was transmitted successfully, but Amazon S3 couldn't process + // it, so it returned an error response. + e.printStackTrace(); + } catch (SdkClientException e) { + // Amazon S3 couldn't be contacted for a response, or the client + // couldn't parse the response from Amazon S3. + e.printStackTrace(); + } + + System.out.println("Hello World!"); + } +} diff --git a/other/java/s3copy/copier/src/test/java/com/seaweedfs/s3/S3CopyTest.java b/other/java/s3copy/copier/src/test/java/com/seaweedfs/s3/S3CopyTest.java new file mode 100644 index 000000000..2bc89b87d --- /dev/null +++ b/other/java/s3copy/copier/src/test/java/com/seaweedfs/s3/S3CopyTest.java @@ -0,0 +1,38 @@ +package com.seaweedfs.s3; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class S3CopyTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public S3CopyTest(String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( S3CopyTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}