dongxing2015
2013-06-26 10:16
浏览 80
已采纳

Amazon AWS SDK PHP 2 - 按实例过滤标签?

I have been able to retrieve a full list of tags from all my EC2 instances using the PHP SDK but I'm struggling to filter the results down to a particular instance...

    // Collect instance information
    $sInstanceId = file_get_contents('http://169.254.169.254/latest/meta-data/instance-id'); // 'i-52da5b1f'
    $sAvailabilityZone = file_get_contents('http://169.254.169.254/latest/meta-data/placement/availability-zone'); // 'eu-west-1b'
    $sRegion = preg_replace('/^(.*)([0-9]{1})([a-zA-Z]{1})/', '$1$2', $sAvailabilityZone);

    use Aws\Common\Aws;
    use Aws\Ec2\Command\DescribeTags;
    use Aws\Common\Enum\Region;

    // Set up the global AWS factory
    $oAWS = Aws::factory(array(
            'key'    => CONST_AWS_ACCESS_KEY,
            'secret' => CONST_AWS_SECRET_KEY,
            'region' => $sRegion
    ));

    // Query EC2 for tags
    $oEC2Client = $oAWS->get('ec2');
    $oModel = $oEC2Client->describeTags()->toArray();

I've tried changing the call to describeTags to...

    $oModel = $oEC2Client->describeTags(array(
            "Filters" => array(
                    array("Name" => "resource-id", "Value" => $sInstanceId)
            )
    ))->toArray();

But that seems to make no difference. Could someone shed some light on this for me please?

图片转代码服务由CSDN问答提供 功能建议

我已经能够使用PHP SDK从我的所有EC2实例中检索完整的标签列表,但我是 努力将结果过滤到特定实例...

  //收集实例信息
 $ sInstanceId = file_get_contents('http://169.254.169.254/latest/ 元数据/实例的编号');  //'i-52da5b1f'
 $ sAvailabilityZone = file_get_contents('http://169.254.169.254/latest/meta-data/placement/availability-zone');  //'eu-west-1b'
 $ sRegion = preg_replace('/ ^(。*)([0-9] {1})([a-zA-Z] {1})/','$ 1  $ 2',$ sAvailabilityZone); 
 
使用Aws \ Common \ Aws; 
使用Aws \ Ec2 \ Command \ DescribeTags; 
使用Aws \ Common \ Enum \ Region; 
 
 //设置全局 AWS factory 
 $ oAWS = Aws :: factory(array(
'key'=> CONST_AWS_ACCESS_KEY,
'secret'=> CONST_AWS_SECRET_KEY,
'region'=> $ sRegion 
)); \  n 
 //为标签查询EC2 
 $ oEC2Client = $ oAWS-> get('ec2'); 
 $ oModel = $ oEC2Client-> describeTags() - > toArray(); 
   
 
 

我尝试将对describeTags的调用更改为...

  $ oModel = $ oEC2Client-> describeTags( 数组(
“过滤器”=>数组(
数组(“名称”=>“resource-id”,“值”=> $ sInstanceId)
)
)) - > toArray()  ; 
   
 
 

但这似乎没有任何区别。 有人可以为我解释一下吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanque19820925 2013-06-28 00:01
    已采纳

    The API docs for Ec2Client.describeTags show that Value should actually be Values and should be an array. Try the following:

    $oModel = $oEC2Client->describeTags(array(
        "Filters" => array(
            array("Name" => "resource-id", "Values" => array($sInstanceId))
        )
    ))->toArray();
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题