Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
How to get information of S3 bucket?

Say for example I have the following bucket set up:

bucketone
…/folderone
    …/text1.txt
    …/text2.txt
…/foldertwo
    …/file1.json
…/folderthree
    …/folderthreesub
        …/file2.json
…/file3.json

But it only goes down one level.

What’s the proper way of retrieving information under a bucket?

Will be sure to accept/upvote answer.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Contrary to the way you'd think it will work, rsplit() actually returns the splits from left-right, even though it applies it right-to-left.

Therefore, you actually want to obtain the last element of the split:

filename = obj['Key'].rsplit('/', 1)[-1]

See: Python rsplit() documentation

Also, be careful of 'pretend directories' that might be created via the console. They are actually zero-length files the make the folder appear in the UI. Therefore, skip files with no name after the final slash.

Make those fixes and it works as desired:

import boto3
import os

s3client = boto3.client('s3')

for obj in s3client.list_objects_v2(Bucket='my-bucket')['Contents']:
   filename = obj['Key'].rsplit('/', 1)[-1]
   localfiledir = os.path.join('/tmp', filename) 
   if filename != '':
      s3client.download_file('my-bucket', obj['Key'], localfiledir)
over 4 years ago · Santiago Trujillo Denunciar

0

Whats wrong with just doing this from the CLI?

aws s3 cp s3://bucketing . --recursive
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda