Search in sources :

Example 81 with S3ObjectSummary

use of com.amazonaws.services.s3.model.S3ObjectSummary in project aws-doc-sdk-examples by awsdocs.

the class DeleteBucket2 method main.

public static void main(String[] args) {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        // Delete all objects from the bucket. This is sufficient
        // for unversioned buckets. For versioned buckets, when you attempt to delete objects, Amazon S3 inserts
        // delete markers for all objects, but doesn't delete the object versions.
        // To delete objects from versioned buckets, delete all of the object versions before deleting
        // the bucket (see below for an example).
        ObjectListing objectListing = s3Client.listObjects(bucketName);
        while (true) {
            Iterator<S3ObjectSummary> objIter = objectListing.getObjectSummaries().iterator();
            while (objIter.hasNext()) {
                s3Client.deleteObject(bucketName, objIter.next().getKey());
            }
            // and delete them.
            if (objectListing.isTruncated()) {
                objectListing = s3Client.listNextBatchOfObjects(objectListing);
            } else {
                break;
            }
        }
        // Delete all object versions (required for versioned buckets).
        VersionListing versionList = s3Client.listVersions(new ListVersionsRequest().withBucketName(bucketName));
        while (true) {
            Iterator<S3VersionSummary> versionIter = versionList.getVersionSummaries().iterator();
            while (versionIter.hasNext()) {
                S3VersionSummary vs = versionIter.next();
                s3Client.deleteVersion(bucketName, vs.getKey(), vs.getVersionId());
            }
            if (versionList.isTruncated()) {
                versionList = s3Client.listNextBatchOfVersions(versionList);
            } else {
                break;
            }
        }
        // After all objects and object versions are deleted, delete the bucket.
        s3Client.deleteBucket(bucketName);
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client couldn't
        // parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) Regions(com.amazonaws.regions.Regions) SdkClientException(com.amazonaws.SdkClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider)

Example 82 with S3ObjectSummary

use of com.amazonaws.services.s3.model.S3ObjectSummary in project aws-doc-sdk-examples by awsdocs.

the class MakingRequests method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        // Get a list of objects in the bucket, two at a time, and
        // print the name and size of each object.
        ListObjectsRequest listRequest = new ListObjectsRequest().withBucketName(bucketName).withMaxKeys(2);
        ObjectListing objects = s3Client.listObjects(listRequest);
        while (true) {
            List<S3ObjectSummary> summaries = objects.getObjectSummaries();
            for (S3ObjectSummary summary : summaries) {
                System.out.printf("Object \"%s\" retrieved with size %d\n", summary.getKey(), summary.getSize());
            }
            if (objects.isTruncated()) {
                objects = s3Client.listNextBatchOfObjects(objects);
            } else {
                break;
            }
        }
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) SdkClientException(com.amazonaws.SdkClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) Regions(com.amazonaws.regions.Regions) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary)

Example 83 with S3ObjectSummary

use of com.amazonaws.services.s3.model.S3ObjectSummary in project aws-doc-sdk-examples by awsdocs.

the class ListObjects method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "To run this example, supply the name of a bucket to list!\n" + "\n" + "Ex: ListObjects <bucket-name>\n";
    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucket_name = args[0];
    System.out.format("Objects in S3 bucket %s:\n", bucket_name);
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    ListObjectsV2Result result = s3.listObjectsV2(bucket_name);
    List<S3ObjectSummary> objects = result.getObjectSummaries();
    for (S3ObjectSummary os : objects) {
        System.out.println("* " + os.getKey());
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary)

Example 84 with S3ObjectSummary

use of com.amazonaws.services.s3.model.S3ObjectSummary in project XRTB by benmfaul.

the class Configuration method processDirectory.

public void processDirectory(AmazonS3Client s3, ObjectListing listing, String bucket) throws Exception {
    for (S3ObjectSummary objectSummary : listing.getObjectSummaries()) {
        long size = objectSummary.getSize();
        logger.info("*** Processing S3 {}, size: {}", objectSummary.getKey(), size);
        S3Object object = s3.getObject(new GetObjectRequest(bucket, objectSummary.getKey()));
        String bucketName = object.getBucketName();
        String keyName = object.getKey();
        GetObjectTaggingRequest request = new GetObjectTaggingRequest(bucketName, keyName);
        GetObjectTaggingResult result = s3.getObjectTagging(request);
        List<Tag> tags = result.getTagSet();
        String type = null;
        String name = null;
        if (tags.isEmpty()) {
            System.err.println("Error: " + keyName + " has no tags");
        } else {
            for (Tag tag : tags) {
                String key = tag.getKey();
                String value = tag.getValue();
                if (key.equals("type")) {
                    type = value;
                }
                if (key.equals("name")) {
                    name = value;
                }
            }
            if (name == null)
                throw new Exception("Error: " + keyName + " is missing a name tag");
            if (name.contains(" "))
                throw new Exception("Error: " + keyName + " has a name attribute with a space in it");
            if (type == null)
                throw new Exception("Error: " + keyName + " has no type tag");
            if (!name.startsWith("$"))
                name = "$" + name;
            readData(type, name, object, size);
        }
    }
}
Also used : GetObjectTaggingRequest(com.amazonaws.services.s3.model.GetObjectTaggingRequest) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) S3Object(com.amazonaws.services.s3.model.S3Object) GeoTag(com.xrtb.geo.GeoTag) Tag(com.amazonaws.services.s3.model.Tag) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) GetObjectTaggingResult(com.amazonaws.services.s3.model.GetObjectTaggingResult)

Example 85 with S3ObjectSummary

use of com.amazonaws.services.s3.model.S3ObjectSummary in project YCSB by brianfrankcooper.

the class S3Client method scanFromStorage.

/**
 * Perform an emulation of a database scan operation on a S3 bucket.
 *
 * @param bucket
 *            The name of the bucket
 * @param startkey
 *            The file key of the first file to read.
 * @param recordcount
 *            The number of files to read
 * @param fields
 *            The list of fields to read, or null for all of them
 * @param result
 *            A Vector of HashMaps, where each HashMap is a set field/value
 *            pairs for one file
 */
protected Status scanFromStorage(String bucket, String startkey, int recordcount, Vector<HashMap<String, ByteIterator>> result, SSECustomerKey ssecLocal) {
    int counter = 0;
    ObjectListing listing = s3Client.listObjects(bucket);
    List<S3ObjectSummary> summaries = listing.getObjectSummaries();
    List<String> keyList = new ArrayList();
    int startkeyNumber = 0;
    int numberOfIteration = 0;
    // getting the list of files in the bucket
    while (listing.isTruncated()) {
        listing = s3Client.listNextBatchOfObjects(listing);
        summaries.addAll(listing.getObjectSummaries());
    }
    for (S3ObjectSummary summary : summaries) {
        String summaryKey = summary.getKey();
        keyList.add(summaryKey);
    }
    // Sorting the list of files in Alphabetical order
    // sorting the list
    Collections.sort(keyList);
    // Getting the position of the startingfile for the scan
    for (String key : keyList) {
        if (key.equals(startkey)) {
            startkeyNumber = counter;
        } else {
            counter = counter + 1;
        }
    }
    // if not using the total number of Files
    if (recordcount < keyList.size()) {
        numberOfIteration = recordcount;
    } else {
        numberOfIteration = keyList.size();
    }
    // of the Files or Till the recordcount number
    for (int i = startkeyNumber; i < numberOfIteration; i++) {
        HashMap<String, ByteIterator> resultTemp = new HashMap<String, ByteIterator>();
        readFromStorage(bucket, keyList.get(i), resultTemp, ssecLocal);
        result.add(resultTemp);
    }
    return Status.OK;
}
Also used : ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) HashMap(java.util.HashMap) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary)

Aggregations

S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)196 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)106 ArrayList (java.util.ArrayList)64 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)61 Test (org.junit.Test)50 Date (java.util.Date)29 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)27 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)25 Test (org.testng.annotations.Test)25 AmazonS3 (com.amazonaws.services.s3.AmazonS3)23 S3Object (com.amazonaws.services.s3.model.S3Object)19 AmazonClientException (com.amazonaws.AmazonClientException)18 IOException (java.io.IOException)17 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)16 AmazonServiceException (com.amazonaws.AmazonServiceException)14 ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)14 File (java.io.File)13 HashMap (java.util.HashMap)13 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)13 StorageFile (org.finra.herd.model.api.xml.StorageFile)13