2

我正在使用下面的代码从Amazon S3usingpython xlrdurllib模块中读取一个 excel 文件,但我遇到了Forbidden访问错误。我知道这是因为我没有通过AWS Access KeyAWS 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?

4

1 回答 1

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())
于 2018-05-29T07:20:27.103 回答