Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
read only particular json files from s3 buckets from multiple folders

am trying to scroll over all buckets in s3 and see if there is a prefix that matches and get into those folders and read the json files.

I have tried to get the folders that contain a prefix, but failing to enter them.

Code:

import boto3
bucket = ['test-eob', 'test-eob-images']
client = boto3.client('s3')
for i in bucket:
    result = client.list_objects(Bucket=i,Prefix = 'PROCESSED_BY/FILE_JSON', Delimiter='/')
    print(result)

Using this am getting the ones with prefix and fails when bucket doesnt have that prefix.

structure of test-eob , test-eob/PROCESSED_BY/FILE_JSON/*.json I have to read the json if only my prefix matches, else come out of the bucket.

Can anyone help me out here.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Try to catch the error(is it a KeyError?) when the bucket does not contain the prefix.

For example:

for i in bucket:
    try:
          result = client.list_objects(Bucket=i,Prefix = 'PROCESSED_BY/FILE_JSON', Delimiter='/')
          print(result)
    except KeyError:
          pass

To read the json, there are several ways. For example with json.loads() from the json module.

So for each object in the bucket:

content_object = s3.Object(bucket_name, file_name)
file_content = content_object.get()['Body'].read().decode('utf-8')
json_content = json.loads(file_content)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda