如何使用网址访问Python中的s3文件? [英] How can I access s3 files in Python using urls?

查看:211
本文介绍了如何使用网址访问Python中的s3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个Python脚本,该脚本将使用s3的url从s3读取和写入文件,例如:'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 :)

这篇关于如何使用网址访问Python中的s3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆