List of usage examples for com.amazonaws AmazonServiceException getErrorType
public ErrorType getErrorType()
From source file:org.openflamingo.fs.s3.S3ObjectProvider.java
License:Apache License
public boolean save(InputStream is, long size, String path) { // Assert.notNull(is, " ?? 'is'? ."); Assert.notNull(is, "Please enter the input stream."); // Assert.hasLength(path, "?? ? ?? 'path'? ."); Assert.hasLength(path, "Please enter the path."); try {/*from w w w . j a v a 2 s .c om*/ String bucket = S3Utils.getBucket(path); String key = StringUtils.remove(path, "/" + bucket + "/"); ObjectMetadata metadata = new ObjectMetadata(); metadata.setHeader(Headers.CONTENT_LENGTH, size); awsClient.putObject(new PutObjectRequest(bucket, key, is, metadata)); return true; } 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()); // throw new FileSystemException("?? . ? ? ? .", ase); throw new FileSystemException("Connot copy the file.", ase); } 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()); // throw new FileSystemException("?? . ? ? ?.", ace); throw new FileSystemException("Connot copy the file.", ace); } }
From source file:org.p365.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*//from w w w .ja 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 */ AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider()); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); String bucketName = "mynewbuket"; String key = "Myobj/sd.jpg"; 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"); if (!s3.doesBucketExist(bucketName)) { 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"); String pathname = "D:\\Program Files\\apache-tomcat-7.0.42\\webapps\\WorkerForP365\\src\\AAA_1465.jpg"; File file = new File(pathname); s3.putObject( new PutObjectRequest(bucketName, key, file).withCannedAcl(CannedAccessControlList.PublicRead)); /* * 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:org.restcomm.connect.commons.amazonS3.S3AccessTool.java
License:Open Source License
public URI uploadFile(final String fileToUpload) { if (s3client == null) { s3client = getS3client();/*from w ww .j a v a 2 s . co m*/ } if (logger.isInfoEnabled()) { logger.info("S3 Region: " + bucketRegion.toString()); } try { if (testing && (!testingUrl.isEmpty() || !testingUrl.equals(""))) { // s3client.setEndpoint(testingUrl); // s3client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true)); FileUtils.touch(new File(URI.create(fileToUpload))); } StringBuffer bucket = new StringBuffer(); bucket.append(bucketName); if (folder != null && !folder.isEmpty()) bucket.append("/").append(folder); URI fileUri = URI.create(fileToUpload); if (logger.isInfoEnabled()) { logger.info("File to upload to S3: " + fileUri.toString()); } File file = new File(fileUri); while (!FileUtils.waitFor(file, 30)) { } if (file.exists()) { PutObjectRequest putRequest = new PutObjectRequest(bucket.toString(), file.getName(), file); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType(new MimetypesFileTypeMap().getContentType(file)); putRequest.setMetadata(metadata); if (reducedRedundancy) putRequest.setStorageClass(StorageClass.ReducedRedundancy); s3client.putObject(putRequest); if (removeOriginalFile) { removeLocalFile(file); } URI recordingS3Uri = s3client.getUrl(bucket.toString(), file.getName()).toURI(); return recordingS3Uri; // return downloadUrl.toURI(); } else { logger.error("Timeout waiting for the recording file: " + file.getAbsolutePath()); return null; } } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException"); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); return null; } catch (AmazonClientException ace) { logger.error("Caught an AmazonClientException "); logger.error("Error Message: " + ace.getMessage()); return null; } catch (URISyntaxException e) { logger.error("URISyntaxException: " + e.getMessage()); return null; } catch (IOException e) { logger.error("Problem while trying to touch recording file for testing", e); return null; } }
From source file:org.selman.tweetamo.PersistentStore.java
License:Apache License
private void handleException(Exception e) throws Exception { if (e instanceof AmazonServiceException) { AmazonServiceException ase = (AmazonServiceException) e; LOG.error("Caught an AmazonServiceException, which means your request made it " + "to AWS, but was rejected with an error response for some reason.", e); LOG.error("Error Message: " + ase.getMessage()); LOG.error("HTTP Status Code: " + ase.getStatusCode()); LOG.error("AWS Error Code: " + ase.getErrorCode()); LOG.error("Error Type: " + ase.getErrorType()); LOG.error("Request ID: " + ase.getRequestId()); } else if (e instanceof AmazonClientException) { AmazonClientException ace = (AmazonClientException) e; LOG.error("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with AWS, " + "such as not being able to access the network.", e); LOG.error("Error Message: " + ace.getMessage()); }/*from ww w .j av a 2 s . co m*/ throw e; }
From source file:org.symphonyoss.vb.util.AwsS3Client.java
License:Apache License
/** * Provide a list of objects from a given bucket w/prefix (folder). * * @param bucketName S3 bucket name/* w w w . ja va 2 s . c o m*/ * @param prefix S3 folder within the bucket * @return List of {@link S3ObjectSummary} sorted by date */ public List<S3ObjectSummary> getAllObjects(String bucketName, String prefix) { try { logger.debug("Listing S3 objects for s3://{}/{}", bucketName, prefix); final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName) .withPrefix(prefix); ListObjectsV2Result result; List<S3ObjectSummary> allObjects = new ArrayList<>(); do { result = s3Client.listObjectsV2(req); allObjects.addAll(result.getObjectSummaries()); req.setContinuationToken(result.getNextContinuationToken()); } while (result.isTruncated()); allObjects.sort(Comparator.comparing(S3ObjectSummary::getLastModified)); return allObjects; } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException, " + "which means your request made it " + "to Amazon S3, but was rejected with an error response " + "for some reason."); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { logger.error("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."); logger.error("Error Message: " + ace.getMessage()); } return null; }
From source file:org.symphonyoss.vb.util.AwsS3Client.java
License:Apache License
public InputStream getObject(S3ObjectSummary objectSummary) { S3Object object = null;/*from w w w .j a va 2s. c om*/ try { logger.info("Retrieving object inputstream for s3://{}/{}", objectSummary.getBucketName(), objectSummary.getKey()); object = s3Client .getObject(new GetObjectRequest(objectSummary.getBucketName(), objectSummary.getKey())); } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException, " + "which means your request made it " + "to Amazon S3, but was rejected with an error response " + "for some reason."); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { logger.error("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."); logger.error("Error Message: " + ace.getMessage()); } return object == null ? null : object.getObjectContent(); }
From source file:org.symphonyoss.vb.util.AwsS3Client.java
License:Apache License
public void putObject(String destBucket, String key, InputStream inputStream, ObjectMetadata metaData) { try {//from w w w . ja va2 s. c o m logger.info("Put object for s3://{}/{}", destBucket, key); byte[] bytes = IOUtils.toByteArray(inputStream); if (metaData == null) metaData = new ObjectMetadata(); metaData.setContentLength(bytes.length); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); s3Client.putObject(new PutObjectRequest(destBucket, key, byteArrayInputStream, metaData)); } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException, " + "which means your request made it " + "to Amazon S3, but was rejected with an error response " + "for some reason."); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { logger.error("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."); logger.error("Error Message: " + ace.getMessage()); } catch (IOException e) { logger.error("Obtaining length", e); } }
From source file:org.symphonyoss.vb.util.AwsS3Client.java
License:Apache License
public void moveObject(S3ObjectSummary objectSummary, String destBucket, String destKey) { try {// ww w . ja v a 2 s . c o m // Copying object CopyObjectRequest copyObjRequest = new CopyObjectRequest(objectSummary.getBucketName(), objectSummary.getKey(), destBucket, destKey); s3Client.copyObject(copyObjRequest); DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(objectSummary.getBucketName(), objectSummary.getKey()); s3Client.deleteObject(deleteObjectRequest); } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException, " + "which means your request made it " + "to Amazon S3, but was rejected with an error response " + "for some reason."); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { logger.error("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."); logger.error("Error Message: " + ace.getMessage()); } }
From source file:org.terracotta.TerracottaCloudFormationSample.java
License:Open Source License
@org.junit.BeforeClass public static void createStack() throws Exception { /*/*from w ww . ja va 2s.c o m*/ * 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 */ amazonCloudFormationClient = new AmazonCloudFormationClient(new PropertiesCredentials( TerracottaCloudFormationSample.class.getResourceAsStream("/AwsCredentials.properties"))); System.out.println("================================"); System.out.println("Terracotta CloudFormation Sample"); System.out.println("================================\n"); try { // Create a stack CreateStackRequest createRequest = new CreateStackRequest(); createRequest.setStackName(stackName); createRequest.setTemplateBody(convertStreamToString( TerracottaCloudFormationSample.class.getResourceAsStream("/TerracottaServerArray.template"))); Parameter parameter = new Parameter(); parameter.setParameterKey("KeyName"); parameter.setParameterValue(keyChainName); List parameters = new ArrayList(); parameters.add(parameter); createRequest.setParameters(parameters); System.out.println("Creating a stack called " + createRequest.getStackName() + "."); amazonCloudFormationClient.createStack(createRequest); // Wait for stack to be created // Note that you could use SNS notifications on the CreateStack call to track the progress of the stack creation System.out.println("Stack creation completed, the stack " + stackName + " completed with " + waitForCompletion(amazonCloudFormationClient, stackName)); // Show all the stacks for this account along with the resources for each stack for (Stack stack : amazonCloudFormationClient.describeStacks(new DescribeStacksRequest()).getStacks()) { System.out.println( "Stack : " + stack.getStackName() + " [" + stack.getStackStatus().toString() + "]"); DescribeStackResourcesRequest stackResourceRequest = new DescribeStackResourcesRequest(); stackResourceRequest.setStackName(stack.getStackName()); for (StackResource resource : amazonCloudFormationClient .describeStackResources(stackResourceRequest).getStackResources()) { System.out.format(" %1$-40s %2$-25s %3$s\n", resource.getResourceType(), resource.getLogicalResourceId(), resource.getPhysicalResourceId()); } } // Lookup a resource by its logical name DescribeStackResourcesRequest logicalNameResourceRequest = new DescribeStackResourcesRequest(); logicalNameResourceRequest.setStackName(stackName); logicalNameResourceRequest.setLogicalResourceId(logicalResourceName); System.out.format("Looking up resource name %1$s from stack %2$s\n", logicalNameResourceRequest.getLogicalResourceId(), logicalNameResourceRequest.getStackName()); for (StackResource resource : amazonCloudFormationClient .describeStackResources(logicalNameResourceRequest).getStackResources()) { System.out.format(" %1$-40s %2$-25s %3$s\n", resource.getResourceType(), resource.getLogicalResourceId(), resource.getPhysicalResourceId()); } //deleteStack } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to AWS CloudFormation, 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 AWS CloudFormation, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:org.zalando.stups.fullstop.plugin.SaveSecurityGroupsPlugin.java
License:Apache License
private List<String> listS3Objects(String bucketName, String prefix) { final List<String> commonPrefixes = Lists.newArrayList(); AmazonS3Client s3client = new AmazonS3Client(); try {/*www .j a v a2s . c om*/ System.out.println("Listing objects"); ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withDelimiter("/") .withBucketName(bucketName).withPrefix(prefix); ObjectListing objectListing; do { objectListing = s3client.listObjects(listObjectsRequest); commonPrefixes.addAll(objectListing.getCommonPrefixes()); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } listObjectsRequest.setMarker(objectListing.getNextMarker()); } while (objectListing.isTruncated()); } 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()); } return commonPrefixes; }