AWS Storage Gateway User Guide
Deleting Snapshots
String marker = null;
do {
ListVolumesRequest request = new
ListVolumesRequest().withGatewayARN(gatewayARN);
ListVolumesResult result = sgClient.listVolumes(request);
marker = result.getMarker();
for (VolumeInfo vi : result.getVolumeInfos())
{
volumes.add(vi);
System.out.println(OutputVolumeInfo(vi));
}
} while (marker != null);
return volumes;
}
private static void DeleteSnapshotsForVolumes(List<VolumeInfo> volumes,
int daysBack2) {
// Find snapshots and delete for each volume
for (VolumeInfo vi : volumes) {
String volumeARN = vi.getVolumeARN();
String volumeId =
volumeARN.substring(volumeARN.lastIndexOf("/")+1).toLowerCase();
Collection<Filter> filters = new ArrayList<Filter>();
Filter filter = new Filter().withName("volume-id").withValues(volumeId);
filters.add(filter);
DescribeSnapshotsRequest describeSnapshotsRequest =
new DescribeSnapshotsRequest().withFilters(filters);
DescribeSnapshotsResult describeSnapshotsResult =
ec2Client.describeSnapshots(describeSnapshotsRequest);
List<Snapshot> snapshots = describeSnapshotsResult.getSnapshots();
System.out.println("volume-id = " + volumeId);
for (Snapshot s : snapshots){
StringBuilder sb = new StringBuilder();
boolean meetsCriteria = !CompareDates(daysBack, s.getStartTime());
sb.append(s.getSnapshotId() + ", " + s.getStartTime().toString());
sb.append(", meets criteria for delete? " + meetsCriteria);
sb.append(", deleted? ");
if (!viewOnly & meetsCriteria) {
sb.append("yes");
DeleteSnapshotRequest deleteSnapshotRequest =
new DeleteSnapshotRequest().withSnapshotId(s.getSnapshotId());
ec2Client.deleteSnapshot(deleteSnapshotRequest);
}
else {
sb.append("no");
}
System.out.println(sb.toString());
}
}
}
private static String OutputVolumeInfo(VolumeInfo vi) {
String volumeInfo = String.format(
"Volume Info:\n" +
" ARN: %s\n" +
" Type: %s\n",
vi.getVolumeARN(),
vi.getVolumeType());
return volumeInfo;
API Version 2013-06-30
165