检索 S3 文件作为对象而不是下载到绝对系统路径 [英] Retrieve S3 file as Object instead of downloading to absolute system path
本文介绍了检索 S3 文件作为对象而不是下载到绝对系统路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚开始学习和使用 S3,阅读文档.实际上我没有找到任何东西可以将文件提取到对象中而不是从 S3 下载它?如果这是可能的,或者我遗漏了什么?
I just started learning and using S3, read the docs. Actually I didn't find anything to fetch the file into an object instead of downloading it from S3? if this could be possible, or I am missing something?
其实我想避免下载文件后的额外IO.
Actually I want to avoid additional IO after downloading the file.
推荐答案
您可以使用 StringIO
并使用 get_contents_as_string
从 S3 获取文件内容,如下所示:
You could use StringIO
and get file content from S3 using get_contents_as_string
, like this:
import pandas as pd
from io import StringIO
from boto.s3.connection import S3Connection
AWS_KEY = 'XXXXXXDDDDDD'
AWS_SECRET = 'pweqory83743rywiuedq'
aws_connection = S3Connection(AWS_KEY, AWS_SECRET)
bucket = aws_connection.get_bucket('YOUR_BUCKET')
fileName = "test.csv"
content = bucket.get_key(fileName).get_contents_as_string()
reader = pd.read_csv(StringIO.StringIO(content))
这篇关于检索 S3 文件作为对象而不是下载到绝对系统路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文