gpt4 book ai didi

python - 使用 boto 和 pandas 从 aws s3 读取 csv 文件

转载 作者:太空狗 更新时间:2023-10-30 00:22:43 26 4
gpt4 key购买 nike

我已经阅读了可用的答案 herehere这些都无济于事。

我正在尝试从 S3 存储桶中读取一个 csv 对象,并且已经能够使用以下代码成功读取数据。

srcFileName="gossips.csv"
def on_session_started():
print("Starting new session.")
conn = S3Connection()
my_bucket = conn.get_bucket("randomdatagossip", validate=False)
print("Bucket Identified")
print(my_bucket)
key = Key(my_bucket,srcFileName)
key.open()
print(key.read())
conn.close()

on_session_started()

但是,如果我尝试使用 pandas 作为数据框读取同一个对象,则会出现错误。最常见的是 S3ResponseError: 403 Forbidden

def on_session_started2():
print("Starting Second new session.")
conn = S3Connection()
my_bucket = conn.get_bucket("randomdatagossip", validate=False)
# url = "https://s3.amazonaws.com/randomdatagossip/gossips.csv"
# urllib2.urlopen(url)

for line in smart_open.smart_open('s3://my_bucket/gossips.csv'):
print line
# data = pd.read_csv(url)
# print(data)

on_session_started2()

我做错了什么?我在 python 2.7 上,不能使用 Python 3。

最佳答案

这是我成功从 S3 上的 csv 读取 df 所做的。

import pandas as pd
import boto3

bucket = "yourbucket"
file_name = "your_file.csv"

s3 = boto3.client('s3')
# 's3' is a key word. create connection to S3 using default config and all buckets within S3

obj = s3.get_object(Bucket= bucket, Key= file_name)
# get object and file (key) from bucket

initial_df = pd.read_csv(obj['Body']) # 'Body' is a key word

关于python - 使用 boto 和 pandas 从 aws s3 读取 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43355074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com