我使用以下代码使用Amazon S3
python
和xlrd
模块从urllib
读取excel文件,但我收到Forbidden
访问错误。我知道这是因为我没有通过AWS Access Key
和AWS Secret Access 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?
答案 0 :(得分:0)
这可以使用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())