如何通过LastModified以亚马逊s3 API顺序列出对象?

时间:2015-01-06 09:53:24

标签: php amazon-s3

我需要列出来自亚马逊s3的对象,以便最新上传的对象应该列在最顶层?怎么做?

$result = $client->listObjects(array(
    // Bucket is required
    'Bucket' => 'string',
    'Delimiter' => 'string',
    'EncodingType' => 'string',
    'Marker' => 'string',
    'MaxKeys' => integer,
    'Prefix' => 'string',
));

上面没有选项进行排序?

以下是我的代码,

   $items = $this->get('my.aws_s3.client')->listObjects(array(
        'Bucket' => "my_bucket"
    ));


    foreach ($items as $i) {
        var_dump($i);
    }

以下是我的输出,

string 'my_bucket' (length=19)
string '' (length=0)
string '' (length=0)
string '1000' (length=4)
boolean true
array (size=1000)
  0 => 
    array (size=6)
      'Key' => string '1286319624.tar.gz' (length=26)
      'LastModified' => string '2010-10-05T23:00:50.000Z' (length=24)
      'ETag' => string '"9118de4f6a8e1e21be99d791108eaa57"' (length=34)
      'Size' => string '90460751' (length=8)
      'Owner' => 
        array (size=2)
          'ID' => string '917a9ca91589caf57633aee246d4263777366b4d00aafa9ac3948910f6eb686f' (length=64)
          'DisplayName' => string 'dbrckll' (length=7)
      'StorageClass' => string 'STANDARD' (length=8)
  1 => 
    array (size=6)
      'Key' => string '1286406023.tar.gz' (length=26)
      'LastModified' => string '2010-10-06T23:00:50.000Z' (length=24)
      'ETag' => string '"e0e94040702eb9e18a906140fb706767"' (length=34)
      'Size' => string '90460758' (length=8)
      'Owner' => 
        array (size=2)
          'ID' => string '917a9ca91589caf57633aee246d4263777366b4d00aafa9ac3948910f6eb686f' (length=64)
          'DisplayName' => string 'dbrckll' (length=7)
      'StorageClass' => string 'STANDARD' (length=8)
  2 => 

如果您看到LastModified   '上次更改时间' =>字符串' 2010-10-05T23:00:50.000Z'先显示然后显示    '上次更改时间' =>字符串' 2010-10-06T23:00:50.000Z'

如何按LastModified的降序对其进行排序?

1 个答案:

答案 0 :(得分:0)

There是使用Spl迭代器的建议,所以实际上它实际上取决于你的实现。这是我使用迭代器的实现:

    $iterator = $client->getListObjectsIterator([
        'Bucket' => 'string',
        'Prefix' => 'string',
        'MAX' => 1000,
    ]);

    $arrayIterator = new \ArrayIterator($iterator->toArray());
    $arrayIterator->uasort(function($a, $b){
        $itemADate = (new DateTime($a['LastModified']));
        $itemBDate = (new DateTime($b['LastModified']));

        return $itemADate->diffInSeconds($itemBDate, false) > 0;
    });

* DateTime只是Carbon实施