使用Boto3按键列表下载S3对象

afi*_*ert 4 python amazon-s3 amazon-web-services boto3

我有一个我从缓存中检索的密钥列表,我想从S3下载相关的对象(文件),而不必为每个密钥发出请求.

假设我有以下几个键:

key_array = [
    '20160901_0750_7c05da39_INCIDENT_MANIFEST.json',
    '20161207_230312_ZX1G222ZS3_INCIDENT_MANIFEST.json',
    '20161211_131407_ZX1G222ZS3_INCIDENT_MANIFEST.json',
    '20161211_145342_ZX1G222ZS3_INCIDENT_MANIFEST.json',
    '20161211_170600_FA68T0303607_INCIDENT_MANIFEST.json'
]
Run Code Online (Sandbox Code Playgroud)

我试图在另一个SO问题上做类似于这个答案的事情,但是修改如下:

import boto3

s3 = boto3.resource('s3')

incidents = s3.Bucket(my_incident_bucket).objects(key_array)

for incident in incidents:
    # Do fun stuff with the incident body
    incident_body = incident['Body'].read().decode('utf-8')
Run Code Online (Sandbox Code Playgroud)

我的最终目标是,我希望避免单独为列表中的每个键点击AWS API.我还想避免不得不拉下整个桶并过滤/迭代完整的结果.

小智 5

我认为你要获得的最好的是n个 API调用,其中n是key_array中键的数量.除了前缀之外,s3的amazon API在基于密钥的服务器端过滤方面没有提供太多.以下是在n个 API调用中获取它的代码:

import boto3
s3 = boto3.client('s3')

for key in key_array:
    incident_body = s3.get_object(Bucket="my_incident_bucket", Key=key)['Body']

    # Do fun stuff with the incident body
Run Code Online (Sandbox Code Playgroud)