Ceph Reef(18.2.X)之Swift操作物件儲存閘道器

尹正杰發表於2024-08-31

                                              作者:尹正傑

版權宣告:原創作品,謝絕轉載!否則將追究法律責任。

目錄
  • 一.Swift概述
    • 1.Switft API介面概述
    • 2.swift實現的基本邏輯
  • 二.swift命令列配置實戰
    • 1.建立swift的實踐使用者
    • 2.基於現有使用者建立子使用者
    • 3.基於子使用者生成secret_key資訊
    • 4.安裝swift命令
    • 5.配置swift的環境變數
  • 三swift常用的命令實操
    • 1.檢視儲存桶列表
    • 2.建立儲存桶
    • 3.刪除儲存桶
    • 4.上傳檔案或目錄
    • 5.下載檔案或目錄
    • 6.刪除檔案或目錄

一.Swift概述

1.Switft API介面概述

swift的使用者賬號對應radosgw中的subuser(子使用者),它隸屬於某個事先存在的user(使用者賬號)。

Swift API的上下文中,儲存桶以container表示,而非S3中的bucket,但二者在功能上相似,都是物件資料的容器。

Python swiftclient時一個用於和swift API互動的python客戶端程式,它包含了Python API(swift模組)和一個swift命令。

swift命令可以透過Swift API完成容器和物件資料的管理操作。

2.swift實現的基本邏輯

- 1.建立專屬的使用者名稱和子使用者授權;
- 2.安裝專屬的客戶端命令和python模組;
- 3.配置專屬的認證配置檔案;
- 4.綜合測試swift的資源物件管理;

二.swift命令列配置實戰

1.建立swift的實踐使用者

	1.建立賬號
[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 ~]# 


	2.檢視賬號列表
[root@ceph141 ~]# radosgw-admin user list
[
    "dashboard",
    "yinzhengjie"
]
[root@ceph141 ~]# 

2.基於現有使用者建立子使用者

[root@ceph141 ~]# radosgw-admin subuser create --uid yinzhengjie --subuser=yinzhengjie:swift --access=full
{
    "user_id": "yinzhengjie",
    "display_name": "尹正傑",
    "email": "",
    "suspended": 0,
    "max_buckets": 1000,
    "subusers": [
        {
            "id": "yinzhengjie:swift",
            "permissions": "full-control"
        }
    ],
    "keys": [
        {
            "user": "yinzhengjie",
            "access_key": "M25RJ5F8XLNVUY4ORF6Z",
            "secret_key": "lk7c4eNCAkTOfaI3BuOcct70peebF2CCPUKpR6s5"
        }
    ],
    "swift_keys": [
        {
            "user": "yinzhengjie:swift",
            "secret_key": "JW9ZWMFX7fZsiaWVMBvbg1q8JA5NA4FA69Ab6z4W"
        }
    ],
    "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.基於子使用者生成secret_key資訊

[root@ceph141 ~]# radosgw-admin key create --subuser=yinzhengjie:swift --key-type=swift --gen-secret
{
    "user_id": "yinzhengjie",
    "display_name": "尹正傑",
    "email": "",
    "suspended": 0,
    "max_buckets": 1000,
    "subusers": [
        {
            "id": "yinzhengjie:swift",
            "permissions": "full-control"
        }
    ],
    "keys": [
        {
            "user": "yinzhengjie",
            "access_key": "M25RJ5F8XLNVUY4ORF6Z",
            "secret_key": "lk7c4eNCAkTOfaI3BuOcct70peebF2CCPUKpR6s5"
        }
    ],
    "swift_keys": [
        {
            "user": "yinzhengjie:swift",
            "secret_key": "trAYFVL6Gb26BLLqdaDznoS1vhBfs1ZR8K18jUjR"
        }
    ],
    "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 ~]# 

4.安裝swift命令

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

	2.安裝python-swiftclient
[root@ceph141 ~]# pip install python-swiftclient==4.6.0


溫馨提示:
	- 1.安裝swiftclient命令時一定要注意版本python 3.6+
	- 2.swiftclient官方連結: 
		https://pypi.org/project/python-swiftclient/	
	- 3.swift命令格式解析:
		swift -A 認證URL/auth -U 使用者名稱:swift -K secret_key COMMAND

5.配置swift的環境變數

	1.如上圖所示,swift命令測試效果沒有需配置環境變數,但是會讓命令後很長
[root@ceph141 ~]# s3cmd ls s3://yinzhengjie-rgw/
2024-08-29 23:55           35  s3://yinzhengjie-rgw/blog.txt
[root@ceph141 ~]# 
[root@ceph141 ~]# swift -A http://ceph142/auth -U yinzhengjie:swift -K trAYFVL6Gb26BLLqdaDznoS1vhBfs1ZR8K18jUjR list yinzhengjie-rgw
blog.txt
[root@ceph141 ~]# 


	2.配置swift環境變數
[root@ceph141 ~]# cat > ~/.swift <<EOF
export ST_AUTH=http://ceph142/auth
export ST_USER=yinzhengjie:swift
export ST_KEY=trAYFVL6Gb26BLLqdaDznoS1vhBfs1ZR8K18jUjR
EOF
[root@ceph141 ~]# 
[root@ceph141 ~]# source ~/.swift 
[root@ceph141 ~]# 

	3.如下圖所示,測試效果很明顯,無需在命令列中加一堆引數啦
[root@ceph141 ~]# swift list  # 檢視所有的儲存桶
yinzhengjie-rgw
[root@ceph141 ~]# 
[root@ceph141 ~]# swift list yinzhengjie-rgw  # 檢視指定儲存桶的物件名稱
blog.txt
[root@ceph141 ~]# 
[root@ceph141 ~]# swift stat
                                    Account: v1
                                 Containers: 1
                                    Objects: 1
                                      Bytes: 35
   Containers in policy "default-placement": 1
      Objects in policy "default-placement": 1
        Bytes in policy "default-placement": 35
Objects in policy "default-placement-bytes": 0
  Bytes in policy "default-placement-bytes": 0
                                X-Timestamp: 1725028907.96608
                X-Account-Bytes-Used-Actual: 4096
                                 X-Trans-Id: tx000003b4b5d13346aa241-0066d1da2b-12179-default
                     X-Openstack-Request-Id: tx000003b4b5d13346aa241-0066d1da2b-12179-default
                              Accept-Ranges: bytes
                               Content-Type: text/plain; charset=utf-8
                                 Connection: Keep-Alive
[root@ceph141 ~]# 

三swift常用的命令實操

1.檢視儲存桶列表

[root@ceph141 ~]# swift list 
yinzhengjie-rgw
[root@ceph141 ~]#
[root@ceph141 ~]# swift list yinzhengjie-rgw  # 檢視指定儲存桶的物件
blog.txt
[root@ceph141 ~]# 

2.建立儲存桶

[root@ceph141 ~]# swift post yinzhengjie-swift
[root@ceph141 ~]# 
[root@ceph141 ~]# swift list 
yinzhengjie-rgw
yinzhengjie-swift
[root@ceph141 ~]# 

3.刪除儲存桶

[root@ceph141 ~]# swift delete yinzhengjie-swift
yinzhengjie-swift
[root@ceph141 ~]# 
[root@ceph141 ~]# swift list 
yinzhengjie-rgw
[root@ceph141 ~]# 

4.上傳檔案或目錄

	1.上傳檔案
[root@ceph141 ~]# swift list yinzhengjie-rgw
blog.txt
[root@ceph141 ~]# 
[root@ceph141 ~]# swift upload yinzhengjie-rgw /etc/os-release 
etc/os-release
[root@ceph141 ~]# 
[root@ceph141 ~]# swift list yinzhengjie-rgw
blog.txt
etc/os-release
[root@ceph141 ~]# 


	2.上傳目錄,和上傳檔案相同
[root@ceph141 ~]# ls /yinzhengjie/ -R
/yinzhengjie/:
data

/yinzhengjie/data:
rbd0  rbd1  rbd2  wp01

/yinzhengjie/data/rbd0:

/yinzhengjie/data/rbd1:

/yinzhengjie/data/rbd2:

/yinzhengjie/data/wp01:
[root@ceph141 ~]# 
[root@ceph141 ~]# swift upload yinzhengjie-rgw /yinzhengjie
yinzhengjie/data/rbd1
yinzhengjie/data/rbd2
yinzhengjie/data/rbd0
yinzhengjie/data/wp01
[root@ceph141 ~]#
[root@ceph141 ~]# swift list yinzhengjie-rgw
blog.txt
etc/os-release
yinzhengjie/data/rbd0
yinzhengjie/data/rbd1
yinzhengjie/data/rbd2
yinzhengjie/data/wp01
[root@ceph141 ~]# 

5.下載檔案或目錄

	1.下載檔案
[root@ceph141 ~]# swift download yinzhengjie-rgw etc/os-release
etc/os-release [auth 0.005s, headers 0.015s, total 0.015s, 0.036 MB/s]
[root@ceph141 ~]# 
[root@ceph141 ~]# ll etc/os-release 
-rw-r--r-- 1 root root 386 Feb 14  2024 etc/os-release
[root@ceph141 ~]# 


	2.下載目錄
[root@ceph141 ~]# swift download yinzhengjie-rgw --prefix='yinzhengjie/data'
yinzhengjie/data/wp01 [auth 0.007s, headers 0.018s, total 0.019s, 0.000 MB/s]
yinzhengjie/data/rbd1 [auth 0.012s, headers 0.023s, total 0.025s, 0.000 MB/s]
yinzhengjie/data/rbd0 [auth 0.009s, headers 0.019s, total 0.019s, 0.000 MB/s]
yinzhengjie/data/rbd2 [auth 0.014s, headers 0.021s, total 0.022s, 0.000 MB/s]
[root@ceph141 ~]# 
[root@ceph141 ~]# ll yinzhengjie/data/
total 24
drwxr-xr-x 6 root root 4096 Aug 30 23:30 ./
drwxr-xr-x 3 root root 4096 Aug 30 23:30 ../
drwxr-xr-x 2 root root 4096 Aug 30 23:30 rbd0/
drwxr-xr-x 2 root root 4096 Aug 30 23:30 rbd1/
drwxr-xr-x 2 root root 4096 Aug 30 23:30 rbd2/
drwxr-xr-x 2 root root 4096 Aug 30 23:30 wp01/
[root@ceph141 ~]# 

	3.下載所有
[root@ceph141 ~]# swift download --all

6.刪除檔案或目錄

	1.刪除檔案
[root@ceph141 ~]# swift list yinzhengjie-rgw
blog.txt
etc/os-release
yinzhengjie/data/rbd0
yinzhengjie/data/rbd1
yinzhengjie/data/rbd2
yinzhengjie/data/wp01
[root@ceph141 ~]# 
[root@ceph141 ~]# swift delete yinzhengjie-rgw etc/os-release
etc/os-release
[root@ceph141 ~]# 
[root@ceph141 ~]# swift list yinzhengjie-rgw
blog.txt
yinzhengjie/data/rbd0
yinzhengjie/data/rbd1
yinzhengjie/data/rbd2
yinzhengjie/data/wp01
[root@ceph141 ~]# 

	2.刪除目錄
[root@ceph141 ~]# swift list yinzhengjie-rgw
blog.txt
yinzhengjie/data/rbd0
yinzhengjie/data/rbd1
yinzhengjie/data/rbd2
yinzhengjie/data/wp01
[root@ceph141 ~]# 
[root@ceph141 ~]# swift delete yinzhengjie-rgw --prefix='yinzhengjie/data'
yinzhengjie/data/rbd1
yinzhengjie/data/rbd2
yinzhengjie/data/rbd0
yinzhengjie/data/wp01
[root@ceph141 ~]# 
[root@ceph141 ~]# swift list yinzhengjie-rgw
blog.txt
[root@ceph141 ~]# 

	3.刪除所有儲存桶
[root@ceph141 ~]# swift delete --all
yinzhengjie-rgw/blog.txt
yinzhengjie-rgw
[root@ceph141 ~]# 
[root@ceph141 ~]# swift list 
[root@ceph141 ~]# 

相關文章