package awss3app;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
public class S3Sample {
private static String bucketName = "purgecoveringletter";
private static String keyId = "ASASASXIQBFDERREWWW";
private static String keyName = "wew23434cb/0KBRvubU+vqwqqw223";
//private static String END_POINT_URL = "http://s3-ap-southeast-1.amazonaws.com";
private static String END_POINT_URL = "http://s3.amazonaws.com";
private static String S3_CACHE="60";
static String fileName = "images.jpg";
public static void main(String[] args) throws IOException {
AWSCredentials credentials = new BasicAWSCredentials(keyId,keyName);
java.security.Security.setProperty("networkaddress.cache.ttl", S3_CACHE);
AmazonS3 s3Client = new AmazonS3Client(credentials);
Region southEast1 = Region.getRegion(Regions.AP_SOUTHEAST_1);
//Region res = Region.getRegion(Regions. "ap-south-1");
s3Client.setRegion(southEast1);
s3Client.setEndpoint(END_POINT_URL);
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setSignerOverride("AWSS3V4SignerType");
//s3Client.setEndpoint("http://s3.amazonaws.com");
try {
System.out.println("Uploading a new object to S3 from a file\n");
//File file = new File(uploadFileName);
// Upload file
//s3Client.putObject(new PutObjectRequest(bucketName, keyName, file));
s3Client.putObject(new PutObjectRequest(bucketName, fileName,new File("G:\\images.jpg")));
GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName, keyName);
S3Object objectPortion = s3Client.getObject(rangeObjectRequest);
System.out.println("Printing bytes retrieved:");
displayTextInputStream(objectPortion.getObjectContent());
} 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 " +
"an internal error while trying to " +
"communicate with S3, " +
"such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
}
private static void displayTextInputStream(InputStream input)
throws IOException {
// Read one text line at a time and display.
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
while (true) {
String line = reader.readLine();
if (line == null) break;
System.out.println(" " + line);
}
System.out.println();
}
}
Dependency jars
After upload the file you can check S3 Browser for uploaded files
No comments:
Post a Comment