注意

本文档适用于 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

删除存储桶

注意

存储桶必须为空!否则它将无法工作!

conn.delete_bucket(bucket.name)

强制删除非空存储桶

注意

在 python 中不可用

创建对象

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

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 变为公开。然后,这会为 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 客户端测试 RadosGW 对 S3 API 的扩展,应将 扩展文件 放置在 ~/.aws/models/s3/2006-03-01/ 目录下。例如,可以使用以下方式获取无序对象列表

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

如果没有扩展文件,在上面的示例中,boto3 将会抱怨 AllowUnordered 参数无效。

由 Ceph 基金会为您呈现

Ceph 文档是由非营利性 Ceph 基金会 资助和托管的社区资源。如果您希望支持这项工作和我们的其他努力,请考虑 立即加入