简体   繁体   English

Amazon AWS通过Java API创建EBS(弹性块存储)

[英]Amazon AWS creating EBS(Elastic block storage) through Java API

I am trying to find a way to create a new EBS and attach it to a running instance pro grammatically through the AWSJavaSDK. 我试图找到一种方法来创建一个新的EBS并通过AWSJavaSDK以编程方式将其附加到正在运行的实例。 I see ways to do this with command line tools and with rest based calls but no way through the SDK proper. 我看到了使用命令行工具和基于休息的调用来实现此目的的方法,但没有办法通过SDK。

You should be able to use createVolume to create the item. 您应该能够使用createVolume来创建项目。 That looks to return a CreateVolumeResult , which has a Volume object inside. 这看起来返回一个CreateVolumeResult ,里面有一个Volume对象。

You would then take the Volume returned from the createVolume call and attachVolume with a matching AttachVolumeRequest . 然后,您将采取Volume从返回createVolume通话和attachVolume具有匹配AttachVolumeRequest

This is all done after you create one of AWS AmazonEC2Client objects: documentation is all pulled from here. 这一切都是在您创建AWS AmazonEC2Client对象之后完成的: 文档全部从此处提取。

Workflow of the code would probably look like this (note: pseudo code is used and there may be a few more pieces to hook in but the workflow should look something like this) 代码的工作流程可能看起来像这样(注意:使用伪代码并且可能还有一些要挂钩但工作流应该看起来像这样)

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);

Please refer the following code to create the EBS volumes using java API. 请参阅以下代码以使用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...");
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM