首页 > 解决方案 > 按关键字搜索 S3 文件夹中的图像

问题描述

使用英文关键字从 S3 搜索和获取图像列表的最佳和简单方法是什么。还是我必须使用 Rekognition 将所有图像元数据存储到数据库中?

我的开发是使用 PHP。

标签: phpamazon-s3amazon-rekognition

解决方案


<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$bucket = '*** Your Bucket Name ***';

// Instantiate the client.
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1'
]);

// Use the high-level iterators (returns ALL of your objects).
try {
    $objects = $s3->getPaginator('ListObjects', [
        'Bucket' => $bucket
    ]);

    echo "Keys retrieved!" . PHP_EOL;
    foreach ($objects as $object) {
        echo $object['Key'] . PHP_EOL;
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

// Use the plain API (returns ONLY up to 1000 of your objects).
try {
    $result = $s3->listObjects([
        'Bucket' => $bucket
    ]);

    echo "Keys retrieved!" . PHP_EOL;
    foreach ($result['Contents'] as $object) {
        echo $object['Key'] . PHP_EOL;
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

因此此代码将返回您存储桶中的所有对象,您可以添加仅当密钥包含扩展名“jpg”、“jpeg”和“png”的逻辑,然后只需打印对象的密钥/名称


推荐阅读