我正在使用下面的代码从 Amazon S3
读取一个 excel 文件使用 python
xlrd
和 urllib
模块,但我得到 Forbidden
访问错误。我知道这是因为我没有通过AWS Access Key
和 AWS Secret Access Key
.我环顾四周,找到了一种将 key 作为参数传递的方法 urllib
但找不到一个例子。
import urllib.request
import xlrd
url = 'https://s3.amazonaws.com/bucket1/final.xlsx'
filecontent = urllib.request.urlopen(url).read()
workbook = xlrd.open_workbook(file_contents=filecontent)
worksheet = workbook.sheet_by_name(SheetName)
如何使用 python xlrd 模块从 S3 读取 excel?
最佳答案
这可以使用 boto API 来完成
import boto
import boto.s3.connection
from boto.s3.key import Key
import sys
import pandas as pd
try:
conn = boto.connect_s3(aws_access_key_id = your_access_key, aws_secret_access_key = your_secret_key)
bucket = conn.get_bucket('your_bucket')
print ("connected to AWS/s3")
except Exception as e:
print ("unable to connect to s3 - please check credentials")
print(e)
sys.exit(1)
destFileName = "/tmp/myFile.xlsx"
k = Key(bucket, "path_to_file_on_s3/sourceFile.xlsx")
k.get_contents_to_filename(destFileName)
df = pd.read_excel(destFileName, sheet_name=Sheet1)
print(df.head())
关于python - 使用python xlrd模块从S3读取excel表时访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50009698/