2013-03-12 54 views
1

我已經使用AWS的AWS API一段時間了,但不知何故我找不到如何刪除使用CreateImage請求創建的快照。AWS Java刪除使用CreateImage創建的快照

此請求將爲您提供包含圖像ID的圖像。 當你想刪除圖像,你可以取消註冊它給這個ID。 但我找不到如何刪除該圖像正在使用的快照。

我在這裏錯過了什麼嗎?

由於提前,

Giriel

PS:一些代碼來說明我的意思:

final CreateImageResult createAMIResult = AWS.ec2.createImage(new CreateImageRequest().withInstanceId(instanceID).withName(amiName).withNoReboot(noReboot)); 
final String imageId = createAMIResult.getImageId(); 

//After a while I want to remove it again 

AWS.ec2.deregisterImage(new DeregisterImageRequest(imageId)); 
//TODO: How to remove the snapshot?? 

回答

2

通過AWS開發者論壇已經搜查後,我找到了解決辦法是相當令人失望。

你必須檢查你的所有快照的描述,並刪除一個匹配你的圖像ID。 Created by CreateImage(i-xxxxxxxx) for ami-xxxxxxxx from vol-xxxxxxxx

因此,它與自己的形象標識的AMI-XXXXXXX部分匹配的問題:使用時創建圖像

說明具有以下格式。

編輯

當您使用AWS提供的複製功能此解決方案不起作用。 我的新解決方案基於這樣一個事實,即EBS卷作爲阻止設備添加到ami並可以訪問快照ID! 部分代碼如圖:

/** 
* Removes an ami and its snapshot. 
* @param amiID 
* @param snapshotID 
*/ 
public static void removeImage(final String amiID, final AmazonEC2 ec2) { 
    if (amiID != null) { 
     DescribeImagesResult result = ec2.describeImages(new DescribeImagesRequest().withImageIds(amiID).withOwners(owner)); 
     if (!result.getImages().isEmpty()) { 
      ec2.deregisterImage(new DeregisterImageRequest(amiID)); 
      for (BlockDeviceMapping blockingDevice : result.getImages().get(0).getBlockDeviceMappings()) { 
       if (blockingDevice.getEbs() != null) { 
        ec2.deleteSnapshot(new DeleteSnapshotRequest().withSnapshotId(blockingDevice.getEbs().getSnapshotId())); 
       } 
      } 
     } 
    } 
}