List of usage examples for com.amazonaws AmazonServiceException getRequestId
public String getRequestId()
From source file:com.ipcglobal.awscdh.config.ManageEc2.java
License:Apache License
/** * Stop EC2 Instances./*w ww .j ava 2 s .c o m*/ * * @throws Exception the exception */ public void stop() throws Exception { try { List<Instance> instances = findInstances(); stopAllInstances(instances); } catch (AmazonServiceException ase) { log.error("Caught Exception: " + ase.getMessage()); log.error("Reponse Status Code: " + ase.getStatusCode()); log.error("Error Code: " + ase.getErrorCode()); log.error("Request ID: " + ase.getRequestId()); throw ase; } catch (AmazonClientException ace) { log.error("Error Message: " + ace.getMessage()); throw ace; } catch (Exception e) { log.error(e); throw e; } }
From source file:com.ipcglobal.fredimportaws.TsvsToRedshift.java
License:Apache License
/** * Process.//from ww w . j a v a 2s . c o m * * @throws Exception the exception */ public void process() throws Exception { try { s3Client.createBucket(new CreateBucketRequest(awsBucketName)); log.info("Start: emptyBucketContents"); long before = System.currentTimeMillis(); emptyBucketContents(); log.info("Complete: emptyBucketContents, elapsed=" + (System.currentTimeMillis() - before)); log.info("Start: transferToBucket"); before = System.currentTimeMillis(); transferToBucket(); log.info("Complete: transferToBucket, elapsed=" + (System.currentTimeMillis() - before)); log.info("Start: copyS3FilesToRedshiftTable"); before = System.currentTimeMillis(); copyS3FilesToRedshiftTable(); log.info("Complete: copyS3FilesToRedshiftTable, elapsed=" + (System.currentTimeMillis() - before)); } catch (AmazonServiceException ase) { log.error("Caught Exception: " + ase.getMessage()); log.error("Reponse Status Code: " + ase.getStatusCode()); log.error("Error Code: " + ase.getErrorCode()); log.error("Request ID: " + ase.getRequestId()); throw ase; } catch (AmazonClientException ace) { log.error("Error Message: " + ace.getMessage()); throw ace; } catch (Exception e) { log.error(e); throw e; } }
From source file:com.jfixby.scarabei.red.aws.test.S3Sample.java
License:Open Source License
public static void main(final String[] args) throws IOException { /*/*from w ww .j av a 2 s.c o m*/ * The ProfileCredentialsProvider will return your [default] credential profile by reading from the credentials file located * at (C:\\Users\\JCode\\.aws\\credentials). */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider("default").getCredentials(); } catch (final 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 (C:\\Users\\%USERNAME%\\.aws\\credentials), and is in valid format.", e); } final AmazonS3 s3 = new AmazonS3Client(credentials); final Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); final String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); final 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 (final 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"); final 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"); final ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (final 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 (final 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 (final 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:com.kirana.utils.GeneratePresignedUrlAndUploadObject.java
public static void main(String[] args) throws IOException { System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, "true"); System.setProperty(SDKGlobalConfiguration.ACCESS_KEY_ENV_VAR, "AKIAJ666LALJZHA6THGQ"); System.setProperty(SDKGlobalConfiguration.SECRET_KEY_ENV_VAR, "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"); // AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider("kirana")); ProfilesConfigFile profile = new ProfilesConfigFile("AwsCredentials.properties"); AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); s3client.setRegion(Region.getRegion(Regions.AP_SOUTHEAST_1)); try {// w ww.j a v a 2 s . co m System.out.println("Generating pre-signed URL."); java.util.Date expiration = new java.util.Date(); long milliSeconds = expiration.getTime(); milliSeconds += 1000 * 60 * 60; // Add 1 hour. expiration.setTime(milliSeconds); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectKey); generatePresignedUrlRequest.setMethod(HttpMethod.PUT); generatePresignedUrlRequest.setExpiration(expiration); // s3client.putObject(bucketName, objectKey, null); URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest); UploadObject(url); System.out.println("Pre-Signed URL = " + url.toString()); } catch (AmazonServiceException exception) { 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: " + exception.getMessage()); System.out.println("HTTP Code: " + exception.getStatusCode()); System.out.println("AWS Error Code:" + exception.getErrorCode()); System.out.println("Error Type: " + exception.getErrorType()); System.out.println("Request ID: " + exception.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:com.liferay.portal.store.s3.S3Store.java
License:Open Source License
protected SystemException transform(AmazonClientException amazonClientException) { if (amazonClientException instanceof AmazonServiceException) { AmazonServiceException amazonServiceException = (AmazonServiceException) amazonClientException; StringBundler sb = new StringBundler(11); sb.append("{errorCode="); String errorCode = amazonServiceException.getErrorCode(); sb.append(errorCode);/* www. j av a2 s . co m*/ sb.append(", errorType="); sb.append(amazonServiceException.getErrorType()); sb.append(", message="); sb.append(amazonServiceException.getMessage()); sb.append(", requestId="); sb.append(amazonServiceException.getRequestId()); sb.append(", statusCode="); sb.append(amazonServiceException.getStatusCode()); sb.append("}"); if (errorCode.equals("AccessDenied")) { return new AccessDeniedException(sb.toString()); } return new SystemException(sb.toString()); } else { return new SystemException(amazonClientException.getMessage(), amazonClientException); } }
From source file:com.maya.portAuthority.util.ImageUploader.java
public static void uploadImage(String imageURL, String imageName, String bucketName) throws MalformedURLException, IOException { // credentials object identifying user for authentication AWSCredentials credentials = new BasicAWSCredentials("AKIAJBFSMHRTIQQ7BKYA", "AdHgeP4dyWInWwPn9YlfxFCm3qP1lHjdxOxeJqDa"); // create a client connection based on credentials AmazonS3 s3client = new AmazonS3Client(credentials); String folderName = "image"; //folder name // String bucketName = "ppas-image-upload"; //must be unique try {//from w w w. j a v a2 s . co m if (!(s3client.doesBucketExist(bucketName))) { s3client.setRegion(Region.getRegion(Regions.US_EAST_1)); // Note that CreateBucketRequest does not specify region. So bucket is // created in the region specified in the client. s3client.createBucket(new CreateBucketRequest(bucketName)); } //Enabe CORS: // <?xml version="1.0" encoding="UTF-8"?> //<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> // <CORSRule> // <AllowedOrigin>http://ask-ifr-download.s3.amazonaws.com</AllowedOrigin> // <AllowedMethod>GET</AllowedMethod> // </CORSRule> //</CORSConfiguration> BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration(); CORSRule corsRule = new CORSRule() .withAllowedMethods( Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.GET })) .withAllowedOrigins(Arrays.asList(new String[] { "http://ask-ifr-download.s3.amazonaws.com" })); configuration.setRules(Arrays.asList(new CORSRule[] { corsRule })); s3client.setBucketCrossOriginConfiguration(bucketName, configuration); } 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()); } String fileName = folderName + SUFFIX + imageName + ".png"; URL url = new URL(imageURL); ObjectMetadata omd = new ObjectMetadata(); omd.setContentType("image/png"); omd.setContentLength(url.openConnection().getContentLength()); // upload file to folder and set it to public s3client.putObject(new PutObjectRequest(bucketName, fileName, url.openStream(), omd) .withCannedAcl(CannedAccessControlList.PublicRead)); }
From source file:com.mycompany.mytubeaws.DownloadServlet.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request// w w w. j a va2s .c o m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String result = ""; String downloadLink = ""; String inputKey = request.getParameter("downloadKey"); if (inputKey == null) result += "inputKey is null; "; else if (inputKey.length() == 0) result += "inputKey is blank; "; else { result += "Downloading an object; "; try { // S3Object object = s3.getObject(new GetObjectRequest(bucketName, inputKey)); // S3ObjectInputStream objectContent = object.getObjectContent(); // // File f = File.createTempFile("aws-java-sdk-download", ""); // FileOutputStream fos = new FileOutputStream(f); // IOUtils.copy(objectContent, fos); // fos.close(); // // result += "Content-Type: '" + object.getObjectMetadata().getContentType() + "'; "; // result += "Mime '" + new MimetypesFileTypeMap().getContentType(f) + "'; "; // // response.setContentType(new MimetypesFileTypeMap().getContentType(f)); // response.setHeader("Content-Disposition","attachment;filename="+inputKey); // // ServletOutputStream out = response.getOutputStream(); // // // f.delete(); GeneratePresignedUrlRequest requestUrl = new GeneratePresignedUrlRequest(bucketName, inputKey); downloadLink = s3.generatePresignedUrl(requestUrl).toString(); System.out.println(downloadLink); //result += downloadLink + " ; "; } 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()); result += "AmazonServiceException thrown; "; } 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()); result += "AmazonClientException thrown; "; } } System.out.println(result); request.setAttribute("resultText", result); request.setAttribute("downloadLink", downloadLink); request.getRequestDispatcher("/DownloadResult.jsp").forward(request, response); //response.sendRedirect("UploadResult.jsp"); }
From source file:com.mycompany.mytubeaws.UploadServlet.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request/*from w w w. j ava2 s.com*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String result = ""; String fileName = null; boolean uploaded = false; // configures upload settings DiskFileItemFactory factory = new DiskFileItemFactory(); // sets memory threshold - beyond which files are stored in disk factory.setSizeThreshold(1024 * 1024 * 3); // 3mb factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); // sets temporary location to store files ServletFileUpload upload = new ServletFileUpload(factory); upload.setFileSizeMax(1024 * 1024 * 40); // sets maximum size of upload file upload.setSizeMax(1024 * 1024 * 50); // sets maximum size of request (include file + form data) try { List<FileItem> formItems = upload.parseRequest(request); // parses the request's content to extract file data if (formItems != null && formItems.size() > 0) // iterates over form's fields { for (FileItem item : formItems) // processes only fields that are not form fields { if (!item.isFormField()) { fileName = item.getName(); UUID id = UUID.randomUUID(); fileName = FilenameUtils.getBaseName(fileName) + "_ID-" + id.toString() + "." + FilenameUtils.getExtension(fileName); File file = File.createTempFile("aws-java-sdk-upload", ""); item.write(file); // write form item to file (?) if (file.length() == 0) throw new RuntimeException("No file selected or empty file uploaded."); try { s3.putObject(new PutObjectRequest(bucketName, fileName, file)); result += "File uploaded successfully; "; uploaded = 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()); result += "AmazonServiceException thrown; "; } 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()); result += "AmazonClientException thrown; "; } file.delete(); } } } } catch (Exception ex) { result += "Generic exception: '" + ex.getMessage() + "'; "; ex.printStackTrace(); } if (fileName != null && uploaded) result += "Generated file ID: " + fileName; System.out.println(result); request.setAttribute("resultText", result); request.getRequestDispatcher("/UploadResult.jsp").forward(request, response); }
From source file:com.netflix.dynomitemanager.sidecore.backup.S3Backup.java
License:Apache License
/** * Uses the Amazon S3 API to upload the AOF/RDB to S3 * Filename: Backup location + DC + Rack + App + Token *//*w w w .j a va2 s . c om*/ @Override public boolean upload(File file, DateTime todayStart) { logger.info("Snapshot backup: sending " + file.length() + " bytes to S3"); /* Key name is comprised of the * backupDir + DC + Rack + token + Date */ String keyName = config.getBackupLocation() + "/" + iid.getInstance().getDatacenter() + "/" + iid.getInstance().getRack() + "/" + iid.getInstance().getToken() + "/" + todayStart.getMillis(); // Get bucket location. logger.info("Key in Bucket: " + keyName); logger.info("S3 Bucket Name:" + config.getBucketName()); AmazonS3Client s3Client = new AmazonS3Client(cred.getAwsCredentialProvider()); try { // Checking if the S3 bucket exists, and if does not, then we create it if (!(s3Client.doesBucketExist(config.getBucketName()))) { logger.error("Bucket with name: " + config.getBucketName() + " does not exist"); return false; } else { logger.info("Uploading data to S3\n"); // Create a list of UploadPartResponse objects. You get one of these for // each part upload. List<PartETag> partETags = new ArrayList<PartETag>(); InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest( config.getBucketName(), keyName); InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest); long contentLength = file.length(); long filePosition = 0; long partSize = this.initPartSize; try { for (int i = 1; filePosition < contentLength; i++) { // Last part can be less than initPartSize (500MB). Adjust part size. partSize = Math.min(partSize, (contentLength - filePosition)); // Create request to upload a part. UploadPartRequest uploadRequest = new UploadPartRequest() .withBucketName(config.getBucketName()).withKey(keyName) .withUploadId(initResponse.getUploadId()).withPartNumber(i) .withFileOffset(filePosition).withFile(file).withPartSize(partSize); // Upload part and add response to our list. partETags.add(s3Client.uploadPart(uploadRequest).getPartETag()); filePosition += partSize; } CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest( config.getBucketName(), keyName, initResponse.getUploadId(), partETags); s3Client.completeMultipartUpload(compRequest); } catch (Exception e) { logger.error("Abosting multipart upload due to error"); s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(config.getBucketName(), keyName, initResponse.getUploadId())); } return true; } } catch (AmazonServiceException ase) { logger.error( "AmazonServiceException;" + " request made it to Amazon S3, but was rejected with an error "); 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 false; } catch (AmazonClientException ace) { logger.error("AmazonClientException;" + " the client encountered " + "an internal error while trying to " + "communicate with S3, "); logger.error("Error Message: " + ace.getMessage()); return false; } }
From source file:com.netflix.dynomitemanager.sidecore.backup.S3Restore.java
License:Apache License
/** * Uses the Amazon S3 API to restore from S3 *//*from ww w .j a v a2 s . c om*/ @Override public boolean restoreData(String dateString) { long time = restoreTime(dateString); if (time > -1) { logger.info("Restoring data from S3."); AmazonS3Client s3Client = new AmazonS3Client(cred.getAwsCredentialProvider()); try { /* construct the key for the backup data */ String keyName = config.getBackupLocation() + "/" + iid.getInstance().getDatacenter() + "/" + iid.getInstance().getRack() + "/" + iid.getInstance().getToken() + "/" + time; logger.info("S3 Bucket Name: " + config.getBucketName()); logger.info("Key in Bucket: " + keyName); // Checking if the S3 bucket exists, and if does not, then we create it if (!(s3Client.doesBucketExist(config.getBucketName()))) { logger.error("Bucket with name: " + config.getBucketName() + " does not exist"); } else { S3Object s3object = s3Client.getObject(new GetObjectRequest(config.getBucketName(), keyName)); logger.info("Content-Type: " + s3object.getObjectMetadata().getContentType()); String filepath = null; if (config.isAof()) { filepath = config.getPersistenceLocation() + "/appendonly.aof"; } else { filepath = config.getPersistenceLocation() + "/nfredis.rdb"; } IOUtils.copy(s3object.getObjectContent(), new FileOutputStream(new File(filepath))); } return true; } catch (AmazonServiceException ase) { logger.error("AmazonServiceException;" + " request made it to Amazon S3, but was rejected with an error "); 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("AmazonClientException;" + " the client encountered " + "an internal error while trying to " + "communicate with S3, "); logger.error("Error Message: " + ace.getMessage()); } catch (IOException io) { logger.error("File storing error: " + io.getMessage()); } } else { logger.error("Date in FP: " + dateString); } return false; }