Example usage for com.amazonaws AmazonServiceException getStatusCode

List of usage examples for com.amazonaws AmazonServiceException getStatusCode

Introduction

In this page you can find the example usage for com.amazonaws AmazonServiceException getStatusCode.

Prototype

public int getStatusCode() 

Source Link

Document

Returns the HTTP status code that was returned with this service exception.

Usage

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. c  o  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.kiribuki.queueservice.QueueService.java

License:Open Source License

public boolean SendMessage(String message) {
    if (swok == true) {
        try {// w  ww .j  av a  2  s .c o  m
            sqs.sendMessage(new SendMessageRequest(myQueueUrl, message));
            swok = true;
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which means your request made it "
                    + "to Amazon SQS, 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());
            swok = false;
        }
    }
    return swok;
}

From source file:com.liferay.portal.store.s3.S3Store.java

License:Open Source License

protected boolean isFileNotFound(AmazonClientException amazonClientException) {

    if (amazonClientException instanceof AmazonServiceException) {
        AmazonServiceException amazonServiceException = (AmazonServiceException) amazonClientException;

        String errorCode = amazonServiceException.getErrorCode();

        if (errorCode.equals(_ERROR_CODE_FILE_NOT_FOUND)
                && (amazonServiceException.getStatusCode() == _STATUS_CODE_FILE_NOT_FOUND)) {

            return true;
        }//from   ww  w. j  av  a2  s  .c  o m
    }

    return false;
}

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);/* w w  w  .j  av a 2 s.com*/

        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.logpig.mweagle.aws.S3FilePutRunnable.java

License:Apache License

@Override
public void run() {
    boolean createBucket = false;
    boolean doExit = false;
    int attempt = 0;
    final AmazonS3Client s3Client = new AmazonS3Client(this.s3Settings.getAWSCredentials());
    while (!doExit && attempt != this.s3Settings.retryCount) {
        try {/* w w  w  .  j  a v a 2 s  .  c om*/
            if (!s3Settings.mockPut) {
                if (createBucket) {
                    s3Client.createBucket(this.s3Settings.bucketName, this.s3Settings.regionName);
                }
                final File logfile = new File(this.filePath);
                final String keyName = UUID.randomUUID().toString();
                final PutObjectRequest request = new PutObjectRequest(this.s3Settings.bucketName, keyName,
                        logfile);
                s3Client.putObject(request);
            } else {
                logger.warn("Mocking file POST: {}", this.filePath);
            }
            doExit = true;
        } catch (AmazonServiceException ex) {
            createBucket = false;
            if (HttpURLConnection.HTTP_NOT_FOUND == ex.getStatusCode()
                    && ex.getErrorCode().equals("NoSuchBucket")) {
                createBucket = true;
            } else {
                // If the credentials are invalid, don't keep trying...
                doExit = HttpURLConnection.HTTP_FORBIDDEN == ex.getStatusCode();
                if (doExit) {
                    logger.error(String.format("Authentication error posting %s to AWS.  Will not retry.",
                            this.filePath), ex);
                } else {
                    logger.error(String.format("Failed to post %s to AWS", this.filePath), ex);
                }
            }
        } catch (AmazonClientException ex) {
            createBucket = false;
            logger.error(String.format("Failed to post %s to AWS", this.filePath), ex);
        } finally {
            // Create bucket failures don't count
            if (!createBucket) {
                attempt += 1;
            }
        }
    }
}

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 {// w  w w . j a v  a 2  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 .ja 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 a  v a2s  .  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 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
 *///from   ww  w  . j a  v  a  2  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
  */// w  ww .  j a  v a 2  s . c o  m
@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;
}