aboutsummaryrefslogtreecommitdiff
path: root/other/java/s3copier/src
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2018-11-24 03:24:40 -0800
committerChris Lu <chris.lu@gmail.com>2018-11-24 03:24:40 -0800
commitd9871e92d2a7afe21804e62f211b468a72a216f4 (patch)
tree0783479cfd8dc1a1143f2cc18db3cb7356898f65 /other/java/s3copier/src
parent76cba561cf38ca89c8483dd7ec3c8fa2206d96fd (diff)
downloadseaweedfs-d9871e92d2a7afe21804e62f211b468a72a216f4.tar.xz
seaweedfs-d9871e92d2a7afe21804e62f211b468a72a216f4.zip
adjust module name
Diffstat (limited to 'other/java/s3copier/src')
-rw-r--r--other/java/s3copier/src/main/java/com/seaweedfs/s3/HighLevelMultipartUpload.java63
-rw-r--r--other/java/s3copier/src/main/java/com/seaweedfs/s3/PutObject.java101
-rw-r--r--other/java/s3copier/src/test/java/com/seaweedfs/s3/PutObjectTest.java38
3 files changed, 202 insertions, 0 deletions
diff --git a/other/java/s3copier/src/main/java/com/seaweedfs/s3/HighLevelMultipartUpload.java b/other/java/s3copier/src/main/java/com/seaweedfs/s3/HighLevelMultipartUpload.java
new file mode 100644
index 000000000..b86df95a0
--- /dev/null
+++ b/other/java/s3copier/src/main/java/com/seaweedfs/s3/HighLevelMultipartUpload.java
@@ -0,0 +1,63 @@
+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.transfer.TransferManager;
+import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
+import com.amazonaws.services.s3.transfer.Upload;
+
+import java.io.File;
+
+public class HighLevelMultipartUpload {
+
+ public static void main(String[] args) throws Exception {
+ String bucketName = "javabucket";
+ String filePath = args[0];
+ File file = new File(filePath);
+ String keyName = "path/to/" + file.getName();
+
+ try {
+ 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();
+
+ TransferManager tm = TransferManagerBuilder.standard()
+ .withS3Client(s3Client)
+ .build();
+
+ // TransferManager processes all transfers asynchronously,
+ // so this call returns immediately.
+ Upload upload = tm.upload(bucketName, keyName, file);
+ System.out.println("Object upload started");
+
+ // Optionally, wait for the upload to finish before continuing.
+ upload.waitForCompletion();
+ System.out.println("Object upload complete");
+ } 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();
+ }
+ }
+}
diff --git a/other/java/s3copier/src/main/java/com/seaweedfs/s3/PutObject.java b/other/java/s3copier/src/main/java/com/seaweedfs/s3/PutObject.java
new file mode 100644
index 000000000..c310f0162
--- /dev/null
+++ b/other/java/s3copier/src/main/java/com/seaweedfs/s3/PutObject.java
@@ -0,0 +1,101 @@
+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 com.amazonaws.services.s3.model.S3Object;
+import com.amazonaws.util.IOUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Hello world!
+ */
+public class PutObject {
+
+ private static Log log = LogFactory.getLog(PutObject.class);
+
+ 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];
+
+ String stringContent = "Uploaded String Object v3";
+ try {
+
+ // Upload a text string as a new object.
+ s3Client.putObject(bucketName, stringObjKeyName, stringContent);
+
+ // 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);
+
+ // test reads
+ S3Object written = s3Client.getObject(bucketName, stringObjKeyName);
+ try {
+ String expected = IOUtils.toString(written.getObjectContent());
+
+ if (!stringContent.equals(expected)){
+ System.out.println("Failed to put and get back the content!");
+ }
+
+ } catch (IOException e) {
+ throw new SdkClientException("Error streaming content from S3 during download");
+ } finally {
+ IOUtils.closeQuietly(written, log);
+ }
+
+ // test deletes
+ s3Client.deleteObject(bucketName, stringObjKeyName);
+
+
+ // delete bucket
+ String tmpBucket = "tmpbucket";
+ s3Client.createBucket(tmpBucket);
+ s3Client.putObject(tmpBucket, stringObjKeyName, stringContent);
+ s3Client.deleteBucket(tmpBucket);
+
+ } 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/s3copier/src/test/java/com/seaweedfs/s3/PutObjectTest.java b/other/java/s3copier/src/test/java/com/seaweedfs/s3/PutObjectTest.java
new file mode 100644
index 000000000..0404dab60
--- /dev/null
+++ b/other/java/s3copier/src/test/java/com/seaweedfs/s3/PutObjectTest.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 PutObjectTest
+ extends TestCase
+{
+ /**
+ * Create the test case
+ *
+ * @param testName name of the test case
+ */
+ public PutObjectTest(String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite()
+ {
+ return new TestSuite( PutObjectTest.class );
+ }
+
+ /**
+ * Rigourous Test :-)
+ */
+ public void testApp()
+ {
+ assertTrue( true );
+ }
+}