title: RGW對象版本控制(多版本)
前言
多版本就是字面意思:在相同的bucket中保留對象的多個版本。功能目的是為了可以恢復因意外刪除或覆蓋操作而失去的對象。
好久之前玩過這個功能,但是沒有記錄下來,下面簡單記錄下。
1、實驗環境
三個虛擬機組成ceph集群,每臺虛擬機配置信息一樣,如下:
[root@ceph05 ~]# hostnamectl
Static hostname: ceph05
Icon name: computer-vm
Chassis: vm
Machine ID: e8e9768445e2448fb3ff5e5b7138039c
Boot ID: 8be60bf35f1a4eb5be558934342009bb
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-957.el7.x86_64
Architecture: x86-64
搭建標準的三節點集群:
[root@ceph05 ~]# ceph -v
ceph version 12.2.12 (1436006594665279fe734b4c15d7e08c13ebd777) luminous (stable)
[root@ceph05 ~]# ceph osd tree
ID CLASS WEIGHT TYPE NAME STATUS REWEIGHT PRI-AFF
-1 0.11691 root default
-3 0.03897 host ceph05
0 hdd 0.01949 osd.0 up 1.00000 1.00000
1 hdd 0.01949 osd.1 up 1.00000 1.00000
-5 0.03897 host ceph06
2 hdd 0.01949 osd.2 up 1.00000 1.00000
3 hdd 0.01949 osd.3 up 1.00000 1.00000
-7 0.03897 host ceph07
4 hdd 0.01949 osd.4 up 1.00000 1.00000
5 hdd 0.01949 osd.5 up 1.00000 1.00000
ceph05節點配置了一個rgw實例:
[root@ceph05 ~]# cat /etc/ceph/ceph.conf
...
# 配置rgw實例信息
[client.rgw.inst01]
rgw_frontends = civetweb port=9001
[root@ceph05 ~]# systemctl start ceph-radosgw@rgw.inst01
ceph06節點作為rgw的客戶端,安裝awscli工具:
[root@ceph06 ~]# yum install -y awscli
2、實際操作
注意:多版本是針對某個bucket去操作的。
2.1、上傳對象
先說結論:一個沒有開啟多版本的bucket,上傳相同對象名的對象會覆蓋,但是開啟了多版本的bucket,會保留對象之前的版本。
創建bucket01:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api create-bucket --bucket bucket01
上傳一個名為test-key-1的對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-object --bucket bucket01 --key test-key-1 --body /tmp/test-1
{
"ETag": "\"1181c1834012245d785120e3505ed169\""
}
查看bucket01下的對象信息:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket bucket01
{
"Versions": [
{
"LastModified": "2019-11-18T09:52:17.501Z",
"VersionId": "null", # 如果沒有開啟多版本,此字段為null
"ETag": "\"1181c1834012245d785120e3505ed169\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"Size": 4
}
]
}
再次上傳一個名為test-key-1的對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-object --bucket bucket01 --key test-key-1 --body /tmp/test-2
{
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\""
}
查看bucket01下的對象信息,看到之前上傳的對象信息已經被覆蓋了:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket bucket01
{
"Versions": [
{
"LastModified": "2019-11-18T09:52:32.016Z",
"VersionId": "null",
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"Size": 4
}
]
}
開啟bucket01的多版本:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-bucket-versioning --bucket bucket01 --versioning-configuration Status=Enabled
查看是否開啟成功:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api get-bucket-versioning --bucket bucket01
{
"Status": "Enabled" # 表示開啟成功
}
再次上傳一個名為test-key-1的對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-object --bucket bucket01 --key test-key-1 --body /tmp/test-4
{
"ETag": "\"45a62d3d5d3e946250904697486591bc\""
}
查看bucket01下的對象信息:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket bucket01
{
"Versions": [
{
"LastModified": "2019-11-19T06:20:54.613Z",
"VersionId": "urD9LXRy9.ZgoDaYNagoZh911SqJlbG",
"ETag": "\"45a62d3d5d3e946250904697486591bc\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"Size": 4
},
{
"LastModified": "2019-11-18T09:52:32.016Z",
"VersionId": "null",
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": false,
"Size": 4
}
]
}
從上面的信息中可以看出,現在有兩個版本的名為test-key-1的對象了,VersionId分別是null和urD9LXRy9.ZgoDaYNagoZh911SqJlbG,其中VersionId是urD9LXRy9.ZgoDaYNagoZh911SqJlbG的對象的IsLatest字段為true,表示改版本的對象是當前最新的。
2.2、獲取對象
先說結論:沒有開啟多版本時,相同對象名的對象只有一個,獲取時也就只能獲取這一個了。開啟了多版本時,默認獲取當前最新版本的對象,也可以顯示指定--version-id參數來獲取指定version的對象。
默認獲取當前最新的版本的對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api get-object --bucket bucket01 --key test-key-1 test-key-1.out
{
"AcceptRanges": "bytes",
"ContentType": "binary/octet-stream",
"LastModified": "Tue, 19 Nov 2019 06:20:54 GMT",
"ContentLength": 4,
"VersionId": "urD9LXRy9.ZgoDaYNagoZh911SqJlbG",
"ETag": "\"45a62d3d5d3e946250904697486591bc\"",
"Metadata": {}
}
獲取指定版本的對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api get-object --bucket bucket01 --key test-key-1 --version-id null test-key-1.out
{
"AcceptRanges": "bytes",
"ContentType": "binary/octet-stream",
"LastModified": "Mon, 18 Nov 2019 09:52:32 GMT",
"ContentLength": 4,
"VersionId": "null",
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\"",
"Metadata": {}
}
2.3、刪除對象
先說結論:沒有開啟多版本時,刪除對象時將永久刪除,且不可恢復。開啟了多版本時,分兩種情況,一種是不指定--version-id刪除對象,此時rgw不會永久刪除該對象,只是給該對象打上一個被刪除標記,還有一種是顯示指定--version-id參數來刪除對象,此時rgw會永久刪除該對象。
新建一個bucket test-bucket-2:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api create-bucket --bucket test-bucket-2
上傳一個名為test-key-1的對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-object --bucket test-bucket-2 --key test-key-1 --body /tmp/test-2
{
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\""
}
查看bucket里面的對象信息:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket test-bucket-2
{
"Versions": [
{
"LastModified": "2019-11-20T01:59:37.478Z",
"VersionId": "null",
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"Size": 4
}
]
}
刪除該對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api delete-object --bucket test-bucket-2 --key test-key-1
再次查看bucket里面對象的信息:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket test-bucket-2
可以看到對象被永久刪除了。
下面看下開啟多版本后刪除對象情況,新建一個test-bucket-3 bucket,并開啟多版本:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api create-bucket --bucket test-bucket-3
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-bucket-versioning --bucket test-bucket-3 --versioning-configuration Status=Enabled
上傳一個對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-object --bucket test-bucket-3 --key test-key-1 --body /tmp/test-2
{
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\""
}
刪除這個對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api delete-object --bucket test-bucket-3 --key test-key-1
{
"VersionId": "ymH3s-OeK.D4kNAHaj8l8laPRn5hFtM",
"DeleteMarker": true
}
查看bucket里面對象信息:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket test-bucket-3
{
"DeleteMarkers": [
{
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"VersionId": "ymH3s-OeK.D4kNAHaj8l8laPRn5hFtM",
"Key": "test-key-1",
"LastModified": "2019-11-20T02:08:10.572Z"
}
],
"Versions": [
{
"LastModified": "2019-11-20T02:06:13.742Z",
"VersionId": "d2-tVH26q4tfS-a5gn7qFsPJ.tMgYq1",
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": false,
"Size": 4
}
]
}
看到在DeleteMarkers下增加了一個標記對象信息,這個DeleteMarkers下面的信息只是用來標記哪些對象被刪除過,實際不會占用存儲空間。
此時可以通過delete-object接口,然后指定DeleteMarkers里的這個version id來刪除這個標記,刪除后這個標記對應的對象就會變成當前最新的了。另一種情況,比如上傳key為k1,內容為11的對象,然后刪除k1,此時生成了DeleteMarkers version id為v1,再上傳key為k1,內容為22的對象后,此時去刪除這個version id v1,是不會把內容為11的對象恢復成最新的,最新的還是內容為22的對象,也就是說只有當DeleteMarkers對應的IsLatest是true的時候,刪除該version id對象才會恢復成當前最新的。
然后嘗試獲取該對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api get-object --bucket test-bucket-3 --key test-key-1 test-key-1.out
An error occurred (NoSuchKey) when calling the GetObject operation: Unknown
看到默認是獲取不到對象了(可以指定version-id去獲?。菍ο髷祿€是保留在rgw里面。開啟多版本時永久刪除對象方式就是指定version-id參數:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api delete-object --bucket test-bucket-3 --key test-key-1 --version-id d2-tVH26q4tfS-a5gn7qFsPJ.tMgYq1
{
"VersionId": "d2-tVH26q4tfS-a5gn7qFsPJ.tMgYq1"
}
2.4、恢復對象
先說結論:恢復一個對象,推薦的方法是copy你想恢復的對象版本到當前的bucket里面,那么該對象就是當前最新的了。
創建一個test-bucket-4:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api create-bucket --bucket test-bucket-4
開啟多版本:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-bucket-versioning --bucket test-bucket-4 --versioning-configuration Status=Enabled
上傳一個對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-object --bucket test-bucket-4 --key test-key-1 --body /tmp/test-2
{
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\""
}
查看對象信息:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket test-bucket-4
{
"Versions": [
{
"LastModified": "2019-11-22T06:20:05.328Z",
"VersionId": "CIkac6aeClGjppAcBpRvFKPQACWvdny",
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"Size": 4
}
]
}
刪除這個對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api delete-object --bucket test-bucket-4 --key test-key-1
{
"VersionId": "pwUBcmPcwRh2BnXOVB4juJcQGN7vkrL",
"DeleteMarker": true
}
恢復對象到之前的版本:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api copy-object --bucket test-bucket-4 --copy-source test-bucket-4/test-key-1?versionId=CIkac6aeClGjppAcBpRvFKPQACWvdny --key test-key-1
{
"CopyObjectResult": {
"LastModified": "2019-11-22T06:22:35.892Z",
"ETag": "348bd3ce10ec00ecc29d31ec97cd5839"
}
}
查看對象信息,可以看到版本為9bThYMWjdOYtQh8mmYzyulzFcOQoEwn的對象已經是當前最新的對象了(IsLatest字段為true):
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket test-bucket-4
{
"DeleteMarkers": [
{
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": false,
"VersionId": "pwUBcmPcwRh2BnXOVB4juJcQGN7vkrL",
"Key": "test-key-1",
"LastModified": "2019-11-22T06:20:36.817Z"
}
],
"Versions": [
{
"LastModified": "2019-11-22T06:22:35.892Z",
"VersionId": "9bThYMWjdOYtQh8mmYzyulzFcOQoEwn",
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"Size": 4
},
{
"LastModified": "2019-11-22T06:20:05.328Z",
"VersionId": "CIkac6aeClGjppAcBpRvFKPQACWvdny",
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": false,
"Size": 4
}
]
}
2.5、關閉多版本
先說結論:關閉多版本后,上傳對象會覆蓋當前最新的對象信息,并且刪除對象時會刪除version-id是null的對象信息。
創建一個bucket:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api create-bucket --bucket test-bucket-6
開啟多版本:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-bucket-versioning --bucket test-bucket-6 --versioning-configuration Status=Enabled
關閉多版本:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-bucket-versioning --bucket test-bucket-6 --versioning-configuration Status=Suspended
上傳一個對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-object --bucket test-bucket-6 --key test-key-1 --body /tmp/test-2
{
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\""
}
查看對象信息:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket test-bucket-6
{
"Versions": [
{
"LastModified": "2019-11-22T07:42:23.645Z",
"VersionId": "null",
"ETag": "\"348bd3ce10ec00ecc29d31ec97cd5839\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"Size": 4
}
]
}
再次上傳一個對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-object --bucket test-bucket-6 --key test-key-1 --body /tmp/test-3
{
"ETag": "\"40b134ab8a3dee5dd9760a7805fd495c\""
}
查看對象信息,可以看到關閉多版本之后,再次上傳對象會覆蓋之前的對象信息:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket test-bucket-6
{
"Versions": [
{
"LastModified": "2019-11-22T07:42:36.041Z",
"VersionId": "null",
"ETag": "\"40b134ab8a3dee5dd9760a7805fd495c\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"Size": 4
}
]
}
此時開啟多版本:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-bucket-versioning --bucket test-bucket-6 --versioning-configuration Status=Enabled
上傳一個對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-object --bucket test-bucket-6 --key test-key-1 --body /tmp/test-4
{
"ETag": "\"45a62d3d5d3e946250904697486591bc\""
}
查看對象信息:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket test-bucket-6
{
"Versions": [
{
"LastModified": "2019-11-22T07:43:06.724Z",
"VersionId": "JGTD3E5hznLQLprYC7pH3fGhYzId8Q6",
"ETag": "\"45a62d3d5d3e946250904697486591bc\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"Size": 4
},
{
"LastModified": "2019-11-22T07:42:36.041Z",
"VersionId": "null",
"ETag": "\"40b134ab8a3dee5dd9760a7805fd495c\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": false,
"Size": 4
}
]
}
關閉多版本:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api put-bucket-versioning --bucket test-bucket-6 --versioning-configuration Status=Suspended
刪除對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api delete-object --bucket test-bucket-6 --key test-key-1
{
"VersionId": "i6BI5Yzm07lX2A.vwP.ttGuaudC7h3K",
"DeleteMarker": true
}
查看對象信息,這里可以看到,關閉多版本后,刪除對象時,如果不指定version-id,會刪除version-id為null的對象:
[root@ceph06 ~]# aws --endpoint=http://192.168.10.30:9001 s3api list-object-versions --bucket test-bucket-6
{
"DeleteMarkers": [
{
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": true,
"VersionId": "null",
"Key": "test-key-1",
"LastModified": "2019-11-22T07:47:34.591Z"
}
],
"Versions": [
{
"LastModified": "2019-11-22T07:43:06.724Z",
"VersionId": "JGTD3E5hznLQLprYC7pH3fGhYzId8Q6",
"ETag": "\"45a62d3d5d3e946250904697486591bc\"",
"StorageClass": "STANDARD",
"Key": "test-key-1",
"Owner": {
"DisplayName": "rgwuser02",
"ID": "rgwuser02"
},
"IsLatest": false,
"Size": 4
}
]
}