Last active
October 26, 2015 17:52
-
-
Save ernesto-butto/1d316954968763bf2307 to your computer and use it in GitHub Desktop.
Amazon s3 Versioning Enabled folder and sub folders recover using AWS SDK for Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void restoreObject() { | |
// INPUT VARIABLES | |
String BUCKET = "bucket-name"; | |
// Adress to the root folder e.g. String key = "users/newhighglass"; | |
String key = "tmp"; | |
String dayBeforeDeletion = "25-Oct-2015"; // A day before the deletion | |
boolean previewMode = true; | |
// END OF INPUT VARIABLES | |
String NEW_LINE = System.getProperty("line.separator"); | |
long startTime = System.currentTimeMillis(); | |
AmazonS3 s3 = new AmazonS3Client(); | |
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy"); | |
int recovered = 0 ; | |
int totalNumberOfVersions = 0 ; | |
String summariesToDelete = "keyToDelete , versionToDelete , dateFromVersion " + NEW_LINE; | |
try { | |
Date dateDeleted = formatter.parse(dayBeforeDeletion); | |
VersionListing versionListing = s3.listVersions(BUCKET,key); | |
// Creates the complete S3VersionSummary list even if results are paginated | |
List<S3VersionSummary> s3VersionSummaryList = getS3VersionSummaries(s3, versionListing); | |
// for logging only | |
totalNumberOfVersions = s3VersionSummaryList.size(); | |
for (S3VersionSummary s3VersionSummary : s3VersionSummaryList){ | |
String keyToDelete = s3VersionSummary.getKey(); | |
String versionToDelete = s3VersionSummary.getVersionId(); | |
Date dateFromVersion = s3VersionSummary.getLastModified(); | |
if (s3VersionSummary.isLatest() && s3VersionSummary.isDeleteMarker() && dateFromVersion.after(dateDeleted)){ | |
if (!previewMode) { | |
DeleteVersionRequest deleteVersionRequest = new DeleteVersionRequest(BUCKET, keyToDelete, versionToDelete); | |
s3.deleteVersion(deleteVersionRequest); | |
} | |
summariesToDelete += keyToDelete + " , " + versionToDelete + " , " + dateFromVersion + NEW_LINE; | |
recovered++; | |
} | |
} | |
long stopTime = System.currentTimeMillis(); | |
long elapsedTime = stopTime - startTime; | |
System.out.print("Total recovered: " +recovered +NEW_LINE); | |
System.out.print("Total Versions: "+ totalNumberOfVersions + NEW_LINE); | |
System.out.print("Time Miliseconds: "+ elapsedTime + NEW_LINE ); | |
System.out.print(summariesToDelete + NEW_LINE); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
This gist is a java method I created on our platform after accidentally deleting about 21,000 images and files from several customers, we had versioning enabled.
We could not find a tool that was able to recover the last version of the folders and subfolders, there were plenty that recovered on file at a time using a graphic interface, or you could download all the versions of everything to your computer, neither of them was suitable in our case.
Here is the main java method that we wrote to recover the files using Amazon JAVA for S3.
You should modify the variables:
BUCKET
to point your bucket,key
to point to the folder root where the folder used to be,dayBeforeDeletion
should be the day before you deleted everything, andpreviewMode
set to true will give you a preview of the files to be recovered, otherwise it will recover your files.Please take into account that this method deletes from the version history of each files the last version, which is a delete marker, created after the date defined.
Deleting a delete marker from a file or folder (key) version history recovers the file, this statement is the central point of the method.
If you need help running this, or if you would like us to create a program with UI for this se case, please let us know, I hope it helps someone that experiences a situation like ours.