当 AWS Rekognition 抛出 "Invalid S3 object exception" 的错误时,通常是由于提供给 Rekognition 的 S3 对象无效或无法访问。以下是可能的解决方法和代码示例:
解决方法:
代码示例:
以下是使用 AWS SDK for Python(boto3)的代码示例,演示如何使用 Rekognition 分析 S3 图像:
import boto3
# 创建 Rekognition 客户端
rekognition = boto3.client('rekognition', region_name='us-west-2')
# 指定 S3 存储桶和图像文件名
bucket_name = 'your_bucket_name'
image_name = 'your_image.jpg'
# 指定图像在 S3 存储桶中的路径
image_path = 'path/to/image/' + image_name
try:
# 使用 DetectLabels 方法分析图像
response = rekognition.detect_labels(
Image={
'S3Object': {
'Bucket': bucket_name,
'Name': image_path
}
}
)
# 处理响应结果
labels = response['Labels']
for label in labels:
print(label['Name'], label['Confidence'])
except rekognition.exceptions.InvalidS3ObjectException as e:
print("Invalid S3 object: ", e)
请确保替换示例代码中的 "your_bucket_name"、"your_image.jpg" 和 "path/to/image/" 为您实际使用的 S3 存储桶名称、图像文件名和图像路径。