Related links
Developer Resources
This code sample has been taken from JetS3t. The JetS3t suite includes some code samples in the codebase package org.jets3t.samples.
As per the document, the JetS3t code samples need to know your AWS Credentials to work. All the sample classes use a utility method SamplesUtils.loadAWSCredentials() to load your AWS Credentials from a properties file called samples.properties in the classpath. Before running the sample code classes you must create this file and add the properties awsAccessKey and awsSecretKey set to your own AWS Credentials.
- S3 Object representations
Items stored in S3 are represented by one of two objects:
* S3Bucket: An S3Bucket is a top-level container in S3 in which objects are stored. Every bucket is identified by a name, and this name must be unique in S3.
* S3Object: An S3Object is a data object stored inside a bucket. It is identified by a key, which can be any string name. An object also has a Content Length, which is the amount of the data in the object.
- Your Amazon Web Services (AWS) login credentials are required to manage S3 accounts. These credentials are stored in an AWSCredentials object:
- To store data in S3 you must first create a bucket, a container for objects.
S3Bucket testBucket = s3Service.createBucket("test-bucket"); System.out.println("Created test bucket: " + testBucket.getName());
- Uploading data objects
JetS3t uses S3Object classes to represent data objects in S3. To store some information in our new test bucket, we must first create an object with a key/name then tell our S3Service to upload it to S3.
In the example below, we print out information about the S3Object before and after uploading it to S3. These print-outs demonstrate that the S3Object returned by the putObject method contains extra information provided by S3, such as the date the object was last modified on an S3 server.
//Create an empty object with a key/name, and print the object's details.
S3Object object = new S3Object("object");
System.out.println("S3Object before upload: " + object)
//Upload the object to our test bucket in S3.
object = s3Service.putObject(testBucket, object);
//Print the details about the uploaded object, which contains more information.
System.out.println("S3Object after upload: " + object);
The example above will create an empty object in S3, which isn't very useful. To include data in the object you must provide some data for the object. If you know the Content/Mime type of the data (e.g. text/plain) you should set this too.
S3Object's can contain any data available from an input stream, but JetS3t provides two convenient object types to hold File or String data. These convenient constructors automatically set the Content-Type and Content-Length of the object.
//Create an S3Object based on a string, with Content-Length set automatically and
//Content-Type set to "text/plain"
String stringData = "Hello World!";
S3Object stringObject = new S3Object("HelloWorld.txt", stringData);
//Create an S3Object based on a file, with Content-Length set automatically and
//Content-Type set based on the file's extension (using the Mimetypes utility class)
File fileData = new File("src/org/jets3t/samples/CodeSamples.java");
S3Object fileObject = new S3Object(fileData);
If your data isn't a File or String you can use any input stream as a data source, but you must manually set the Content-Length.
//Create an object containing a greeting string as input stream data.
String greeting = "Hello World!";
S3Object helloWorldObject = new S3Object("HelloWorld2.txt");
ByteArrayInputStream greetingIS = new ByteArrayInputStream(greeting.getBytes());
helloWorldObject.setDataInputStream(greetingIS);
helloWorldObject.setContentLength(
greeting.getBytes(Constants.DEFAULT_ENCODING).length);
helloWorldObject.setContentType("text/plain");
//Upload the data objects.
s3Service.putObject(testBucket, stringObject);
s3Service.putObject(testBucket, fileObject);
s3Service.putObject(testBucket, helloWorldObject);
//Print details about the uploaded object.
System.out.println("S3Object with data: " + helloWorldObject);
Overall there are multiple steps involved in uploading single file to the S3 bucket. In addition, to retain the original path of the file on S3 bucket is not as simple as IDrive EVS. Also, using S3 APIs you cannot upload multiple file(s) / folder(s) the way IDrive EVS does with a single line of code.