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

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

Introduction

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

Prototype

public ObjectMetadata getObjectMetadata() 

Source Link

Document

Gets the metadata stored by Amazon S3 for this object.

Usage

From source file:org.openflamingo.fs.s3.S3DirectoryInfo.java

License:Apache License

public S3DirectoryInfo(String path, S3Object object) {
    this.path = path;
    this.fullyQualifiedPath = path;
    this.length = object.getObjectMetadata().getContentLength();
    this.modificationTime = object.getObjectMetadata().getLastModified().getTime();
    this.accesTime = object.getObjectMetadata().getLastModified().getTime();
}

From source file:org.openflamingo.fs.s3.S3ObjectInfo.java

License:Apache License

/**
 *  ??//  w w  w  .ja  v  a  2 s.c om
 *
 * @param object Amazon S3 Object
 */
public S3ObjectInfo(S3Object object, String filename) {
    this.fullyQualifiedPath = "/" + object.getBucketName() + "/" + object.getKey();
    this.filename = filename;
    this.path = FileUtils.getPath(this.fullyQualifiedPath);
    this.length = object.getObjectMetadata().getContentLength();
    this.modificationTime = object.getObjectMetadata().getLastModified().getTime();
    this.accesTime = object.getObjectMetadata().getLastModified().getTime();
}

From source file:org.openflamingo.fs.s3.S3Utils.java

License:Apache License

/**
 * Object  .// w w w  . j a  v  a2 s.  c o m
 *
 * @param client     Amazon S3 Client
 * @param bucketName Bucket Name
 */
public static Map<String, String> getObject(AmazonS3Client client, String bucketName, String objectKey) {
    S3Object object = client.getObject(bucketName, objectKey);
    ObjectMetadata objectMetadata = object.getObjectMetadata();

    Map<String, String> map = new HashMap<String, String>();

    if (!object.getKey().endsWith("/")) {
        String qualifiedPath = "/" + bucketName + "/" + object.getKey();
        map.put("bucketName", object.getBucketName());
        map.put("name", FileUtils.getFilename(qualifiedPath));
        map.put("path", qualifiedPath);
    } else {
        map.put("bucketName", object.getBucketName());
        map.put("name", object.getKey());
        map.put("name", "/" + bucketName + "/" + object.getKey());
    }

    setValue("redirectionLocation", object.getRedirectLocation(), map);
    setValue("version", objectMetadata.getVersionId(), map);
    setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
    setValue("contentType", objectMetadata.getContentType(), map);
    setValue("etag", objectMetadata.getETag(), map);
    setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
    setValue("contentLength", objectMetadata.getContentLength(), map);
    setValue("lastModified", objectMetadata.getLastModified(), map);
    return map;
}

From source file:org.openflamingo.fs.s3.S3Utils.java

License:Apache License

/**
 * Object  .//from w  ww.j  av a 2 s  .co m
 *
 * @param client     Amazon S3 Client
 * @param bucketName Bucket Name
 */
public static Map<String, String> getDirectory(AmazonS3Client client, String bucketName, String objectKey) {
    S3Object object = client.getObject(bucketName, objectKey);
    ObjectMetadata objectMetadata = object.getObjectMetadata();

    List<FileInfo> filesList = new ArrayList<FileInfo>();
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(object.getBucketName())
            .withPrefix(objectKey).withDelimiter("/");

    ObjectListing objectListing = null;

    do {
        objectListing = client.listObjects(listObjectsRequest);
        List<String> commonPrefixes = objectListing.getCommonPrefixes();
        List<S3ObjectSummary> summary = objectListing.getObjectSummaries();
        listObjectsRequest.setMarker(objectListing.getNextMarker());
    } while (objectListing.isTruncated());

    Map<String, String> map = new HashMap<String, String>();

    map.put("bucketName", object.getBucketName());
    map.put("name", object.getKey());
    map.put("redirectionLocation", object.getRedirectLocation());

    setValue("version", objectMetadata.getVersionId(), map);
    setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
    setValue("contentType", objectMetadata.getContentType(), map);
    setValue("etag", objectMetadata.getETag(), map);
    setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
    setValue("contentLength", objectMetadata.getContentLength(), map);
    setValue("lastModified", objectMetadata.getLastModified(), map);
    return null;
}

From source file:org.p365.S3Sample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*//from w w  w .j  ava 2s .c om
     * 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.springframework.integration.aws.s3.core.AmazonS3OperationsImpl.java

License:Apache License

public AmazonS3Object getObject(String bucketName, String folder, String objectName) {
    if (logger.isDebugEnabled())
        logger.debug("Getting from bucket " + bucketName + ", from folder " + folder + " the  object name "
                + objectName);//from   w w  w .  jav  a 2  s  . c  o  m
    GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
    S3Object s3Object = client.getObject(request);
    AmazonS3Object object = new AmazonS3Object(s3Object.getObjectMetadata().getUserMetadata(),
            s3Object.getObjectMetadata().getRawMetadata(), s3Object.getObjectContent(), null);
    return object;
}

From source file:org.springframework.integration.aws.s3.core.DefaultAmazonS3Operations.java

License:Apache License

/**
 * Gets the object from the given bucket with the given key using the AWS SDK implementation
 *
 * @param bucketName/*from  ww w.j a  v a 2  s.c  o  m*/
 * @param key
 * @return The Amazon S3 Object representing the Object in S3, may be null.
 */
@Override
protected AmazonS3Object doGetObject(String bucketName, String key) {
    GetObjectRequest request = new GetObjectRequest(bucketName, key);
    S3Object s3Object;
    try {
        s3Object = client.getObject(request);
    } catch (AmazonS3Exception e) {
        if ("NoSuchKey".equals(e.getErrorCode())) {
            //If the key is not found, return null rather than throwing the exception
            return null;
        } else {
            //throw the exception to caller in all other cases
            throw e;
        }
    }
    return new AmazonS3Object(s3Object.getObjectMetadata().getUserMetadata(),
            s3Object.getObjectMetadata().getRawMetadata(), s3Object.getObjectContent(), null);
}

From source file:org.systemsbiology.athero.SimpleStoreActivitiesS3Impl.java

License:Open Source License

/**
 * //from  w ww  .ja v a 2 s.  c o  m
 * @param bucketName
 *          Name of S3 bucket
 * @param remoteName
 *          Key to use for uploaded S3 object
 * @param localName
 *          Name of the file locally
 * @param toBox
 *          This is an output parameter here.  
 *          Used to communicate the name of the box that runs download activity
 * @return
 *      A Value object
 * @throws IOException 
 */

private String downloadFileFromS3(String bucketName, String remoteName, String localName) throws IOException {
    System.out.println("downloadFileFromS3 begin remoteName=" + remoteName + ", localName=" + localName);
    AmazonS3 storage = getS3Client();
    try {
        FileOutputStream f = new FileOutputStream(localName);
        try {
            S3Object obj = storage.getObject(bucketName, remoteName);
            InputStream inputStream = obj.getObjectContent();
            long totalSize = obj.getObjectMetadata().getContentLength();

            try {
                long totalRead = 0;
                int read = 0;
                byte[] bytes = new byte[1024];
                long lastHeartbeatTime = System.currentTimeMillis();
                while ((read = inputStream.read(bytes)) != -1) {
                    totalRead += read;
                    f.write(bytes, 0, read);
                    int progress = (int) (totalRead / totalSize * 100);
                    lastHeartbeatTime = heartbeat(lastHeartbeatTime, progress);
                }
            } finally {
                inputStream.close();
            }
        } finally {
            f.close();
        }
    } catch (AmazonServiceException e) {
        String message = "Failure downloading from S3";
        System.out.println(message);
        throw e;
    } catch (AmazonClientException e) {
        String message = "Failure downloading from S3";
        System.out.println(message);
        throw e;
    } catch (IOException e) {
        String message = "Failure downloading from S3";
        System.out.println(message);
        throw e;
    }
    // Return hostname file was downloaded to        
    System.out.println("downloadFileFromS3 done");
    return hostSpecificTaskList;
    //todo: remove after testing
}

From source file:org.weakref.s3fs.util.AmazonS3ClientMock.java

License:Apache License

@Override
public CopyObjectResult copyObject(String sourceBucketName, String sourceKey, String destinationBucketName,
        String destinationKey) throws AmazonClientException, AmazonServiceException {

    S3Element element = find(sourceBucketName, sourceKey);

    if (element != null) {

        S3Object objectSource = element.getS3Object();
        // copy object with
        S3Object resObj = new S3Object();
        resObj.setBucketName(destinationBucketName);
        resObj.setKey(destinationKey);//from  w  w  w  .ja va 2 s . co  m
        resObj.setObjectContent(objectSource.getObjectContent());
        resObj.setObjectMetadata(objectSource.getObjectMetadata());
        resObj.setRedirectLocation(objectSource.getRedirectLocation());
        // copy perission
        AccessControlList permission = new AccessControlList();
        permission.setOwner(element.getPermission().getOwner());
        permission.grantAllPermissions(element.getPermission().getGrants().toArray(new Grant[0]));
        // maybe not exists key TODO
        objects.get(find(destinationBucketName))
                .add(new S3Element(resObj, permission, sourceKey.endsWith("/")));

        return new CopyObjectResult();
    }

    throw new AmazonServiceException("object source not found");
}

From source file:org.zalando.stups.fullstop.controller.S3Controller.java

License:Apache License

@RequestMapping(method = RequestMethod.GET, value = "/download")
public void downloadFiles(@RequestParam(value = "bucket") final String bucket,
        @RequestParam(value = "location") final String location, @RequestParam(value = "page") final int page) {

    try {//from  w  w  w  . ja  v a 2 s  . c o  m
        log.info("Creating fullstop directory here: {}", fullstopLoggingDir);

        boolean mkdirs = new File(fullstopLoggingDir).mkdirs();
    } catch (SecurityException e) {
        // do nothing
    }

    AmazonS3Client amazonS3Client = new AmazonS3Client();
    amazonS3Client.setRegion(Region.getRegion(Regions
            .fromName((String) cloudTrailProcessingLibraryProperties.getAsProperties().get(S3_REGION_KEY))));

    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket) //
            .withPrefix(location) //
            .withMaxKeys(page);

    ObjectListing objectListing = amazonS3Client.listObjects(listObjectsRequest);

    final List<S3ObjectSummary> s3ObjectSummaries = objectListing.getObjectSummaries();

    while (objectListing.isTruncated()) {

        objectListing = amazonS3Client.listNextBatchOfObjects(objectListing);
        s3ObjectSummaries.addAll(objectListing.getObjectSummaries());

    }

    for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
        String bucketName = s3ObjectSummary.getBucketName();
        String key = s3ObjectSummary.getKey();

        S3Object object = amazonS3Client.getObject(new GetObjectRequest(bucketName, key));
        InputStream inputStream = object.getObjectContent();

        File file = new File(fullstopLoggingDir,
                object.getBucketName() + object.getObjectMetadata().getETag() + JSON_GZ);

        copyInputStreamToFile(inputStream, file);
        log.info("File saved here: {}", file.getAbsolutePath());

    }
}