跳转到主要内容

简单、快速的Amazon AWS S3接口

项目描述

simples3是一个相当简单、相当快速的对Amazon S3存储服务的接口。

它是从对其他库的挫败感中产生的,这些库要么写得过于实用(慢),要么过于臃肿,或者只是半成品。

该模块的目标是

  • 简单

  • 相当的速度

  • 非侵入性

它确实是设计为适合程序员记忆的。三个基本操作与字典一样简单。

由于简单,所以没有依赖性 - 代码仅依赖于Python标准库。

simples3需要Python 2.5+和nose来运行测试。目前尚不支持Python 3。

IRC

#sendapatchchat.freenode.net

用法

简单的Amazon AWS S3接口

通过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()

请注意类型是八位字节流。这仅仅是因为我们没有指定任何其他内容。可以使用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。除此之外,它是一个字符串子类。

最后,字典-like 行为体现在测试中

>>> "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_formake_url_authedmake_url

  • S3Bucket 类添加了可选的超时参数。

  • 添加了基于nose的测试。

  • 添加了对 poster.streaminghttp 的流式传输支持。

  • 添加了对Google App Engine的支持。

simples3 0.5 的变化

  • 添加S3到S3的复制方法。

simples3 0.4 的变化

  • 一些小修复,主要是由于之前的版本命名方案不佳,所以发布为0.4。

  • 0.4.1:在HTTP 500时使put方法重试。

  • 0.4.1:修复了在提供元数据时签名生成中的关键错误。

simples3 0.3 的变化

  • 在存储桶上添加了 url_for 方法,让您可以使用过期URL。感谢Pavel Repin。

  • 更好的测试覆盖率。

  • simples3 现在可以在Python 2.6的 mimetypes 模块上工作。

  • r1:更好地处理异常解析器中的HTTP错误,这破坏了存在测试。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

simples3-1.0.tar.gz (14.6 kB 查看散列)

上传时间

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面