PHP动态图片库-将S3Bucket图片作为数组获取-不显示图片

时间:2018-12-07 05:17:02

标签: php amazon-s3 gallery

我正在根据文件名从S3存储桶动态获取图像,并尝试在网页上显示它们。该代码完美地填充了URL数组。但是,当我使用foreach函数通过将img src作为url提供来在php中构建html时,图像无法显示。

以下是代码:

$objects = json_decode(exec($pyexec) , true);
//echo gettype($objects);


foreach ($objects as $object) {
  $num = $s3->getObjectUrl(BUCKET, $object);
  //echo $num;
  $imgname = basename($num);

   echo '<div class="col-xs-6 col-sm-2 ">';
    echo '<img src="'.$num.'" id="thumbNails'.$imgname.'" width="200" height="200" class="img" alt="Generic placeholder thumbnail" /></div>';
}

在此gettype($objects)中显示它是一个数组。当我使用print_r($objects)时,它会显示以下内容:

Array
(
    [0] => 1205944447_1544090338.044875.jpg
    [1] => 1205944447_1544090338.456757.jpg
    [2] => 1205944447_1544090338.870485.jpg
    [3] => 1205944447_1544090340.111561.jpg
)

在foreach中,当我回显URL时,得到以下信息:

https://myawsbuck123.s3.ap-south-1.amazonaws.com/1205944447_1544090338.044875.jpghttps://myawsbuck123.s3.ap-south-1.amazonaws.com/1205944447_1544090338.456757.jpghttps://myawsbuck123.s3.ap-south-1.amazonaws.com/1205944447_1544090338.870485.jpghttps://myawsbuck123.s3.ap-south-1.amazonaws.com/1205944447_1544090340.111561.jpg

从控制台运行它时,没有在网页上填充foreach函数,而是得到以下输出:

<div class="col-xs-6 col-sm-2 "><img src="https://myawsbuck123.s3.ap-south-1.amazonaws.com/1205944447_1544090338.044875.jpg" id="thumbNails1205944447_1544090338.044875.jpg" width="200" height="200" class="img" alt="Generic placeholder thumbnail" /></div><div class="col-xs-6 col-sm-2 "><img src="https://myawsbuck123.s3.ap-south-1.amazonaws.com/1205944447_1544090338.456757.jpg" id="thumbNails1205944447_1544090338.456757.jpg" width="200" height="200" class="img" alt="Generic placeholder thumbnail" /></div><div class="col-xs-6 col-sm-2 "><img src="https://myawsbuck123.s3.ap-south-1.amazonaws.com/1205944447_1544090338.870485.jpg" id="thumbNails1205944447_1544090338.870485.jpg" width="200" height="200" class="img" alt="Generic placeholder thumbnail" /></div><div class="col-xs-6 col-sm-2 "><img src="https://myawsbuck123.s3.ap-south-1.amazonaws.com/1205944447_1544090340.111561.jpg" id="thumbNails1205944447_1544090340.111561.jpg" width="200" height="200" class="img" alt="Generic placeholder thumbnail" /></div>

对我来说,它们看起来都还不错。不确定为什么它是图片,无法在网页上看到。我已经检查了S3存储桶的权限-他们对存储桶以及存储桶中的图像都启用了公共访问。但是,当我尝试从本地目录填充图像时,执行类似的代码不会遇到麻烦。如何显示S3存储桶中的图像,URL是否正确?

编辑: 我正在使用python查询dynamodb表以获取图像的名称,然后尝试根据图像和存储桶名称获取图像url。当我刚转储print_r($objects)时,没有错误。但是,当我使用PHP制作图库时,出现以下boto3&php错误。

Traceback (most recent call last):
  File "filterImages.py", line 2, in <module>
    import boto3
  File "/usr/local/lib/python2.7/dist-packages/boto3/__init__.py", line 16, in <module>
    from boto3.session import Session
  File "/usr/local/lib/python2.7/dist-packages/boto3/session.py", line 17, in <module>
    import botocore.session
ImportError: No module named botocore.session
[Fri Dec 07 11:00:51.507209 2018] [:error] [pid 6767] [client ::1:64844] PHP Warning:  Invalid argument supplied for foreach() in /var/www/html/getImages.php on line 48, referer: http://localhost/images.php

0 个答案:

没有答案