📅  最后修改于: 2023-12-03 15:12:11.271000             🧑  作者: Mango
Boto3是AWS( Amazon Web Services )的 SDK for Python,可与Amazon S3,Amazon DynamoDB等服务进行交互。在这个教程中,我们将会学会如何通过Boto3使用AWS S3服务来读取CSV文件。
你可以从以下链接中下载安装程序
! pip install boto3
为了能够使用AWS S3服务,我们需要设置AWS凭据授权,来实现访问Amazon S3服务的能力。
在 AWS 管理控制台 选择访问自己的安全证书管理模块;
创建 AWS programmatic access key:https://console.aws.amazon.com/iam/home?region=us-west-2#/security_credentials
第一个步骤就是导入Boto3和CSV库。CSV库可以帮助我们处理CSV文件。以下是所需的代码:
# Importing necessary libraries
import boto3
import csv
# Creating resource using environment variables
s3 = boto3.resource('s3',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
# Defining function that will help us to read csv file directly from S3
def get_data_from_s3(bucket_name, file_name):
try:
# Reading CSV directly from S3
obj = s3.Object(bucket_name, file_name)
response = obj.get()
rows = response['Body'].read().split().decode('utf-8').split('\n')
csv_reader = csv.reader(rows, delimiter=',', quotechar='"')
data = []
for row in csv_reader:
data.append(row)
return data
except Exception as e:
print('Error getting object from S3:', e)
# Now runnind above written function to get csv file data
bucket_name = 'my-s3-bucket'
file_name = 'my-sample.csv'
data = get_data_from_s3(bucket_name, file_name)
print("Data:", data)
现在你已经学会了如何使用Boto3访问AWS S3服务,使用 Python 读取 CSV 文件。希望这篇文章能够对你有所帮助!