Example usage for com.amazonaws.services.s3.model S3ObjectSummary getKey

List of usage examples for com.amazonaws.services.s3.model S3ObjectSummary getKey

Introduction

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

Prototype

public String getKey() 

Source Link

Document

Gets the key under which this object is stored in Amazon S3.

Usage

From source file:com.jfixby.scarabei.red.aws.test.S3Sample.java

License:Open Source License

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

    /*//w  w w. j  ava  2 s .  c  om
     * 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.jktheunique.aws.util.S3Util.java

License:Open Source License

public static void deleteBucket() {
    String name = Constants.S3_BUCKET_NAME.toLowerCase(Locale.US);
    List<S3ObjectSummary> objData = s3Client.listObjects(name).getObjectSummaries();
    if (objData.size() > 0) {
        DeleteObjectsRequest emptyBucket = new DeleteObjectsRequest(name);
        List<KeyVersion> keyList = new ArrayList<KeyVersion>();
        for (S3ObjectSummary summary : objData) {
            keyList.add(new KeyVersion(summary.getKey()));
        }/*from w w  w .j  a va  2  s  .co  m*/
        emptyBucket.withKeys(keyList);
        s3Client.deleteObjects(emptyBucket);
    }
    s3Client.deleteBucket(name);
}

From source file:com.jktsoftware.amazondownloader.download.S3TypeBucket.java

License:Open Source License

public List<IObject> getObjectsInRepo() {
    String repoid = getRepoId();/*from   ww w  .  jav a 2 s .c  o  m*/
    AWSCredentials awscredentials = new BasicAWSCredentials(this.credentials.getAccessKey(),
            this.credentials.getSecretAccessKey());

    AmazonS3 s3 = new AmazonS3Client(awscredentials);
    s3.setEndpoint(endpoint);

    System.out.println("Getting objects");
    ObjectListing objectListing = s3.listObjects(repoid);

    List<IObject> objects = new ArrayList<IObject>();

    for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
        S3TypeObject obj = new S3TypeObject(objectSummary.getKey(), objectSummary.getSize(),
                objectSummary.getBucketName(), objectSummary.getStorageClass(), s3);
        objects.add(obj);
    }
    return objects;
}

From source file:com.liferay.amazontools.S3Cleaner.java

License:Open Source License

protected void deleteBucket(String bucketName) {
    ObjectListing objectListing = amazonS3Client.listObjects(bucketName);

    List<S3ObjectSummary> objectSummaries = objectListing.getObjectSummaries();

    if (!objectSummaries.isEmpty()) {
        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);

        List<DeleteObjectsRequest.KeyVersion> keyVersions = new ArrayList<>();

        for (S3ObjectSummary objectSummary : objectSummaries) {
            keyVersions.add(new DeleteObjectsRequest.KeyVersion(objectSummary.getKey()));
        }//from w ww .  ja  v a2s  .  c o m

        deleteObjectsRequest.setKeys(keyVersions);

        amazonS3Client.deleteObjects(deleteObjectsRequest);
    }

    amazonS3Client.deleteBucket(bucketName);
}

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

License:Open Source License

@Override
public String[] getFileNames(long companyId, long repositoryId, String dirName) {

    String key = null;/*from   www .j a va 2  s  .  c om*/

    if (Validator.isNull(dirName)) {
        key = _s3KeyTransformer.getRepositoryKey(companyId, repositoryId);
    } else {
        key = _s3KeyTransformer.getDirectoryKey(companyId, repositoryId, dirName);
    }

    List<S3ObjectSummary> s3ObjectSummaries = getS3ObjectSummaries(key);

    Iterator<S3ObjectSummary> iterator = s3ObjectSummaries.iterator();

    String[] fileNames = new String[s3ObjectSummaries.size()];

    for (int i = 0; i < fileNames.length; i++) {
        S3ObjectSummary s3ObjectSummary = iterator.next();

        fileNames[i] = _s3KeyTransformer.getFileName(s3ObjectSummary.getKey());
    }

    return fileNames;
}

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

License:Open Source License

protected void deleteObjects(String prefix) {
    try {/*w w  w .j a  v a2  s  . co  m*/
        String[] keys = new String[_DELETE_MAX];

        List<S3ObjectSummary> s3ObjectSummaries = getS3ObjectSummaries(prefix);

        Iterator<S3ObjectSummary> iterator = s3ObjectSummaries.iterator();

        while (iterator.hasNext()) {
            DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(_bucketName);

            for (int i = 0; i < keys.length; i++) {
                if (iterator.hasNext()) {
                    S3ObjectSummary s3ObjectSummary = iterator.next();

                    keys[i] = s3ObjectSummary.getKey();
                } else {
                    keys = Arrays.copyOfRange(keys, 0, i);

                    break;
                }
            }

            deleteObjectsRequest.withKeys(keys);

            _amazonS3.deleteObjects(deleteObjectsRequest);
        }
    } catch (AmazonClientException ace) {
        throw transform(ace);
    }
}

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

License:Open Source License

protected String getHeadVersionLabel(long companyId, long repositoryId, String fileName)
        throws NoSuchFileException {

    String key = _s3KeyTransformer.getFileKey(companyId, repositoryId, fileName);

    List<S3ObjectSummary> s3ObjectSummaries = getS3ObjectSummaries(key);

    Iterator<S3ObjectSummary> iterator = s3ObjectSummaries.iterator();

    String[] keys = new String[s3ObjectSummaries.size()];

    for (int i = 0; i < keys.length; i++) {
        S3ObjectSummary s3ObjectSummary = iterator.next();

        keys[i] = s3ObjectSummary.getKey();
    }//from  w ww .  j  a  va  2 s .c om

    if (keys.length > 0) {
        Arrays.sort(keys);

        String headVersionKey = keys[keys.length - 1];

        int x = headVersionKey.lastIndexOf(CharPool.SLASH);

        return headVersionKey.substring(x + 1);
    }

    throw new NoSuchFileException(companyId, repositoryId, fileName);
}

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

License:Open Source License

protected void moveObjects(String oldPrefix, String newPrefix) throws DuplicateFileException {

    ObjectListing objectListing = _amazonS3.listObjects(_bucketName, newPrefix);

    List<S3ObjectSummary> newS3ObjectSummaries = objectListing.getObjectSummaries();

    if (!newS3ObjectSummaries.isEmpty()) {
        throw new DuplicateFileException(StringBundler
                .concat("Duplicate S3 object found when moving files from ", oldPrefix, " to ", newPrefix));
    }//www.  j a v  a 2  s  . c om

    List<S3ObjectSummary> oldS3ObjectSummaries = getS3ObjectSummaries(oldPrefix);

    for (S3ObjectSummary s3ObjectSummary : oldS3ObjectSummaries) {
        String oldKey = s3ObjectSummary.getKey();

        String newKey = _s3KeyTransformer.moveKey(oldKey, oldPrefix, newPrefix);

        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(_bucketName, oldKey, _bucketName, newKey);

        _amazonS3.copyObject(copyObjectRequest);
    }

    for (S3ObjectSummary objectSummary : oldS3ObjectSummaries) {
        String oldKey = objectSummary.getKey();

        DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(_bucketName, oldKey);

        _amazonS3.deleteObject(deleteObjectRequest);
    }
}

From source file:com.lithium.flow.filer.S3Filer.java

License:Apache License

@Override
@Nonnull//from  w w w .j  a  v  a2 s.  c  o  m
public List<Record> listRecords(@Nonnull String path) throws IOException {
    ObjectListing listing = s3
            .listObjects(new ListObjectsRequest().withBucketName(bucket).withPrefix(path.substring(1)));

    List<Record> records = Lists.newArrayList();
    for (S3ObjectSummary summary : listing.getObjectSummaries()) {
        File file = new File(summary.getKey());
        String parent = file.getParent();
        String name = file.getName();
        long time = summary.getLastModified().getTime();
        long size = summary.getSize();
        boolean directory = name.endsWith("/");
        records.add(new Record(uri, "/" + parent, name, time, size, directory));
    }
    return records;
}

From source file:com.mesosphere.dcos.cassandra.executor.backup.S3StorageDriver.java

License:Apache License

private static Map<String, Long> listSnapshotFiles(AmazonS3Client amazonS3Client, String bucketName,
        String backupName) {//  w  ww . j av a  2  s .com
    Map<String, Long> snapshotFiles = new HashMap<>();
    final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName)
            .withPrefix(backupName);
    ListObjectsV2Result result;
    do {
        result = amazonS3Client.listObjectsV2(req);
        for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
            snapshotFiles.put(objectSummary.getKey(), objectSummary.getSize());
        }
        req.setContinuationToken(result.getNextContinuationToken());
    } while (result.isTruncated());

    return snapshotFiles;
}