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

3.6K
Views
Escriba el archivo csv y guárdelo en S3 usando AWS Lambda (python)

Estoy tratando de escribir un archivo csv en un depósito S3 usando AWS Lambda, y para esto usé el siguiente código:

 data=[[1,2,3],[23,56,98]] with open("s3://my_bucket/my_file.csv", "w") as f: f.write(data)

Y esto genera el siguiente error:

 [Errno 2] No such file or directory: u's3://my_bucket/my_file.csv': IOError Traceback (most recent call last): File "/var/task/lambda_function.py", line 51, in lambda_handler with open("s3://my_bucket/my_file.csv", "w") as f: IOError: [Errno 2] No such file or directory: u's3://my_bucket/my_file.csv'

¿Puedo tener algo de ayuda con esto por favor?

PD: estoy usando python 2.7

Agradeciendotelo de antemano

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Mejor responder más tarde que nunca. Hay cuatro pasos para obtener sus datos en S3:

  • Llame al depósito S3
  • Cargue los datos en Lambda usando la biblioteca de solicitudes (si no la tiene instalada, tendrá que cargarla como una capa)
  • Escriba los datos en el archivo Lambda '/tmp'
  • Cargue el archivo en s3

Algo como esto:

 import csv import requests #all other apropriate libs already be loaded in lambda #properly call your s3 bucket s3 = boto3.resource('s3') bucket = s3.Bucket('your-bucket-name') key = 'yourfilename.txt' #you would need to grab the file from somewhere. Use this incomplete line below to get started: with requests.Session() as s: getfile = s.get('yourfilelocation') #Only then you can write the data into the '/tmp' folder. with open('/tmp/yourfilename.txt', 'w', newline='') as f: w = csv.writer(f) w.writerows(filelist) #upload the data into s3 bucket.upload_file('/tmp/yourfilename.txt', key)

Espero eso ayude.

over 4 years ago · Santiago Trujillo Report

0

No estoy al tanto de usar AWS Lambda, pero he estado usando Boto3 para hacer lo mismo. Es un código simple de pocas líneas.

 #Your file path will be something like this: #s3://<your_s3_bucket_name>/<Directory_name>/<File_name>.csv import boto3 BUCKET_NAME = '<your_s3_bucket_name>' PREFIX = '<Directory_name>/' s3 = boto3.resource('s3') obj = s3.Object(BUCKET_NAME, PREFIX + '<File_name>.csv') obj.put(Body=content)
over 4 years ago · Santiago Trujillo Report

0

with open("s3://my_bucket/my_file.csv", "w+") as f:

en vez de

 with open("s3://my_bucket/my_file.csv", "w") as f:

observe que la "w" ha cambiado a "w+", esto significa que escribirá en el archivo y, si no existe, lo creará.

over 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!