Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

743
Views
Python/ Boto 3: ¿Cómo recuperar/descargar archivos de AWS S3?

En Python/Boto 3, descubrí que para descargar un archivo individualmente desde S3 a local puede hacer lo siguiente:

 bucket = self._aws_connection.get_bucket(aws_bucketname) for s3_file in bucket.list(): if filename == s3_file.name: self._downloadFile(s3_file, local_download_directory) break;

Y para descargar todos los archivos en un directorio elegido:

 else: bucket = self._aws_connection.get_bucket(aws_bucketname) for s3_file in bucket.list(): self._downloadFile(s3_file, local_download_directory)

Y la función auxiliar _downloadFile() :

 def _downloadFile(self, s3_file, local_download_destination): full_local_path = os.path.expanduser(os.path.join(local_download_destination, s3_file.name)) try: print "Downloaded: %s" % (full_local_path) s3_file.get_contents_to_filename(full_local_path)

Pero ambos no parecen estar funcionando. Usando Boto 3 y Python, me gustaría poder descargar todos los archivos, preferiblemente como un zip, en un directorio definido en S3 a mi local.

¿Qué podría estar haciendo mal y cuál es la implementación correcta de los parámetros?

Gracias de antemano, y me aseguraré de aceptar/votar la respuesta.

CÓDIGO DE ACTUALIZACIÓN : Getting an error: “AttributeError: 'S3' object has no attribute

 import sys import json import os import subprocess import boto3 from boto.s3.connection import S3Connection s3 = boto3.resource('s3') s3client = boto3.client('s3') #This works for bucket in s3.buckets.all(): print(bucket.name) def main(): #Getting an error: “AttributeError: 'S3' object has no attribute 'download'” s3client.download('testbucket', 'fileone.json', 'newfile') if __name__ == "__main__": main()
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Para descargar archivos de S3 a Local FS, utilice el método download_file()

 s3client = boto3.client('s3') s3client.download_file(Bucket, Key, Filename)

Si el objeto S3 es s3://mybucket/foo/bar/file.txt , entonces los argumentos serían

 Bucket --> mybucket Key --> foo/bar/file.txt Filename --> /local/path/file.txt

No hay ningún método para descargar el depósito completo. Una forma alternativa sería listar todos los objetos en el cubo y descargarlos individualmente como archivos.

 for obj in s3client.list_objects(Bucket='mybucket')['Contents']: try: filename = obj['Key'].rsplit('/', 1)[1] except IndexError: filename = obj['Key'] localfilename = os.path.join('/home/username/Downloads/', filename) # The local directory must exist. s3client.download_file('mybucket', obj['Key'], localfilename)

Nota: La respuesta de list_objects() se trunca a 1000 objetos. Use los marcadores en la respuesta para recuperar el resto de objetos en el depósito.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!