注意

本文档适用于 Ceph 开发版本。

Python S3 示例

创建连接

这会创建一个连接,以便你可以与服务器交互。

import boto
import boto.s3.connection
access_key = 'put your access key here!'
secret_key = 'put your secret key here!'

conn = boto.connect_s3(
        aws_access_key_id = access_key,
        aws_secret_access_key = secret_key,
        host = 'objects.dreamhost.com',
        #is_secure=False,               # uncomment if you are not using ssl
        calling_format = boto.s3.connection.OrdinaryCallingFormat(),
        )

列出拥有的存储桶

这会获取你拥有的存储桶列表。

for bucket in conn.get_all_buckets():
        print("{name}\t{created}".format(
                name = bucket.name,
                created = bucket.creation_date,
        ))

输出看起来会像这样:

mahbuckat1   2011-04-21T18:05:39.000Z
mahbuckat2   2011-04-21T18:05:48.000Z
mahbuckat3   2011-04-21T18:07:18.000Z

创建存储桶

这会创建一个名为my-new-bucket

bucket = conn.create_bucket('my-new-bucket')

列出存储桶的内容

这会获取存储桶中的对象列表。

for key in bucket.list():
        print("{name}\t{size}\t{modified}".format(
                name = key.name,
                size = key.size,
                modified = key.last_modified,
        ))

输出看起来会像这样:

myphoto1.jpg 251262  2011-08-08T21:35:48.000Z
myphoto2.jpg 262518  2011-08-08T21:38:01.000Z

删除存储桶

Note

存储桶必须是空的!否则它将不起作用!

conn.delete_bucket(bucket.name)

非空存储桶的强制删除

注意

在 python 中不可用

创建对象

这会创建一个文件hello.txt包含字符串"Hello World!"

key = bucket.new_key('hello.txt')
key.set_contents_from_string('Hello World!')

上传对象或文件

这会创建一个文件logo.png包含来自文件的"logo.png"

key = bucket.new_key('logo.png')
key.set_contents_from_filename('logo.png')

更改对象的 ACL

这会使对象hello.txt对公众可读,secret_plans.txt并且是私有的。

hello_key = bucket.get_key('hello.txt')
hello_key.set_canned_acl('public-read')
plans_key = bucket.get_key('secret_plans.txt')
plans_key.set_canned_acl('private')

下载对象(到文件)

这会下载对象perl_poetry.pdf并将其保存到/home/larry/documents/

key = bucket.get_key('perl_poetry.pdf')
key.get_contents_to_filename('/home/larry/documents/perl_poetry.pdf')

删除对象

这会删除对象goodbye.txt

bucket.delete_key('goodbye.txt')

生成对象下载 URL(签名和未签名的)

这会为hello.txt生成一个未签名的下载 URL。这之所以有效,是因为我们通过上述设置 ACL 使其公开。hello.txt public by setting the ACL above. This then generates a signed download URL for secret_plans.txt生成一个签名下载 URL,该 URL 将在 1 小时内有效。签名下载 URL 即使对象是私有的,在有效期内也会有效(当有效期结束时,URL 将停止工作)。

hello_key = bucket.get_key('hello.txt')
hello_url = hello_key.generate_url(0, query_auth=False, force_http=True)
print(hello_url)

plans_key = bucket.get_key('secret_plans.txt')
plans_url = plans_key.generate_url(3600, query_auth=True, force_http=True)
print(plans_url)

这的输出看起来会像这样:

http://objects.dreamhost.com/my-bucket-name/hello.txt
http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX

使用 S3 API 扩展

要使用 boto3 客户端测试 S3 API 的 RadosGW 扩展,扩展文件extensions file应该放在:~/.aws/models/s3/2006-03-01/目录下。

print(conn.list_objects(Bucket='my-new-bucket', AllowUnordered=True))

没有扩展文件,在上面的例子中,boto3 会抱怨 bad582: 参数无效。AllowUnordered argument is invalid.

由 Ceph 基金会带给您

Ceph 文档是一个社区资源,由非盈利的 Ceph 基金会资助和托管Ceph Foundation. 如果您想支持这一点和我们的其他工作,请考虑加入现在加入.