适用于PHP的AWS开发工具包-通过getIterator进行迭代并过滤响应

时间:2018-11-30 10:02:24

标签: amazon-s3 aws-php-sdk

我在S3存储桶中有50万张图像,并且图像的数量每天以35-40k的速度增长。这些图像遵循以下命名法:

est20181127-124627.jpg
est20181127-124628.jpg

名称中包含创建日期和时间。我正在使用AWS开发工具包FOR PHP与S3存储桶进行交互。当我提供以下代码时:

$objects = $s3->getIterator('ListObjects', ['Bucket' => $bucket]);
foreach ($objects as $object) {
    $urls = $s3->getObjectUrl(BUCKET, $object['Key']);
    echo $urls;
} 

我收到以下答复:

https://homesecmum.s3.ap-south-1.amazonaws.com/est20181127-124627.jpg https://homesecmum.s3.ap-south-1.amazonaws.com/est20181127-124628.jpg https://homesecmum.s3.ap-south-1.amazonaws.com/est20181127-124629.jpg https://homesecmum.s3.ap-south-1.amazonaws.com/est20181127-124630.jpg https://homesecmum.s3.ap-south-1.amazonaws.com/est20181127-124631.jpg https://homesecmum.s3.ap-south-1.amazonaws.com/est20181127-124632.jpg 

我正在尝试根据日期和时间模式过滤图像。我尝试了以下代码:

$stdate = '20181127-123556';
$endate = '20181127-123700';

$start = DateTime::createFromFormat('Ymj-His',$stdate);
$end   = DateTime::createFromFormat('Ymj-His',$endate);
$patternval = '/(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})-(?<hour>\d{2})(?<minute>\d{2})(?<second>\d{2})/';
$format = 'Ymj-His';
foreach ($objects as $object) {
    $matches=array();     
    preg_match($patternval, basename($s3->getObjectUrl(BUCKET, $object['Key'])), $matches);
    if ($matches) {
        $filedate = DateTime::createFromFormat($format, $matches[0]);
    }
    if ($filedate >= $start && $filedate <= $end) {
              $good_files[] = $s3->getObjectUrl(BUCKET, $object['Key']);
              print_r($good_files);   
          }
    } 

上面返回了以下内容:

Array
(
    [0] => https://homesecmum.s3.ap-south-1.amazonaws.com/est20181127-124627.jpg
)
Array
(
    [0] => https://homesecmum.s3.ap-south-1.amazonaws.com/est20181127-124627.jpg
    [1] => https://homesecmum.s3.ap-south-1.amazonaws.com/est20181127-124628.jpg
)

即,对于日期时间范围内的每个图像,foreach迭代并提供多个数组。但是,当我尝试使用此代码时:

for ($i = 0; $i < count($objects); $i++) {
    $matches=array();     
    preg_match($patternval, basename($s3->getObjectUrl(BUCKET, $object['Key'])), $matches);
    if ($matches) {
        $filedate = DateTime::createFromFormat($format, $matches[0]);
            }
    if ($filedate >= $start && $filedate <= $end) {
              $good_files[] = $s3->getObjectUrl(BUCKET, $object['Key']);
              print_r($good_files);  
          }
}

我遇到以下错误:

PHP Fatal error:  Cannot use object of type Generator as array in /var/www/html

0 个答案:

没有答案