這裏我試圖列出可用的實例。但是,當我運行代碼時,我收到錯誤消息爲「無法通過區域提供程序鏈查找區域。必須在構建器或安裝環境中提供顯式區域以提供區域。」AWS Java SDK EC2描述實例拋出錯誤
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
@Controller
public class EC2InstancesController {
@RequestMapping(value="/getAllInstances", produces = {"application/json"},
consumes = {"application/json"}, method = RequestMethod.GET)
public List<Instance> getAllInstances(){
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
boolean done = false;
while(!done) {
DescribeInstancesRequest request = new DescribeInstancesRequest();
DescribeInstancesResult response = ec2.describeInstances(request);
for(Reservation reservation : response.getReservations()) {
for(Instance instance : reservation.getInstances()) {
System.out.printf(
"Found reservation with id %s, " +
"AMI %s, " +
"type %s, " +
"state %s " +
"and monitoring state %s",
instance.getInstanceId(),
instance.getImageId(),
instance.getInstanceType(),
instance.getState().getName(),
instance.getMonitoring().getState());
}
}
request.setNextToken(response.getNextToken());
if(response.getNextToken() == null) {
done = true;
}
}
return null;
}
}
當我試着打從郵遞員端點我收到以下錯誤
{
"timestamp": 1498107927641,
"status": 500,
"error": "Internal Server Error",
"exception": "com.amazonaws.SdkClientException",
"message": "Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.",
"path": "/getAllInstances"
}
我還添加以下代碼在上面,但還是從郵差其餘客戶端收到同樣的錯誤信息。
Region region = Region.getRegion(Regions.US_EAST_1);
ec2.setRegion(region);
有人可以幫我解決這個問題。謝謝..
哪個版本的JAVA AWS SDK? –
版本 - 1.11.150。 – Nithyananth