简单快速的amazon aws s3接口
simples3的Python项目详细描述
simple s3是一个相当简单、相当快速的接口,可以连接到Amazon的S3存储 服务。
它源于对其他库的失望,这些库要么也被编写了 实用主义的(慢的),太臃肿的,或者只做了一半。
本模块的目标是:
- simplicity,
- decent speed,
- non-intrusiveness.
它真的是为适应程序员的记忆而设计的。三大基本操作 和字典一样简单。
出于简单性,没有依赖性-代码仅依赖于python 标准库。
simples3需要python 2.5+和nose来运行测试。python 3支持尚不可用。
IRC
#sendapatch在chat.freenode.net上。
用法
一个简单的amazon aws s3接口
访问bucket是通过S3Bucket类完成的。它有三个要求 参数:
>>> s = S3Bucket(bucket, ... access_key=access_key, ... secret_key=secret_key) ... >>> print s # doctest: +ELLIPSIS <S3Bucket ... at 'https://s3.amazonaws.com/...'>
或者,如果您想使用想要的任何域,请将base_url设置为 像http://s3.example.com:
>>> s = S3Bucket(bucket, ... access_key=access_key, ... secret_key=secret_key, ... base_url=base_url) >>> print s # doctest: +ELLIPSIS <S3Bucket ... at 'http...'>
注意上面缺少斜线,这很重要。把它想象成 “所有呼叫的前缀”。方案也可以是https或 常规的http或任何其他urllib2兼容方案(如 注册您自己的计划。)
现在,让我们开始做一些有用的事情。首先放一个简单的文件 在那里:
>>> s.put("my file", "my content")
好吧,拿回来:
>>> f = s.get("my file") >>> f.read() 'my content'
很好,很整洁,但是如果我们想知道更多关于我们所取文件的信息呢?简单:
>>> f.s3_info["modify"] # doctest: +ELLIPSIS datetime.datetime(...) >>> f.s3_info["mimetype"] 'application/octet-stream' >>> f.s3_info.keys() ['mimetype', 'modify', 'headers', 'date', 'size', 'metadata'] >>> f.close()
注意,类型是octet stream。那只是因为我们没有具体说明 别的。使用mimetype关键字参数:
>>> s.put("my new file!", "Improved content!\nMultiple lines!", ... mimetype="text/plain")
让我们冷静点,使用pythonic api来获取:
>>> f = s["my new file!"] >>> print f.read() Improved content! Multiple lines! >>> f.s3_info["mimetype"] 'text/plain' >>> f.close()
干得好,嗯。现在,让我们删除它:
>>> del s["my new file!"]
本可以改用delete方法,但我们没有。
如果您只想知道一把钥匙,请询问,您将收到:
>>> from pprint import pprint >>> s["This is a testfile."] = S3File("Hi!", metadata={"hairdo": "Secret"}) >>> pprint(s.info("This is a testfile.")) # doctest: +ELLIPSIS {'date': datetime.datetime(...), 'headers': {'content-length': '3', 'content-type': 'application/octet-stream', 'date': '...', 'etag': '"..."', 'last-modified': '...', 'server': 'AmazonS3', 'x-amz-id-2': '...', 'x-amz-meta-hairdo': 'Secret', 'x-amz-request-id': '...'}, 'metadata': {'hairdo': 'Secret'}, 'mimetype': 'application/octet-stream', 'modify': datetime.datetime(...), 'size': 3}
值得注意的是,您在metadata键中解析了元数据。你 可能还注意到文件是如何上传的,使用s3file对象 像那样。从某种意义上说,这是一种更好的方法。
s3file只接受其关键字参数,并将它们传递到put 稍后。除此之外,它是str子类。
最后一个类似于dict的行为是在测试中:
>>> "This is a testfile." in s True >>> del s["This is a testfile."] >>> "This is a testfile." in s False
您还可以使用put设置屏蔽acl,这非常简单:
>>> s.put("test/foo", "test", acl="public-read") >>> s.put("test/bar", "rawr", acl="public-read")
砰。还有什么?列出存储桶:
>>> for (key, modify, etag, size) in s.listdir(prefix="test/"): ... print "%r (%r) is size %r, modified %r" % (key, etag, size, modify) ... # doctest: +ELLIPSIS 'test/bar' ('"..."') is size 4, modified datetime.datetime(...) 'test/foo' ('"..."') is size 4, modified datetime.datetime(...)
这就是基本的总结。
Simples3 1.0中的更改
- 使simples3成为一个“扁平包”,导入工作照常进行。
- 将url_for重构为make_url_authed,make_url。
- 向S3Bucket类添加了可选的超时参数。
- 增加了基于鼻子的测试。
- 添加了对使用poster.streaminghttp进行流式处理的支持。
- 增加了对谷歌应用引擎的支持。
Simples3 0.5中的更改
- 添加s3-to-s3复制方法。
Simples3 0.4中的更改
- 小补丁,发布为0.4,主要是因为之前的版本命名 计划是个坏主意。
- 0.4.1:在HTTP 500上重试了Put方法。
- 0.4.1:修复给定元数据时签名生成中的严重错误。
Simples3 0.3中的更改
- 在bucket上添加一个url_for方法,允许您使用过期的url。谢谢 帕维尔·雷平。
- 更好的测试覆盖率。
- simples3现在可以在python 2.6的mimetypes模块上工作。
- R1:处理异常解析器中的HTTP错误,这打破了存在 测试。