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