Last active
October 8, 2016 03:57
-
-
Save harshavardhana/6d1da16ac037f2b4337e87d8edfb49cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"). | |
* You may not use this file except in compliance with the License. | |
* A copy of the License is located at | |
* | |
* http://aws.amazon.com/apache2.0 | |
* | |
* or in the "license" file accompanying this file. This file is distributed | |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | |
* express or implied. See the License for the specific language governing | |
* permissions and limitations under the License. | |
*/ | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.io.Writer; | |
import java.util.UUID; | |
import com.amazonaws.AmazonClientException; | |
import com.amazonaws.AmazonServiceException; | |
import com.amazonaws.auth.AWSCredentials; | |
import com.amazonaws.auth.profile.ProfileCredentialsProvider; | |
import com.amazonaws.regions.Region; | |
import com.amazonaws.regions.Regions; | |
import com.amazonaws.services.s3.AmazonS3; | |
import com.amazonaws.services.s3.AmazonS3Client; | |
import com.amazonaws.services.s3.S3ClientOptions; | |
import com.amazonaws.services.s3.model.Bucket; | |
import com.amazonaws.services.s3.model.GetObjectRequest; | |
import com.amazonaws.services.s3.model.ListObjectsRequest; | |
import com.amazonaws.services.s3.model.ObjectListing; | |
import com.amazonaws.services.s3.model.PutObjectRequest; | |
import com.amazonaws.services.s3.model.S3Object; | |
import com.amazonaws.services.s3.model.S3ObjectSummary; | |
/** | |
* This sample demonstrates how to make basic requests to Amazon S3 using the | |
* AWS SDK for Java. | |
* <p> | |
* <b>Prerequisites:</b> You must have a valid Amazon Web Services developer | |
* account, and be signed up to use Amazon S3. For more information on Amazon | |
* S3, see http://aws.amazon.com/s3. | |
* <p> | |
* Fill in your AWS access credentials in the provided credentials file | |
* template, and be sure to move the file to the default location | |
* (~/.aws/credentials) where the sample code will load the credentials from. | |
* <p> | |
* <b>WARNING:</b> To avoid accidental leakage of your credentials, DO NOT keep | |
* the credentials file in your source directory. | |
* | |
* http://aws.amazon.com/security-credentials | |
*/ | |
public class S3Sample { | |
public static void main(String[] args) throws IOException { | |
/* | |
* The ProfileCredentialsProvider will return your [default] | |
* credential profile by reading from the credentials file located at | |
* (~/.aws/credentials). | |
*/ | |
AWSCredentials credentials = null; | |
try { | |
credentials = new ProfileCredentialsProvider().getCredentials(); | |
} catch (Exception e) { | |
throw new AmazonClientException( | |
"Cannot load the credentials from the credential profiles file. " + | |
"Please make sure that your credentials file is at the correct " + | |
"location (~/.aws/credentials), and is in valid format.", e); | |
} | |
// System.setProperty("org.apache.commons.logging.Log", | |
// "org.apache.commons.logging.impl.NoOpLog"); | |
AmazonS3 s3 = new AmazonS3Client(credentials); | |
//Region usWest2 = Region.getRegion(Regions.US_WEST_2); | |
Region usEast1 = Region.getRegion(Regions.US_EAST_1); | |
s3.setRegion(usEast1); | |
s3.setEndpoint("http://localhost:9000"); | |
S3ClientOptions clientOptions = new S3ClientOptions(); | |
clientOptions.withPathStyleAccess(true); | |
s3.setS3ClientOptions(clientOptions); | |
String bucketName = "my-first-s3-bucket";// + UUID.randomUUID(); | |
String key = "MyObjectKey"; | |
System.out.println("==========================================="); | |
System.out.println("Getting Started with Amazon S3"); | |
System.out.println("===========================================\n"); | |
try { | |
/* | |
* Upload an object to your bucket - You can easily upload a file to | |
* S3, or upload directly an InputStream if you know the length of | |
* the data in the stream. You can also specify your own metadata | |
* when uploading to S3, which allows you set a variety of options | |
* like content-type and content-encoding, plus additional metadata | |
* specific to your applications. | |
*/ | |
System.out.println("Uploading a new object to S3 from a file\n"); | |
s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); | |
} catch (AmazonServiceException ase) { | |
System.out.println("Caught an AmazonServiceException, which means your request made it " | |
+ "to Amazon S3, but was rejected with an error response for some reason."); | |
System.out.println("Error Message: " + ase.getMessage()); | |
System.out.println("HTTP Status Code: " + ase.getStatusCode()); | |
System.out.println("AWS Error Code: " + ase.getErrorCode()); | |
System.out.println("Error Type: " + ase.getErrorType()); | |
System.out.println("Request ID: " + ase.getRequestId()); | |
} catch (AmazonClientException ace) { | |
System.out.println("Caught an AmazonClientException, which means the client encountered " | |
+ "a serious internal problem while trying to communicate with S3, " | |
+ "such as not being able to access the network."); | |
System.out.println("Error Message: " + ace.getMessage()); | |
} | |
} | |
/** | |
* Creates a temporary file with text data to demonstrate uploading a file | |
* to Amazon S3 | |
* | |
* @return A newly created temporary file with text data. | |
* | |
* @throws IOException | |
*/ | |
private static File createSampleFile() throws IOException { | |
File file = File.createTempFile("aws-java-sdk-", ".txt"); | |
file.deleteOnExit(); | |
Writer writer = new OutputStreamWriter(new FileOutputStream(file)); | |
writer.write("abcdefghijklmnopqrstuvwxyz\n"); | |
writer.write("01234567890112345678901234\n"); | |
writer.write("!@#$%^&*()-=[]{};':',.<>/?\n"); | |
writer.write("01234567890112345678901234\n"); | |
writer.write("abcdefghijklmnopqrstuvwxyz\n"); | |
writer.close(); | |
return file; | |
} | |
/** | |
* Displays the contents of the specified input stream as text. | |
* | |
* @param input | |
* The input stream to display as text. | |
* | |
* @throws IOException | |
*/ | |
private static void displayTextInputStream(InputStream input) throws IOException { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(input)); | |
while (true) { | |
String line = reader.readLine(); | |
if (line == null) break; | |
System.out.println(" " + line); | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment