我正在做一个项目,在该项目中,我从S3存储桶中读取文件并删除所有NA值,然后将它们上传到其他S3存储桶中。我一直在看Lambda教程和示例代码,但是我很难理解它是如何工作的。
我的目标是读取S3存储桶中的任何文件,并使用Lambda函数,删除所有NA值,然后将它们上传到其他S3存储桶。但是我真的不明白发生了什么。我阅读了文档,但对我的理解不是很有帮助。
如何使以下代码从S3存储桶中读取CSV文件,然后删除所有NA值,然后将它们上传到新的S3存储桶中?
import json
import os
import boto3
import csv
def lambda_handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
file_key = record['s3']['object']['key']
s3 = boto3.client('s3')
csv_file = s3.get_object(Bucket=bucket, Key=file_key)
csv_content = csv_file['Body'].read().split(b'\n')
csv_data = csv.DictReader(csv_content)
任何链接到文档,或视频和建议,将不胜感激。
def upload_file(file_name, bucket, object_name=None):
"""Upload a file to an S3 bucket
:param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then file_name is used
:return: True if file was uploaded, else False
"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = file_name
# Upload the file
s3_client = boto3.client('s3')
try:
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True
import boto3
s3 = boto3.resource('s3')
s3.meta.client.download_file('mybucket', 'hello.txt', '/tmp/hello.txt')
现在,您只需以所需的任何方式处理这些csv文件,然后以高效的方式处理和上传到s3,这将是完全不同的主题。
这篇文章中有很多答案。如何使用boto将文件上传到S3存储桶中的目录
如果好奇,您也可以选中此复选框,以了解如何处理较大的文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句