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:eu.stratosphere.nephele.fs.s3.S3FileSystem.java

License:Apache License

/**
 * {@inheritDoc}/*  w  w  w  . j  ava 2 s . co  m*/
 */
@Override
public FileStatus getFileStatus(final Path f) throws IOException {

    final S3BucketObjectPair bop = this.directoryStructure.toBucketObjectPair(f);

    // This is the S3:/// base directory
    if (!bop.hasBucket() && !bop.hasObject()) {
        return new S3FileStatus(f, 0L, true, 0L, 0L);
    }

    try {
        if (bop.hasBucket() && !bop.hasObject()) {

            final List<Bucket> buckets = this.s3Client.listBuckets();
            final Iterator<Bucket> it = buckets.iterator();

            // Iterator throw list of buckets to find out creation date
            while (it.hasNext()) {

                final Bucket bucket = it.next();
                if (bop.getBucket().equals(bucket.getName())) {

                    final long creationDate = dateToLong(bucket.getCreationDate());
                    // S3 does not track access times, so this implementation always sets it to 0
                    return new S3FileStatus(f, 0L, true, creationDate, 0L);
                }
            }

            throw new FileNotFoundException("Cannot find " + f.toUri());
        }

        try {
            final ObjectMetadata om = this.s3Client.getObjectMetadata(bop.getBucket(), bop.getObject());
            final long modificationDate = dateToLong(om.getLastModified());
            // S3 does not track access times, so this implementation always sets it to 0
            if (objectRepresentsDirectory(bop.getObject(), om.getContentLength())) {
                return new S3FileStatus(f, 0L, true, modificationDate, 0L);
            } else {
                return new S3FileStatus(f, om.getContentLength(), false, modificationDate, 0L);
            }

        } catch (AmazonServiceException e) {
            if (e.getStatusCode() == HTTP_RESOURCE_NOT_FOUND_CODE) {
                throw new FileNotFoundException("Cannot find " + f.toUri());
            } else {
                throw e;
            }
        }
    } catch (AmazonClientException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }
}

From source file:eu.stratosphere.nephele.fs.s3.S3FileSystem.java

License:Apache License

/**
 * {@inheritDoc}//from www . j a  v a2 s. c om
 */
@Override
public boolean mkdirs(final Path f) throws IOException {

    final S3BucketObjectPair bop = this.directoryStructure.toBucketObjectPair(f);
    if (!bop.hasBucket() && !bop.hasObject()) {
        // Ignore this call
        return false;
    }

    boolean retCode = false;

    try {

        // Make sure the bucket exists
        if (bop.hasBucket()) {
            if (this.s3Client.doesBucketExist(bop.getBucket())) {
            } else {
                this.s3Client.createBucket(bop.getBucket());
                retCode = true;
            }
        }

        if (bop.hasObject()) {

            // Make sure object name ends with a directory separator character
            String object = bop.getObject();
            if (!object.isEmpty()) {
                if (object.charAt(object.length() - 1) != S3_DIRECTORY_SEPARATOR) {
                    object = object.concat(Character.toString(S3_DIRECTORY_SEPARATOR));
                }
            }

            while (true) {

                try {
                    this.s3Client.getObjectMetadata(bop.getBucket(), object);
                } catch (AmazonServiceException e) {
                    if (e.getStatusCode() == HTTP_RESOURCE_NOT_FOUND_CODE) {
                        createEmptyObject(bop.getBucket(), object);

                        if (object.length() > 1) {
                            final int nextPos = object.lastIndexOf(S3_DIRECTORY_SEPARATOR, object.length() - 2);
                            if (nextPos >= 0) {
                                object = object.substring(0, nextPos + 1);
                                continue;
                            }
                        }

                    } else {
                        // Rethrow the exception
                        throw e;
                    }
                }

                // Object already exists, exit
                break;
            }
        }
    } catch (AmazonClientException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }

    return retCode;
}

From source file:eu.stratosphere.runtime.fs.s3.S3FileSystem.java

License:Apache License

@Override
public FileStatus getFileStatus(final Path f) throws IOException {

    final S3BucketObjectPair bop = this.directoryStructure.toBucketObjectPair(f);

    // This is the S3:/// base directory
    if (!bop.hasBucket() && !bop.hasObject()) {
        return new S3FileStatus(f, 0L, true, 0L, 0L);
    }/*from   w ww .  ja v  a 2s  .co  m*/

    try {
        if (bop.hasBucket() && !bop.hasObject()) {

            final List<Bucket> buckets = this.s3Client.listBuckets();
            final Iterator<Bucket> it = buckets.iterator();

            // Iterator throw list of buckets to find out creation date
            while (it.hasNext()) {

                final Bucket bucket = it.next();
                if (bop.getBucket().equals(bucket.getName())) {

                    final long creationDate = dateToLong(bucket.getCreationDate());
                    // S3 does not track access times, so this implementation always sets it to 0
                    return new S3FileStatus(f, 0L, true, creationDate, 0L);
                }
            }

            throw new FileNotFoundException("Cannot find " + f.toUri());
        }

        try {
            final ObjectMetadata om = this.s3Client.getObjectMetadata(bop.getBucket(), bop.getObject());
            final long modificationDate = dateToLong(om.getLastModified());
            // S3 does not track access times, so this implementation always sets it to 0
            if (objectRepresentsDirectory(bop.getObject(), om.getContentLength())) {
                return new S3FileStatus(f, 0L, true, modificationDate, 0L);
            } else {
                return new S3FileStatus(f, om.getContentLength(), false, modificationDate, 0L);
            }

        } catch (AmazonServiceException e) {
            if (e.getStatusCode() == HTTP_RESOURCE_NOT_FOUND_CODE) {
                throw new FileNotFoundException("Cannot find " + f.toUri());
            } else {
                throw e;
            }
        }
    } catch (AmazonClientException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }
}

From source file:eu.stratosphere.runtime.fs.s3.S3FileSystem.java

License:Apache License

@Override
public boolean mkdirs(final Path f) throws IOException {

    final S3BucketObjectPair bop = this.directoryStructure.toBucketObjectPair(f);
    if (!bop.hasBucket() && !bop.hasObject()) {
        // Ignore this call
        return false;
    }/*from  ww  w . ja v  a  2  s . co  m*/

    boolean retCode = false;

    try {

        // Make sure the bucket exists
        if (bop.hasBucket()) {
            if (this.s3Client.doesBucketExist(bop.getBucket())) {
            } else {
                this.s3Client.createBucket(bop.getBucket());
                retCode = true;
            }
        }

        if (bop.hasObject()) {

            // Make sure object name ends with a directory separator character
            String object = bop.getObject();
            if (!object.isEmpty()) {
                if (object.charAt(object.length() - 1) != S3_DIRECTORY_SEPARATOR) {
                    object = object.concat(Character.toString(S3_DIRECTORY_SEPARATOR));
                }
            }

            while (true) {

                try {
                    this.s3Client.getObjectMetadata(bop.getBucket(), object);
                } catch (AmazonServiceException e) {
                    if (e.getStatusCode() == HTTP_RESOURCE_NOT_FOUND_CODE) {
                        createEmptyObject(bop.getBucket(), object);

                        if (object.length() > 1) {
                            final int nextPos = object.lastIndexOf(S3_DIRECTORY_SEPARATOR, object.length() - 2);
                            if (nextPos >= 0) {
                                object = object.substring(0, nextPos + 1);
                                continue;
                            }
                        }

                    } else {
                        // Rethrow the exception
                        throw e;
                    }
                }

                // Object already exists, exit
                break;
            }
        }
    } catch (AmazonClientException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }

    return retCode;
}

From source file:example.uploads3.UploadS3.java

License:Apache License

public static void main(String[] args) throws Exception {
    String uploadFileName = args[0];
    String bucketName = "haos3";
    String keyName = "test/byspark.txt";
    // Create a Java Spark Context.
    SparkConf conf = new SparkConf().setAppName("UploadS3");
    JavaSparkContext sc = new JavaSparkContext(conf);

    AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
    try {//from  w  w  w.  java2s . c  o  m
        System.out.println("Uploading a new object to S3 from a file\n");
        File file = new File(uploadFileName);
        PutObjectRequest putRequest = new PutObjectRequest(bucketName, keyName, file);

        // Request server-side encryption.
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setServerSideEncryption("AES256");
        putRequest.setMetadata(objectMetadata);

        s3client.putObject(putRequest);

    } 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:exemplos.S3Sample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*//w  ww .j a v a2 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 = "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:fsi_admin.JAwsS3Conn.java

License:Open Source License

@SuppressWarnings("rawtypes")
private boolean subirArchivo(StringBuffer msj, AmazonS3 s3, String S3BUKT, String nombre, Vector archivos) {
    //System.out.println("AwsConn SubirArchivo:" + nombre + ":nombre");

    if (!archivos.isEmpty()) {
        FileItem actual = null;/*w  w w .  j ava 2 s  .  c  o  m*/

        try {
            for (int i = 0; i < archivos.size(); i++) {
                InputStream inputStream = null;
                try {
                    actual = (FileItem) archivos.elementAt(i);
                    /////////////////////////////////////////////////////////
                    //Obtain the Content length of the Input stream for S3 header
                    InputStream is = actual.getInputStream();
                    byte[] contentBytes = IOUtils.toByteArray(is);

                    Long contentLength = Long.valueOf(contentBytes.length);

                    ObjectMetadata metadata = new ObjectMetadata();
                    metadata.setContentLength(contentLength);

                    //Reobtain the tmp uploaded file as input stream
                    inputStream = actual.getInputStream();

                    //Put the object in S3
                    //System.out.println("BUCKET: " + S3BUKT + " OBJETO: " + nombre.replace('_', '-'));
                    //System.out.println("BUCKET: " + S3BUKT + " OBJETO: " + nombre.replace('_', '-'));
                    s3.putObject(new PutObjectRequest(S3BUKT, nombre, inputStream, metadata));
                } finally {
                    if (inputStream != null)
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }
                ////////////////////////////////////////////////////////////
            }
            return true;
        } catch (AmazonServiceException ase) {
            ase.printStackTrace();
            msj.append("Error de AmazonServiceException al subir archivo a S3.<br>");
            msj.append("Mensaje: " + ase.getMessage() + "<br>");
            msj.append("Cdigo de Estatus HTTP: " + ase.getStatusCode() + "<br>");
            msj.append("Cdigo de Error AWS:   " + ase.getErrorCode() + "<br>");
            msj.append("Tipo de Error:       " + ase.getErrorType() + "<br>");
            msj.append("Request ID:       " + ase.getRequestId());
            return false;
        } catch (AmazonClientException ace) {
            ace.printStackTrace();
            msj.append("Error de AmazonClientException al subir archivo a S3.<br>");
            msj.append("Mensaje: " + ace.getMessage());
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            msj.append("Error de Entrada/Salida al subir archivo a S3: " + e.getMessage());
            return false;
        }

    } else {
        msj.append("Error al subir archivo a la nube: No se envi ningun archivo");
        return false;
    }
}

From source file:fsi_admin.JAwsS3Conn.java

License:Open Source License

private boolean eliminarArchivo(StringBuffer msj, AmazonS3 s3, String S3BUKT, String nombre) {
    //System.out.println("AwsConn EliminarArchivo:" + nombre + ":nombre");

    try {/*  www  .j a  va  2  s  . c  o m*/
        //System.out.println("BUCKET: " + S3BUKT + " OBJETO: " + nombre.replace('_', '-'));
        //System.out.println("BUCKET: " + S3BUKT + " OBJETO: " + nombre.replace('_', '-'));
        s3.deleteObject(S3BUKT, nombre);
        return true;
    } catch (AmazonServiceException ase) {
        ase.printStackTrace();
        msj.append("Error de AmazonServiceException al eliminar archivo de S3.<br>");
        msj.append("Mensaje: " + ase.getMessage() + "<br>");
        msj.append("Cdigo de Estatus HTTP: " + ase.getStatusCode() + "<br>");
        msj.append("Cdigo de Error AWS:   " + ase.getErrorCode() + "<br>");
        msj.append("Tipo de Error:       " + ase.getErrorType() + "<br>");
        msj.append("Request ID:       " + ase.getRequestId());
        return false;
    } catch (AmazonClientException ace) {
        ace.printStackTrace();
        msj.append("Error de AmazonClientException al eliminar archivo de S3.<br>");
        msj.append("Mensaje: " + ace.getMessage());
        return false;
    }

}

From source file:fsi_admin.JAwsS3Conn.java

License:Open Source License

private boolean descargarArchivo(HttpServletResponse response, StringBuffer msj, AmazonS3 s3, String S3BUKT,
        String nombre, String destino) {
    //System.out.println("AwsConn DescargarArchivo:" + nombre + ":nombre");

    try {/*from w ww . j a v  a  2  s .c om*/
        System.out.println("DESCARGA BUCKET: " + S3BUKT + " OBJETO: " + nombre);
        S3Object object = s3.getObject(new GetObjectRequest(S3BUKT, nombre));
        //out.println("Content-Type: "  + object.getObjectMetadata().getContentType());
        //System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());
        byte[] byteArray = IOUtils.toByteArray(object.getObjectContent());
        ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
        JBajarArchivo fd = new JBajarArchivo();
        fd.doDownload(response, getServletConfig().getServletContext(), bais,
                object.getObjectMetadata().getContentType(), byteArray.length, destino);
        System.out.println("Content-Length: " + object.getObjectMetadata().getContentLength() + " BA: "
                + byteArray.length);
        return true;
    } catch (AmazonServiceException ase) {
        ase.printStackTrace();
        msj.append("Error de AmazonServiceException al descargar archivo de S3.<br>");
        msj.append("Mensaje: " + ase.getMessage() + "<br>");
        msj.append("Cdigo de Estatus HTTP: " + ase.getStatusCode() + "<br>");
        msj.append("Cdigo de Error AWS:   " + ase.getErrorCode() + "<br>");
        msj.append("Tipo de Error:       " + ase.getErrorType() + "<br>");
        msj.append("Request ID:       " + ase.getRequestId());
        return false;
    } catch (AmazonClientException ace) {
        ace.printStackTrace();
        msj.append("Error de AmazonClientException al descargar archivo de S3.<br>");
        msj.append("Mensaje: " + ace.getMessage());
        return false;
    } catch (IOException ace) {
        ace.printStackTrace();
        msj.append("Error de IOException al descargar archivo de S3.<br>");
        msj.append("Mensaje: " + ace.getMessage());
        return false;
    }

}

From source file:getting_started.GettingStartedApp.java

License:Open Source License

public static void main(String[] args) throws Exception {

    System.out.println("===========================================");
    System.out.println("Welcome to the AWS Java SDK!");
    System.out.println("===========================================");

    /*/* w w  w .jav a2  s .com*/
     * Amazon EC2
     *
     * The AWS EC2 client allows you to create, delete, and administer
     * instances programmatically.
     *
     * In this sample, we use an EC2 client to submit a Spot request,
     * wait for it to reach the active state, and then cancel and terminate
     * the associated instance.
     */
    try {
        // Setup the helper object that will perform all of the API calls.
        Requests requests = new Requests();

        // Submit all of the requests.
        requests.submitRequests();

        // Loop through all of the requests until all bids are in the active state 
        // (or at least not in the open state).
        do {
            // Sleep for 60 seconds.
            Thread.sleep(SLEEP_CYCLE);
        } while (requests.areAnyOpen());

        // Cancel all requests and terminate all running instances.
        requests.cleanup();

    } catch (AmazonServiceException ase) {
        // Write out any exceptions that may have occurred.
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Reponse Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());
    }
}