Loading S3 objects from a list of keys using Boto3

I have a list of keys that I retrieve from the cache, and I want to load related objects (files) from S3 without having to make a key request.

Assuming I have the following key array:

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'
]

I am trying to do something similar to this answer to another SO question, but it has changed like this:

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')

My ultimate goal is that I would like to avoid using the AWS API separately for each key in the list. I would also like to avoid having to pull out the entire bucket and filter / correct the full results.

+4
source share
1 answer

, , , n API, n - key_array. API- amazon s3 , . , 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
+5

Source: https://habr.com/ru/post/1666049/


All Articles