使用Amazon S3 Python版本 连接ceph 基本操作

这篇博客介绍了如何在Windows 10环境下,通过Python的boto库连接并管理Ceph集群上的S3存储。内容包括创建S3连接、列举桶、创建和删除桶、列出及操作桶内对象、下载和删除对象,以及生成对象的下载URL。
摘要由CSDN通过智能技术生成

参考自Ceph官方文档

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值