aws Java sdk 如何设置标签到卷
Aws Java sdk how to set tags to volume
我正在尝试使用标签创建 ec2 卷
//Create ec2 client using credentials
AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(creds).withRegion(region).build();
List<TagSpecification> ts = new ArrayList<TagSpecification>();
ts.add(new TagSpecification().withTags(new Tag("Name","HelloWorld")));
CreateVolumeRequest cvr = new CreateVolumeRequest(10,azone);
cvr.setVolumeType(VolumeType.Gp2);
cvr.setTagSpecifications(ts);
ec2.createVolume(cvr);
但是当我尝试执行上面的代码时它会抛出以下错误。
Exception in thread "main" com.amazonaws.services.ec2.model.AmazonEC2Exception: 'null' is not a valid taggable resource type for this operation. (Service: AmazonEC2; Status Code: 400; Error Code: InvalidParameterValue)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1588)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1258)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1030)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:742)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:716)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access0(AmazonHttpClient.java:667)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
at com.amazonaws.services.ec2.AmazonEC2Client.doInvoke(AmazonEC2Client.java:13611)
at com.amazonaws.services.ec2.AmazonEC2Client.invoke(AmazonEC2Client.java:13587)
at com.amazonaws.services.ec2.AmazonEC2Client.executeCreateVolume(AmazonEC2Client.java:3453)
at com.amazonaws.services.ec2.AmazonEC2Client.createVolume(AmazonEC2Client.java:3430)
at S3Driver.main(S3Driver.java:49)</p>
<p>Process finished with exit code 1
</pre>
以下代码应创建一个新卷并为其分配标签。
public void createVolumeWithTags() {
AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
.withRegion(Regions.US_WEST_2)
.build();
CreateVolumeRequest createVolumeRequest = new CreateVolumeRequest(10, "us-west-2a");
CreateVolumeResult createVolumeResult = amazonEC2.createVolume(createVolumeRequest);
ArrayList<Tag> instanceTags = new ArrayList<Tag>();
instanceTags.add(new Tag("Name", "HelloWorld"));
CreateTagsRequest createTagsRequest = new CreateTagsRequest().withTags(instanceTags).withResources(createVolumeResult.getVolume().getVolumeId());
amazonEC2.createTags(createTagsRequest);
}
上面的答案要求您首先创建卷,然后再次调用 EC2 API 以单独标记资源。虽然它确实有效,但对 API 的额外调用确实需要更多时间并创建额外的 exception/failure 场景。
以下代码只需对您的原始代码稍作调整即可实现您的目标,并且无需单独调用 ec2 api(标签在创建时应用)。唯一的变化是 - .withResourceType(ResourceType.Volume)
.
//Create ec2 client using credentials
AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(creds).withRegion(region).build();
List<TagSpecification> ts = new ArrayList<TagSpecification>();
ts.add(new TagSpecification().withTags(new Tag("Name","HelloWorld"))
.withResourceType(ResourceType.Volume));
CreateVolumeRequest cvr = new CreateVolumeRequest(10,azone);
cvr.setVolumeType(VolumeType.Gp2);
cvr.setTagSpecifications(ts);
ec2.createVolume(cvr);
进一步的简化是使用可变参数方法而不是创建列表(特别是如果您只设置一个标签)-
//Create ec2 client using credentials
AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(creds).withRegion(region).build();
TagSpecification ts = new TagSpecification()
.withTags(new Tag("Name","HelloWorld"))
.withResourceType(ResourceType.Volume);
CreateVolumeRequest cvr = new CreateVolumeRequest(10,azone)
.withVolumeType(VolumeType.Gp2);
.withTagSpecifications(ts);
ec2.createVolume(cvr);
我正在尝试使用标签创建 ec2 卷
//Create ec2 client using credentials
AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(creds).withRegion(region).build();
List<TagSpecification> ts = new ArrayList<TagSpecification>();
ts.add(new TagSpecification().withTags(new Tag("Name","HelloWorld")));
CreateVolumeRequest cvr = new CreateVolumeRequest(10,azone);
cvr.setVolumeType(VolumeType.Gp2);
cvr.setTagSpecifications(ts);
ec2.createVolume(cvr);
但是当我尝试执行上面的代码时它会抛出以下错误。
Exception in thread "main" com.amazonaws.services.ec2.model.AmazonEC2Exception: 'null' is not a valid taggable resource type for this operation. (Service: AmazonEC2; Status Code: 400; Error Code: InvalidParameterValue)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1588)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1258)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1030)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:742)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:716)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access0(AmazonHttpClient.java:667)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
at com.amazonaws.services.ec2.AmazonEC2Client.doInvoke(AmazonEC2Client.java:13611)
at com.amazonaws.services.ec2.AmazonEC2Client.invoke(AmazonEC2Client.java:13587)
at com.amazonaws.services.ec2.AmazonEC2Client.executeCreateVolume(AmazonEC2Client.java:3453)
at com.amazonaws.services.ec2.AmazonEC2Client.createVolume(AmazonEC2Client.java:3430)
at S3Driver.main(S3Driver.java:49)</p>
<p>Process finished with exit code 1
</pre>
以下代码应创建一个新卷并为其分配标签。
public void createVolumeWithTags() {
AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
.withRegion(Regions.US_WEST_2)
.build();
CreateVolumeRequest createVolumeRequest = new CreateVolumeRequest(10, "us-west-2a");
CreateVolumeResult createVolumeResult = amazonEC2.createVolume(createVolumeRequest);
ArrayList<Tag> instanceTags = new ArrayList<Tag>();
instanceTags.add(new Tag("Name", "HelloWorld"));
CreateTagsRequest createTagsRequest = new CreateTagsRequest().withTags(instanceTags).withResources(createVolumeResult.getVolume().getVolumeId());
amazonEC2.createTags(createTagsRequest);
}
上面的答案要求您首先创建卷,然后再次调用 EC2 API 以单独标记资源。虽然它确实有效,但对 API 的额外调用确实需要更多时间并创建额外的 exception/failure 场景。
以下代码只需对您的原始代码稍作调整即可实现您的目标,并且无需单独调用 ec2 api(标签在创建时应用)。唯一的变化是 - .withResourceType(ResourceType.Volume)
.
//Create ec2 client using credentials
AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(creds).withRegion(region).build();
List<TagSpecification> ts = new ArrayList<TagSpecification>();
ts.add(new TagSpecification().withTags(new Tag("Name","HelloWorld"))
.withResourceType(ResourceType.Volume));
CreateVolumeRequest cvr = new CreateVolumeRequest(10,azone);
cvr.setVolumeType(VolumeType.Gp2);
cvr.setTagSpecifications(ts);
ec2.createVolume(cvr);
进一步的简化是使用可变参数方法而不是创建列表(特别是如果您只设置一个标签)-
//Create ec2 client using credentials
AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withCredentials(creds).withRegion(region).build();
TagSpecification ts = new TagSpecification()
.withTags(new Tag("Name","HelloWorld"))
.withResourceType(ResourceType.Volume);
CreateVolumeRequest cvr = new CreateVolumeRequest(10,azone)
.withVolumeType(VolumeType.Gp2);
.withTagSpecifications(ts);
ec2.createVolume(cvr);