List of usage examples for com.amazonaws AmazonServiceException getMessage
@Override
public String getMessage()
From source file:hu.mta.sztaki.lpds.cloud.entice.imageoptimizer.iaashandler.amazontarget.Storage.java
License:Apache License
/** * @param file Local file to upload//from ww w . ja va2 s. co m * @param endpoint S3 endpoint URL * @param accessKey Access key * @param secretKey Secret key * @param bucket Bucket name * @param path Key name (path + file name) * @throws Exception On any error */ public static void upload(File file, String endpoint, String accessKey, String secretKey, String bucket, String path) throws Exception { AmazonS3Client amazonS3Client = null; try { AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); ClientConfiguration clientConfiguration = new ClientConfiguration(); clientConfiguration.setMaxConnections(MAX_CONNECTIONS); clientConfiguration.setMaxErrorRetry(PredefinedRetryPolicies.DEFAULT_MAX_ERROR_RETRY); clientConfiguration.setConnectionTimeout(ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT); amazonS3Client = new AmazonS3Client(awsCredentials, clientConfiguration); S3ClientOptions clientOptions = new S3ClientOptions().withPathStyleAccess(true); amazonS3Client.setS3ClientOptions(clientOptions); amazonS3Client.setEndpoint(endpoint); // amazonS3Client.putObject(new PutObjectRequest(bucket, path, file)); // up to 5GB TransferManager tm = new TransferManager(amazonS3Client); // up to 5TB Upload upload = tm.upload(bucket, path, file); // while (!upload.isDone()) { upload.getProgress().getBytesTransferred(); Thread.sleep(1000); } // to get progress upload.waitForCompletion(); tm.shutdownNow(); } catch (AmazonServiceException x) { Shrinker.myLogger.info("upload error: " + x.getMessage()); throw new Exception("upload exception", x); } catch (AmazonClientException x) { Shrinker.myLogger.info("upload error: " + x.getMessage()); throw new Exception("upload exception", x); } finally { if (amazonS3Client != null) { try { amazonS3Client.shutdown(); } catch (Exception e) { } } } }
From source file:ics.uci.edu.amazons3.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/*ww w .j a v a 2 s . c o m*/ * This credentials provider implementation loads your AWS credentials * from a properties file at the root of your classpath. * * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ final AmazonS3 s3 = new AmazonS3Client( new BasicAWSCredentials("AKIAJTW5BOY6EXOGV2YQ", "PDcnFYIf9Hdo9GsKTEjLXretZ3yEg4mRCDQKjxu6")); 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 { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, * so once a bucket name has been taken by any user, you can't create * another bucket with that same name. * * You can optionally specify a location for your bucket if you want to * keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * 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())); /* * Download an object - When you download an object, you get all of * the object's metadata and a stream from which to read the contents. * It's important to read the contents of the stream as quickly as * possibly since the data is streamed directly from Amazon S3 and your * network connection will remain open until you read all the data or * close the input stream. * * GetObjectRequest also supports several other options, including * conditional downloading of objects based on modification times, * ETags, and selectively downloading a range of an object. */ System.out.println("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, * there is no way to undelete an object, so use caution when deleting objects. */ System.out.println("Deleting an object\n"); s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } 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()); } }
From source file:ie.peternagy.jcrypto.module.config.S3ConfigHandler.java
License:Open Source License
@Override public void validateConfig(Map config) { AWSCredentials credentials = new BasicAWSCredentials((String) config.get("access-key"), (String) config.get("secret-key")); AmazonS3 s3client = new AmazonS3Client(credentials); try {/*from w w w .ja va 2 s. c o m*/ System.out.println("S3 config validation...\n"); s3client.doesBucketExist((String) config.get("bucket-name")); System.out.println("Valid configuration!\n"); } 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()); } }
From source file:iit.edu.supadyay.s3.S3upload.java
public static boolean upload(String bucketName, String uploadFileName, String keyName) throws IOException, InterruptedException { //access = "AKIAJ2YSLRUZR5B3F5HQ"; //secret = "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"; //AWSCredentials credentials = new BasicAWSCredentials(access, secret); //AmazonS3 s3client = new AmazonS3Client(getCredentials()); AmazonS3 s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider()); try {/*w w w.ja v a2 s . co m*/ System.out.println("Uploading a new object to S3 from a file\n"); File file = new File(uploadFileName); System.out.println("I am before here\n"); s3client.createBucket(bucketName); System.out.println("I am here\n"); s3client.putObject(new PutObjectRequest(bucketName, keyName, file)); s3client.setObjectAcl(bucketName, keyName, CannedAccessControlList.PublicReadWrite); } 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()); return false; } 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()); return false; } return true; }
From source file:io.milton.s3.AmazonS3ManagerImpl.java
License:Open Source License
@Override public boolean isRootBucket(String bucketName) { LOG.info("Checks if the specified bucket " + bucketName + " exists or not"); try {//from w ww.j a v a 2 s .c om return amazonS3Client.doesBucketExist(bucketName); } catch (AmazonServiceException ase) { LOG.error(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.error(ace.getMessage(), ace); } return false; }
From source file:io.milton.s3.AmazonS3ManagerImpl.java
License:Open Source License
@Override public Bucket createBucket(String bucketName) { try {//from w ww . ja v a 2 s. c o m // Checks if the specified bucket exists or not and // if doesn't exist then creates a new Amazon S3 bucket // with the specified name in the default (US) region boolean isBucketExist = isRootBucket(bucketName); if (!isBucketExist) { LOG.info("Creates a new Amazon S3 bucket " + bucketName + " with the specified name in the default (US) region"); return amazonS3Client.createBucket(bucketName); } List<Bucket> buckets = findBuckets(); if (!buckets.isEmpty()) { for (Bucket bucket : buckets) { if (bucket.getName().equals(bucketName)) { return bucket; } } } } catch (AmazonServiceException ase) { LOG.error(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.error(ace.getMessage(), ace); } return null; }
From source file:io.milton.s3.AmazonS3ManagerImpl.java
License:Open Source License
@Override public boolean deleteBucket(String bucketName) { LOG.info("Deletes the specified bucket " + bucketName); try {//from w w w . j a v a 2 s. c o m // Make sure delete all the entities in the bucket deleteEntities(bucketName); // Delete the specified bucket for the given name amazonS3Client.deleteBucket(bucketName); return true; } catch (AmazonServiceException ase) { LOG.error(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.error(ace.getMessage(), ace); } return false; }
From source file:io.milton.s3.AmazonS3ManagerImpl.java
License:Open Source License
@Override public List<Bucket> findBuckets() { LOG.info("Returns a list of all Amazon S3 buckets that the authenticated " + "sender of the request owns"); try {/* ww w .j a v a 2s .c o m*/ return amazonS3Client.listBuckets(); } catch (AmazonServiceException ase) { LOG.error(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.error(ace.getMessage(), ace); } return Collections.emptyList(); }
From source file:io.milton.s3.AmazonS3ManagerImpl.java
License:Open Source License
@Override public boolean uploadEntity(String bucketName, String keyName, File file) { LOG.info("Uploads the specified file " + file + " to Amazon S3 under the specified bucket " + bucketName + " and key name " + keyName); try {/* w ww .j a v a2 s . c o m*/ PutObjectResult putObjectResult = amazonS3Client.putObject(bucketName, keyName, file); if (putObjectResult != null) { return true; } } catch (AmazonServiceException ase) { LOG.error(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.error(ace.getMessage(), ace); } return false; }
From source file:io.milton.s3.AmazonS3ManagerImpl.java
License:Open Source License
@Override public boolean uploadEntity(String bucketName, String keyName, InputStream inputStream, ObjectMetadata metadata) {//from w w w .ja v a 2 s. co m LOG.info("Uploads the specified input stream " + inputStream + " and object metadata to Amazon S3 under the specified bucket " + bucketName + " and key name " + keyName); try { PutObjectResult putObjectResult = amazonS3Client.putObject(bucketName, keyName, inputStream, metadata); if (putObjectResult != null) { LOG.info("Upload the specified input stream " + inputStream + " state: " + putObjectResult); return true; } } catch (AmazonServiceException ase) { LOG.warn(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.warn(ace.getMessage(), ace); } return false; }