我怎样才能使 s3 bucket 子图像迭代通用
how can I make a s3 bucket sub images iteration generic
我在 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
在 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'])
您可以在存储桶中过滤该文件夹。例如:
#import boto3
s3 = boto3.resource('s3')
python_bucket = s3.Bucket('python')
for images in python_bucket.objects.filter(Prefix="boss/"):
print images.key
更新:
根据您最近的编辑,您可以遍历 bucket/folder 和 运行 您的脚本。这是一个更完整的代码段,应该适用于您的 Lambda 函数:
import boto3
def lambda_handler(event, context):
s3 = boto3.resource('s3')
images = ""
python_bucket = s3.Bucket('python')
#Here, you're going through each image in your bucket/folder.
for image in python_bucket.objects.filter(Prefix="boss/"):
images += image.key
return images
这是您可以做的最好的事情,将 s3 事件通知添加为 lambda 函数的触发器,并为您的对象前缀配置它 "boss/"你的情况
这里的前缀是"boss/"
然后在您的代码中更改您的存储桶和密钥:
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
在这个的帮助下,每当对象上传到您的 bucket/boss/ 路径时,您的代码将自动获取它并运行 它会在代码中进行处理吗
有了这个,您的 lambda 将不需要任何硬编码的存储桶和密钥字符串,并且将 运行 自动针对子文件夹 path.Further 中的图像上传,如果您只想处理图像,请添加filter 模式为 .jpg , .jpeg , .png 等
我在 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
在 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'])
您可以在存储桶中过滤该文件夹。例如:
#import boto3
s3 = boto3.resource('s3')
python_bucket = s3.Bucket('python')
for images in python_bucket.objects.filter(Prefix="boss/"):
print images.key
更新:
根据您最近的编辑,您可以遍历 bucket/folder 和 运行 您的脚本。这是一个更完整的代码段,应该适用于您的 Lambda 函数:
import boto3
def lambda_handler(event, context):
s3 = boto3.resource('s3')
images = ""
python_bucket = s3.Bucket('python')
#Here, you're going through each image in your bucket/folder.
for image in python_bucket.objects.filter(Prefix="boss/"):
images += image.key
return images
这是您可以做的最好的事情,将 s3 事件通知添加为 lambda 函数的触发器,并为您的对象前缀配置它 "boss/"你的情况
这里的前缀是"boss/"
然后在您的代码中更改您的存储桶和密钥:
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
在这个的帮助下,每当对象上传到您的 bucket/boss/ 路径时,您的代码将自动获取它并运行 它会在代码中进行处理吗
有了这个,您的 lambda 将不需要任何硬编码的存储桶和密钥字符串,并且将 运行 自动针对子文件夹 path.Further 中的图像上传,如果您只想处理图像,请添加filter 模式为 .jpg , .jpeg , .png 等