首页 > 解决方案 > 如何使 s3 存储桶子图像迭代通用

问题描述

我在 s3 上的存储桶被命名为 'python' ,它的子文件夹是 'boss' 。所以我想在 lambda 函数中获取文件夹老板的所有图像。目前我正在硬编码值,但将图像放在根目录中而不是子文件夹中。

bucket="python"
key="20180530105812.jpeg"

然后我想对所有图像一一调用这个函数

def lambda_handler(event, context):
    # Get the object from the event
    bucket="ais-django"
    key="20180530105812.jpeg"

    try:

        # Calls Amazon Rekognition IndexFaces API to detect faces in S3 object 
        # to index faces into specified collection

        response = index_faces(bucket, key)

        # Commit faceId and full name object metadata to DynamoDB

标签: pythonamazon-web-servicesamazon-s3aws-lambdaamazon-rekognition

解决方案


在 s3 客户端上使用 list_object 操作。

bucket="python"
client=boto3.client('s3')
response = client.list_objects(
    Bucket=bucket,
    Prefix='boss'
)
numberofobjects=len(response['Contents'])
for x in range(1, numberofobjects):
    try:
        response2=index_faces(bucket, response['Contents'][x]['Key'])

推荐阅读