Akemi

Python boto3模块—访问S3风格API

2024/11/13

前几天面试遇到面试官问,有没有用过ceph,对对象存储了不了解,

我只能回答出知道RDB和CephFS,这两天就抽空研究一下RGW

Ceph-RGW

基础配置与信息获取

ceph的对象存储叫RGW——RADOS Gateway,
通过yum install ceph-radosgw 安装
通过ceph-deploy rgw create <name> 创建存储桶
通过radosgw-admin user create --uid=<用户名> --display-name=<显示名>创建用户,并且返回access_key和secret_key

在ceph.conf中设置rgw监听的端口,和ip组成endpoint_url

S3接口介绍

S3是AWS一开始搞的对象存储,现在很多的对象存储,接口都是根据S3或者Swift风格进行设计的,比如cloudflare的R2,ceph的RGW,都可以兼容S3接口

boto3接口连接

正好我在用cloudflare的R2做图床,就可以用他做测试

对于R2则需要在web创建API,会给一个访问密钥ID——access_key、机密访问密钥ID——secret_key

pip3 install boto3

boto3模块连接——获取桶信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import boto3
from botocore.config import Config

access_key_id='7ea4af6ad8d01d9e8d5a7a162006be70'
secret_access_key='xxx'
endpoint_url='https://da2c2a71e23cbacf4d351c67aa6025ef.r2.cloudflarestorage.com'

s3 = boto3.client('s3',
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key,
endpoint_url=endpoint_url,
config=Config(signature_version='s3v4'),
verify=True
)

response = s3.list_buckets()
print('Existing buckets:')
for bucket in response['Buckets']:
print(f'{bucket["Name"]}')

# Existing buckets:
# wangsheng

除此之外还有很多方法

比如创建通create_bucket()、删除桶delete_bucket()

桶下载文件download_file()、桶上传文件upload_file()等

列出桶中对象list_objects_v2()

CATALOG
  1. 1. Ceph-RGW
  2. 2. boto3接口连接