3

I am trying to find a way to create a new EBS and attach it to a running instance pro grammatically through the AWSJavaSDK. I see ways to do this with command line tools and with rest based calls but no way through the SDK proper.

4

3 回答 3

6

您应该能够使用createVolume来创建项目。这看起来会返回一个CreateVolumeResult,它里面有一个Volume对象。

然后,您将从调用中Volume返回并使用匹配的AttachVolumeRequest进行attachVolumecreateVolume

这一切都是在您创建 AWSAmazonEC2Client对象之一之后完成的:文档都是从这里提取的。

代码的工作流程可能看起来像这样(注意:使用了伪代码,可能还有更多的部分要挂钩,但工作流程应该看起来像这样)

AWSCredentials credentials = new AWSCredentials();
AmazonEC2Client client = new AmazonEC2Client(credentials);
CreateVolumeResult request = new  CreateVolumeRequest(java.lang.Integer size,
                       java.lang.String availabilityZone);
CreateVolumeResponse volumeResponse = client.createVolume(request);
AttachVolumeRequest attachRequest = new AttachVolumeRequest(volumeResponse.getVolume().getVolumeId(),  java.lang.String instanceId, java.lang.String device);
client.attachVolume(attachRequest);
于 2013-04-23T20:10:36.587 回答
1

请参考以下代码以使用 java API 创建 EBS 卷。

public void createVolume(String instanceId){
    System.out.println("Creating the volume begins...");
    CreateVolumeRequest  creq = new CreateVolumeRequest(50, "us-west-2a");
    CreateVolumeResult cres =  ec2.createVolume(creq);

     // Create the list of tags we want to create
    System.out.println("Setting the tags to the volume...");
    ArrayList<Tag> instanceTags = new ArrayList<Tag>();
    instanceTags.add(new Tag("Name","Sachin"));
    CreateTagsRequest createTagsRequest = new CreateTagsRequest().withTags(instanceTags).withResources(cres.getVolume().getVolumeId());
    ec2.createTags(createTagsRequest);
    System.out.println("Attaching the volume to the instance....");
    AttachVolumeRequest areq = new AttachVolumeRequest(cres.getVolume().getVolumeId(),instanceId, "/dev/sdh");
    AttachVolumeResult ares = ec2.attachVolume(areq);
    System.out.println("Creating the volume ends...");
}
于 2014-08-18T10:19:14.090 回答
0

使用CreateVolumeRequestapi 中的对象创建您的请求,并按照此处CreateVolumeResponce的说明在返回的对象中查看结果

于 2013-04-23T20:10:42.893 回答