https://docs.ceph.com/en/pacific/radosgw/s3/python/
Jupyter Notebook可直接运行版本https://download.csdn.net/download/HzauTriste/82743913?spm=1001.2014.3001.5503
环境背景Win10下搭建三台Ubuntu20虚拟机形成的ceph集群
背景链接:https://blog.csdn.net/HzauTriste/article/details/122480450?spm=1001.2014.3001.5501
import boto.s3.connection
# 账户
access_key = 'xxxx'
secret_key = 'xxxx'
#创建连接
conn = boto.connect_s3(
aws_access_key_id = access_key,
aws_secret_access_key = secret_key,
host = '10.0.0.xxx',
port = xxxx,
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,
)
创建一个桶
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,
)
删除桶
笔记: 桶必须是空的!否则就不行了!
conn.delete_bucket(bucket.name)
创建对象
key = bucket.new_key('hello.txt')
key.set_contents_from_string('Hello World!')
更改对象的 ACL
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')
下载对象(到文件)
key = bucket.get_key('perl_poetry.pdf')
key.get_contents_to_filename('/home/larry/documents/perl_poetry.pdf')
删除对象
bucket.delete_key('goodbye.txt')
生成对象下载 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



