Ceph Reef(18.2.X)之python操作对象存储网关

本文涉及的产品
对象存储 OSS,20GB 3个月
对象存储 OSS,内容安全 1000次 1年
对象存储 OSS,恶意文件检测 1000次 1年
简介: 这篇文章介绍了如何在Ceph Reef(18.2.X)环境中使用Python操作对象存储网关(rgw),包括环境搭建、账号创建、使用s3cmd工具以及编写和测试Python代码。

                                              作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.环境准备

1.Ceph Reef(18.2.X)的对象存储网关(rgw)组件搭建

推荐阅读:
    https://developer.aliyun.com/article/1605062

2.创建账号

[root@ceph141 ~]# radosgw-admin user create --uid "yinzhengjie" --display-name "尹正杰"
{
    "user_id": "yinzhengjie",
    "display_name": "尹正杰",
    "email": "",
    "suspended": 0,
    "max_buckets": 1000,
    "subusers": [],
    "keys": [
        {
            "user": "yinzhengjie",
            "access_key": "M25RJ5F8XLNVUY4ORF6Z",
            "secret_key": "lk7c4eNCAkTOfaI3BuOcct70peebF2CCPUKpR6s5"
        }
    ],
    "swift_keys": [],
    "caps": [],
    "op_mask": "read, write, delete",
    "default_placement": "",
    "default_storage_class": "",
    "placement_tags": [],
    "bucket_quota": {
        "enabled": false,
        "check_on_raw": false,
        "max_size": -1,
        "max_size_kb": 0,
        "max_objects": -1
    },
    "user_quota": {
        "enabled": false,
        "check_on_raw": false,
        "max_size": -1,
        "max_size_kb": 0,
        "max_objects": -1
    },
    "temp_url_keys": [],
    "type": "rgw",
    "mfa_ids": []
}

[root@ceph141 ~]#

3.s3cmd工具

    1.安装s3cmd
apt -y install s3cmd

    2.查看rgw所在节点
[root@ceph141 ~]# ceph orch ls  | grep rgw
rgw.yinzhengjie  ?:80             1/1  10m ago    31m  ceph142      
[root@ceph141 ~]# 
[root@ceph141 ~]# echo 172.30.100.142 www.yinzhengjie.com >> /etc/hosts
[root@ceph141 ~]# 

    3.运行s3cmd的运行环境,生成"/root/.s3cfg"配置文件
[root@ceph141 ~]# s3cmd --configure 

Enter new values or accept defaults in brackets with Enter.
Refer to user manual for detailed description of all options.

Access key and Secret key are your identifiers for Amazon S3. Leave them empty for using the env variables.
Access Key: M25RJ5F8XLNVUY4ORF6Z
Secret Key: lk7c4eNCAkTOfaI3BuOcct70peebF2CCPUKpR6s5
Default Region [US]: 

Use "s3.amazonaws.com" for S3 Endpoint and not modify it to the target Amazon S3.
S3 Endpoint [s3.amazonaws.com]: www.yinzhengjie.com

Use "%(bucket)s.s3.amazonaws.com" to the target Amazon S3. "%(bucket)s" and "%(location)s" vars can be used
if the target S3 system supports dns based buckets.
DNS-style bucket+hostname:port template for accessing a bucket [%(bucket)s.s3.amazonaws.com]: www.yinzhengjie.com/%(bucket)

Encryption password is used to protect your files from reading
by unauthorized persons while in transfer to S3
Encryption password: 
Path to GPG program [/usr/bin/gpg]: 

When using secure HTTPS protocol all communication with Amazon S3
servers is protected from 3rd party eavesdropping. This method is
slower than plain HTTP, and can only be proxied with Python 2.7 or newer
Use HTTPS protocol [Yes]: No

On some networks all internet access must go through a HTTP proxy.
Try setting it here if you can't connect to S3 directly
HTTP Proxy server name: 

New settings:
  Access Key: M25RJ5F8XLNVUY4ORF6Z
  Secret Key: lk7c4eNCAkTOfaI3BuOcct70peebF2CCPUKpR6s5
  Default Region: US
  S3 Endpoint: www.yinzhengjie.com
  DNS-style bucket+hostname:port template for accessing a bucket: www.yinzhengjie.com/%(bucket)
  Encryption password: 
  Path to GPG program: /usr/bin/gpg
  Use HTTPS protocol: False
  HTTP Proxy server name: 
  HTTP Proxy server port: 0

Test access with supplied credentials? [Y/n] Y
Please wait, attempting to list all buckets...
Success. Your access key and secret key worked fine :-)

Now verifying that encryption works...
Not configured. Never mind.

Save settings? [y/N] y
Configuration saved to '/root/.s3cfg'
[root@ceph141 ~]#

二.Python操作对象存储

1.安装python环境

    1.安装pip工具包
[root@ceph141 ~]# apt -y install python3-pip

    2.配置pip3软件源
[root@ceph141 ~]# mkdir ~/.pip
[root@ceph141 ~]# vim ~/.pip/pip.conf
[root@ceph141 ~]# cat  ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[root@ceph141 ~]# 

    3.安装boto包
[root@ceph141 ~]# pip install boto

2.编写python程序

[root@ceph141 ~]# cat rgw-yinzhengjie.py 
import boto
import boto.s3.connection

access_key = 'M25RJ5F8XLNVUY4ORF6Z'
secret_key = 'lk7c4eNCAkTOfaI3BuOcct70peebF2CCPUKpR6s5'

# 连接rgw
conn = boto.connect_s3(
        aws_access_key_id = access_key,
        aws_secret_access_key = secret_key,
        host = 'www.yinzhengjie.com',
        is_secure=False,
        calling_format = boto.s3.connection.OrdinaryCallingFormat(),
        )

# 创建bucket
bucket = conn.create_bucket('yinzhengjie-rgw')

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


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

# 创建一个对象
key = bucket.new_key('blog.txt')
key.set_contents_from_string('https://www.cnblogs.com/yinzhengjie')

# 生成对象下载的URL
hello_key = bucket.get_key('blog.txt')
hello_url = hello_key.generate_url(0, query_auth=False, force_http=True)
print(hello_url)
[root@ceph141 ~]# 

参考链接:
      https://docs.ceph.com/en/latest/radosgw/s3/python/

3.测试python代码测试

[root@ceph141 ~]# python3 rgw-yinzhengjie.py 
yinzhengjie-rgw 2024-08-29T23:40:36.356Z
blog.txt        35      2024-08-29T23:44:19.424Z
http://www.yinzhengjie.com/yinzhengjie-rgw/blog.txt
[root@ceph141 ~]#

4.使用s3cmd命令访问测试

[root@ceph141 ~]# s3cmd get s3://yinzhengjie-rgw/blog.txt
download: 's3://yinzhengjie-rgw/blog.txt' -> './blog.txt'  [1 of 1]
 35 of 35   100% in    0s   712.50 B/s  done
[root@ceph141 ~]# 
[root@ceph141 ~]# more ./blog.txt
https://www.cnblogs.com/yinzhengjie
[root@ceph141 ~]#

5.创建访问策略

    1.编写策略配置文件
[root@ceph141 ~]# cat yinzhengjie-anonymous-access-policy.json 
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": {"AWS": ["*"]},
    "Action": "s3:GetObject",
    "Resource": [
      "arn:aws:s3:::yinzhengjie-rgw/*"
    ]
  }]
}
[root@ceph141 ~]# 

    2.应用策略
[root@ceph141 ~]# s3cmd info s3://yinzhengjie-rgw
s3://yinzhengjie-rgw/ (bucket):
   Location:  default
   Payer:     BucketOwner
   Expiration Rule: none
   Policy:    none
   CORS:      none
   ACL:       尹正杰: FULL_CONTROL
[root@ceph141 ~]# 
[root@ceph141 ~]# s3cmd setpolicy yinzhengjie-anonymous-access-policy.json s3://yinzhengjie-rgw
s3://yinzhengjie-rgw/: Policy updated
[root@ceph141 ~]# 
[root@ceph141 ~]# s3cmd info s3://yinzhengjie-rgw
s3://yinzhengjie-rgw/ (bucket):
   Location:  default
   Payer:     BucketOwner
   Expiration Rule: none
   Policy:    {
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": {"AWS": ["*"]},
    "Action": "s3:GetObject",
    "Resource": [
      "arn:aws:s3:::yinzhengjie-rgw/*"
    ]
  }]
}

   CORS:      none
   ACL:       尹正杰: FULL_CONTROL
[root@ceph141 ~]#

6.发起http请求测试

[root@ceph141 ~]# curl -s http://www.yinzhengjie.com/yinzhengjie-rgw/blog.txt | more 
https://www.cnblogs.com/yinzhengjie
[root@ceph141 ~]# 


温馨提示:
   由于咱们访问的并不是html文件,不建议使用浏览器访问,而是用curl来模拟http请求即可。
相关实践学习
借助OSS搭建在线教育视频课程分享网站
本教程介绍如何基于云服务器ECS和对象存储OSS,搭建一个在线教育视频课程分享网站。
目录
相关文章
|
4月前
|
存储 人工智能 开发工具
AI助理化繁为简,速取代码参数——使用python SDK 处理OSS存储的图片
只需要通过向AI助理提问的方式输入您的需求,即可瞬间获得核心流程代码及参数,缩短学习路径、提升开发效率。
1486 4
AI助理化繁为简,速取代码参数——使用python SDK 处理OSS存储的图片
|
5月前
|
存储 API Swift
Ceph Reef(18.2.X)之Swift操作对象存储网关
这篇文章详细介绍了Ceph Reef(18.2.X)中通过Swift API操作对象存储网关的方法,包括创建用户、子用户、配置环境变量、以及使用swift命令行工具进行存储桶和对象的管理。
68 7
Ceph Reef(18.2.X)之Swift操作对象存储网关
|
5月前
|
存储 对象存储
Ceph Reef(18.2.X)的对象存储网关(rgw)组件搭建
这篇文章是关于Ceph Reef(18.2.X)版本中对象存储系统的配置和使用案例,包括对象存储网关的概述、核心资源介绍、Ceph RGW支持的接口、高可用radosgw的部署、s3cmd工具的使用以及如何通过HTTP方式访问对象存储。
215 3
Ceph Reef(18.2.X)的对象存储网关(rgw)组件搭建
|
5月前
|
存储 对象存储
radosgw高可用对象存储网关实战指南
关于radosgw高可用对象存储网关的实战指南,涵盖了从基本概念到具体操作案例,再到架构设计和使用技巧的全面介绍。
112 6
|
Java PHP Ruby
Python中的网关:WSGI
互联网飞速发展,在web开发的江湖中,除了武林盟主Java以外,许多脚本语言都参与进来分一杯羹。花开花落,花落花开,无数少年侠士在Web江湖演绎着自己的传说,不不负少年游。
435 0
Python中的网关:WSGI
|
2月前
|
人工智能 数据可视化 数据挖掘
探索Python编程:从基础到高级
在这篇文章中,我们将一起深入探索Python编程的世界。无论你是初学者还是有经验的程序员,都可以从中获得新的知识和技能。我们将从Python的基础语法开始,然后逐步过渡到更复杂的主题,如面向对象编程、异常处理和模块使用。最后,我们将通过一些实际的代码示例,来展示如何应用这些知识解决实际问题。让我们一起开启Python编程的旅程吧!
|
2月前
|
存储 数据采集 人工智能
Python编程入门:从零基础到实战应用
本文是一篇面向初学者的Python编程教程,旨在帮助读者从零开始学习Python编程语言。文章首先介绍了Python的基本概念和特点,然后通过一个简单的例子展示了如何编写Python代码。接下来,文章详细介绍了Python的数据类型、变量、运算符、控制结构、函数等基本语法知识。最后,文章通过一个实战项目——制作一个简单的计算器程序,帮助读者巩固所学知识并提高编程技能。
|
2月前
|
Unix Linux 程序员
[oeasy]python053_学编程为什么从hello_world_开始
视频介绍了“Hello World”程序的由来及其在编程中的重要性。从贝尔实验室诞生的Unix系统和C语言说起,讲述了“Hello World”作为经典示例的起源和流传过程。文章还探讨了C语言对其他编程语言的影响,以及它在系统编程中的地位。最后总结了“Hello World”、print、小括号和双引号等编程概念的来源。
126 80
|
23天前
|
存储 缓存 Java
Python高性能编程:五种核心优化技术的原理与Python代码
Python在高性能应用场景中常因执行速度不及C、C++等编译型语言而受质疑,但通过合理利用标准库的优化特性,如`__slots__`机制、列表推导式、`@lru_cache`装饰器和生成器等,可以显著提升代码效率。本文详细介绍了这些实用的性能优化技术,帮助开发者在不牺牲代码质量的前提下提高程序性能。实验数据表明,这些优化方法能在内存使用和计算效率方面带来显著改进,适用于大规模数据处理、递归计算等场景。
58 5
Python高性能编程:五种核心优化技术的原理与Python代码
|
2月前
|
Python
[oeasy]python055_python编程_容易出现的问题_函数名的重新赋值_print_int
本文介绍了Python编程中容易出现的问题,特别是函数名、类名和模块名的重新赋值。通过具体示例展示了将内建函数(如`print`、`int`、`max`)或模块名(如`os`)重新赋值为其他类型后,会导致原有功能失效。例如,将`print`赋值为整数后,无法再用其输出内容;将`int`赋值为整数后,无法再进行类型转换。重新赋值后,这些名称失去了原有的功能,可能导致程序错误。总结指出,已有的函数名、类名和模块名不适合覆盖赋新值,否则会失去原有功能。如果需要使用类似的变量名,建议采用其他命名方式以避免冲突。
52 14

推荐镜像

更多