Here is some stuff to download all buckets, list them, list their contents.
//connection string
private static void dBConnection() {
app.setAwsCredentials(CONST.getAccessKey(), CONST.getSecretKey());
conn = new AmazonS3Client(app.getAwsCredentials());
app.setListOfBuckets(conn.listBuckets());
System.out.println(CONST.getConnectionSuccessfullMessage());
}
private static void downloadBucket() {
do {
for (S3ObjectSummary objectSummary : app.getS3Object().getObjectSummaries()) {
app.setBucketKey(objectSummary.getKey());
app.setBucketName(objectSummary.getBucketName());
if(objectSummary.getKey().contains(CONST.getDesiredKey())){
//DOWNLOAD
try
{
s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
s3Client.getObject(
new GetObjectRequest(app.getBucketName(),app.getBucketKey()),
new File(app.getDownloadedBucket())
);
} catch (IOException e) {
e.printStackTrace();
}
do
{
if(app.getBackUpExist() == true){
System.out.println("Converting back up file");
app.setCurrentPacsId(objectSummary.getKey());
passIn = app.getDataBaseFile();
CONVERT= new DataConversion(passIn);
System.out.println(CONST.getFileDownloadedMessage());
}
}
while(app.getObjectExist()==true);
if(app.getObjectExist()== false)
{
app.setNoObjectFound(true);
}
}
}
app.setS3Object(conn.listNextBatchOfObjects(app.getS3Object()));
}
while (app.getS3Object().isTruncated());
}
/----------------------------Extension Methods-------------------------------------/
//Unzip bucket after download
public static void unzipBucket() throws IOException {
unzip = new UnZipBuckets();
unzip.unZipIt(app.getDownloadedBucket());
System.out.println(CONST.getFileUnzippedMessage());
}
//list all S3 buckets
public static void listAllBuckets(){
for (Bucket bucket : app.getListOfBuckets()) {
String bucketName = bucket.getName();
System.out.println(bucketName + "\t" + StringUtils.fromDate(bucket.getCreationDate()));
}
}
//Get the contents from the auto back up bucket
public static void listAllBucketContents(){
do {
for (S3ObjectSummary objectSummary : app.getS3Object().getObjectSummaries()) {
if(objectSummary.getKey().contains(CONST.getDesiredKey())){
System.out.println(objectSummary.getKey() + "\t" + objectSummary.getSize() + "\t" + StringUtils.fromDate(objectSummary.getLastModified()));
app.setBackUpCount(app.getBackUpCount() + 1);
}
}
app.setS3Object(conn.listNextBatchOfObjects(app.getS3Object()));
}
while (app.getS3Object().isTruncated());
System.out.println("There are a total of : " + app.getBackUpCount() + " buckets.");
}
}