Example usage for com.amazonaws.services.s3.model S3Object getObjectContent

List of usage examples for com.amazonaws.services.s3.model S3Object getObjectContent

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model S3Object getObjectContent.

Prototype

public S3ObjectInputStream getObjectContent() 

Source Link

Document

Gets the input stream containing the contents of this object.

Usage

From source file:com.easarrive.image.thumbor.executer.service.impl.SQSNotificationHandlerForThumbor.java

License:Open Source License

private String getAccessUrl(S3Object s3Object) throws Exception {
    S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
    HttpRequestBase httpRequestBase = s3ObjectInputStream.getHttpRequest();
    URI uri = httpRequestBase.getURI();
    String accessUrl = uri.toString();
    return accessUrl;
}

From source file:com.eBilling.util.S3Example.java

void downloadfile(AWSCredentials credentials2) throws IOException {
    AmazonS3 s3client = new AmazonS3Client(credentials2);
    try {//from  w ww.j  av  a 2s.co m
        System.out.println("Downloading an object");
        S3Object s3object = s3client.getObject(new GetObjectRequest(bucketName, keyName));
        System.out.println("Content-Type: " + s3object.getObjectMetadata().getContentType());
        InputStream input = s3object.getObjectContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        while (true) {
            String line = reader.readLine();
            if (line == null)
                break;

            System.out.println("    " + line);
        }
        System.out.println();
    } 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:com.emc.vipr.s3.s3api.java

License:Open Source License

public static S3ObjectInputStream ReadObject(String S3_ACCESS_KEY_ID, String S3_SECRET_KEY, String S3_ENDPOINT,
        String S3_ViPR_NAMESPACE, String S3_BUCKET, String key) throws Exception {

    ViPRS3Client s3 = getS3Client(S3_ACCESS_KEY_ID, S3_SECRET_KEY, S3_ENDPOINT, S3_ViPR_NAMESPACE);
    // create the object in the demo bucket
    S3Object object = s3.getObject(S3_BUCKET, key);

    return object.getObjectContent();

}

From source file:com.emc.vipr.s3.sample._02_ReadObject.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // retrieve the key value from user
    System.out.println("Enter the object key:");
    String key = new BufferedReader(new InputStreamReader(System.in)).readLine();

    // read the object from the demo bucket
    S3Object object = s3.getObject(ViPRS3Factory.S3_BUCKET, key);

    // convert object to a text string
    BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
    String content = reader.readLine();

    // print object key/value and content for validation
    System.out.println(/*from   w  ww . ja  va  2s .  com*/
            String.format("object [%s/%s] content: [%s]", object.getBucketName(), object.getKey(), content));
}

From source file:com.emc.vipr.s3.sample._09_UpdateAppendExtensions.java

License:Open Source License

private static String readObject(ViPRS3Client s3, String bucketName, String key) throws IOException {
    // read the object from the demo bucket
    S3Object object = s3.getObject(bucketName, key);

    // convert object to a text string
    BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
    String content = reader.readLine();
    return content;
}

From source file:com.epam.dlab.module.aws.AdapterS3File.java

License:Apache License

/**
 * Open the source file and return reader.
 *
 * @throws AdapterException//w ww .  jav  a 2  s . c om
 */
private InputStream getFileStream() throws AdapterException {
    try {
        GetObjectRequest request = new GetObjectRequest(bucket, getCurrentFileName());
        S3Object object = clientS3.getObject(request);
        lastModificationDate = object.getObjectMetadata().getLastModified();
        return object.getObjectContent();
    } catch (Exception e) {
        throw new AdapterException("Cannot open file " + bucket + DELIMITER + getCurrentFileName() + ". "
                + e.getLocalizedMessage(), e);
    }
}

From source file:com.erudika.para.storage.AWSFileStore.java

License:Apache License

@Override
public InputStream load(String path) {
    if (StringUtils.startsWith(path, "/")) {
        path = path.substring(1);//from  w  w w.j  a  v a2s .c o m
    }
    if (!StringUtils.isBlank(path)) {
        S3Object file = s3.getObject(bucket, path);
        return file.getObjectContent();
    }
    return null;
}

From source file:com.eucalyptus.objectstorage.providers.s3.S3ProviderClient.java

License:Open Source License

@Override
public GetObjectResponseType getObject(final GetObjectType request) throws S3Exception {
    User requestUser = getRequestUser(request);
    OsgInternalS3Client internalS3Client = null;
    GetObjectRequest getRequest = new GetObjectRequest(request.getBucket(), request.getKey());
    try {/*from  ww  w .j a v a 2 s . com*/
        internalS3Client = getS3Client(requestUser);
        AmazonS3Client s3Client = internalS3Client.getS3Client();
        GetObjectResponseType reply = request.getReply();
        S3Object response;
        response = s3Client.getObject(getRequest);
        populateResponseMetadata(reply, response.getObjectMetadata());
        reply.setDataInputStream(response.getObjectContent());
        return reply;
    } catch (AmazonServiceException e) {
        LOG.debug("Error from backend", e);
        throw S3ExceptionMapper.fromAWSJavaSDK(e);
    }
}

From source file:com.eucalyptus.objectstorage.providers.s3.S3ProviderClient.java

License:Open Source License

@Override
public GetObjectExtendedResponseType getObjectExtended(final GetObjectExtendedType request) throws S3Exception {
    User requestUser = getRequestUser(request);
    OsgInternalS3Client internalS3Client = null;

    Boolean getMetaData = request.getGetMetaData();
    Long byteRangeStart = request.getByteRangeStart();
    Long byteRangeEnd = request.getByteRangeEnd();
    Date ifModifiedSince = request.getIfModifiedSince();
    Date ifUnmodifiedSince = request.getIfUnmodifiedSince();
    String ifMatch = request.getIfMatch();
    String ifNoneMatch = request.getIfNoneMatch();

    GetObjectRequest getRequest = new GetObjectRequest(request.getBucket(), request.getKey());
    if (byteRangeStart == null) {
        byteRangeStart = 0L;// w w  w.  j  ava 2s.c o  m
    }
    if (byteRangeEnd != null) {
        getRequest.setRange(byteRangeStart, byteRangeEnd);
    }
    if (getMetaData != null) {
        //Get object metadata
    }
    if (ifModifiedSince != null) {
        getRequest.setModifiedSinceConstraint(ifModifiedSince);
    }
    if (ifUnmodifiedSince != null) {
        getRequest.setUnmodifiedSinceConstraint(ifUnmodifiedSince);
    }
    if (ifMatch != null) {
        List matchList = new ArrayList();
        matchList.add(ifMatch);
        getRequest.setMatchingETagConstraints(matchList);
    }
    if (ifNoneMatch != null) {
        List nonMatchList = new ArrayList();
        nonMatchList.add(ifNoneMatch);
        getRequest.setNonmatchingETagConstraints(nonMatchList);
    }
    try {
        internalS3Client = getS3Client(requestUser);
        AmazonS3Client s3Client = internalS3Client.getS3Client();
        S3Object response = s3Client.getObject(getRequest);

        GetObjectExtendedResponseType reply = request.getReply();
        populateResponseMetadata(reply, response.getObjectMetadata());
        reply.setDataInputStream(response.getObjectContent());
        reply.setByteRangeStart(request.getByteRangeStart());
        reply.setByteRangeEnd(request.getByteRangeEnd());
        return reply;
    } catch (AmazonServiceException e) {
        LOG.debug("Error from backend", e);
        throw S3ExceptionMapper.fromAWSJavaSDK(e);
    }
}

From source file:com.example.S3Sample02.java

License:Open Source License

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

    /*//from w  w  w . j  av  a 2s . c  om
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (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 (~/.aws/credentials), and is in valid format.", e);
    }

    AmazonS3 s3 = new AmazonS3Client(credentials);

    //        AP_SOUTHEAST_2

    //       Region usWest2 = Region.getRegion(Regions.AP_SOUTHEAST_2 );
    //       s3.setRegion(usWest2);

    //        String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();

    String bucketName = "imos-test-data-1";

    String key = "MyObjectKey" + UUID.randomUUID();

    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()));

        System.out.println("done\n");

        /*
         * 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());

        System.out.println("done\n");

        /*
         * 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();
        System.out.println("done\n");

        /*
         * 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());
    }
}