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
Mejor responder más tarde que nunca. Hay cuatro pasos para obtener sus datos 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.
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)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á.