如何使用 url 访问 Python 中的 s3 文件? [英] How can I access s3 files in Python using urls?
问题描述
我想编写一个 Python 脚本,该脚本将使用 s3 的 url 读取和写入文件,例如:'s3:/mybucket/file'.它需要在本地和云端运行,无需任何代码更改.有没有办法做到这一点?
I want to write a Python script that will read and write files from s3 using their url's, eg:'s3:/mybucket/file'. It would need to run locally and in the cloud without any code changes. Is there a way to do this?
这里有一些很好的建议,但我真正想要的是允许我这样做的东西:
There are some good suggestions here but what I really want is something that allows me to do this:
myfile = open("s3://mybucket/file", "r")
然后像使用任何其他文件对象一样使用该文件对象.那真的很酷.如果它不存在,我可能只是为自己写这样的东西.我可以在 simples3 或 boto 上构建抽象层.
and then use that file object like any other file object. That would be really cool. I might just write something like this for myself if it doesn't exist. I could build that abstraction layer on simples3 or boto.
推荐答案
对于开场,应该是这么简单:
For opening, it should be as simple as:
import urllib
opener = urllib.URLopener()
myurl = "https://s3.amazonaws.com/skyl/fake.xyz"
myfile = opener.open(myurl)
如果文件是公开的,这将适用于 s3.
This will work with s3 if the file is public.
要使用 boto 编写文件,它有点像这样:
To write a file using boto, it goes a little something like this:
from boto.s3.connection import S3Connection
conn = S3Connection(AWS_KEY, AWS_SECRET)
bucket = conn.get_bucket(BUCKET)
destination = bucket.new_key()
destination.name = filename
destination.set_contents_from_file(myfile)
destination.make_public()
让我知道这是否适合你:)
lemme know if this works for you :)
这篇关于如何使用 url 访问 Python 中的 s3 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!