1

我需要在我的 S3 上显示存储桶内容,并且我正在使用 Amazon 的 PHP SDK。我的代码很简单

 $objects = $s3->list_objects("mybucket",array("max-keys"=>5));
 var_dump($objects);

我从服务器得到的响应对我来说非常复杂 - 它的本质是

Object(CFResponse)[107]
    public 'header' => 
    array (size=11)
      'x-amz-id-2' => string      
    ...
    public 'body' => 
    object(CFSimpleXML)[106]
      public '@attributes' => 
        array (size=1)
          'ns' => string 'http://s3.amazonaws.com/doc/2006-03-01/' (length=39)
      public 'Name' => string 'cdneu.2yourfacecdn.com' (length=22)
      public 'Prefix' => 
        object(CFSimpleXML)[3]
      public 'Marker' => 
        object(CFSimpleXML)[105]
      public 'MaxKeys' => string '5' (length=1)
      public 'IsTruncated' => string 'true' (length=4)
      public 'Contents' => 
        array (size=5)
          0 => 
            object(CFSimpleXML)[104]
              ...
          1 => 
            object(CFSimpleXML)[103]
              ...
          2 => 
            object(CFSimpleXML)[102]
              ...
          3 => 
            object(CFSimpleXML)[101]
              ...
          4 => 
            object(CFSimpleXML)[100]
              ...
         public 'status' => int 200

我相信“内容”下的部分是我正在寻找的,但我如何访问它?我习惯于接收数组,在那里我可以弄清楚键是什么以及如何访问,但这对我来说很难,有什么猜测吗?

4

2 回答 2

1

试试这个以列出每个对象的关键元素:

$s3 = new AmazonS3();
$objects = $s3->list_objects("YOUR BUCKET NAME",array("max-keys"=>5));
foreach ($objects->body->Contents as $item){
    print_r($item->Key."");
}
于 2012-12-06T16:02:56.163 回答
0

您可以访问以下内容

$contents = $objects['Contents'];
于 2015-04-22T20:09:39.453 回答