title: RGW對象版本控制(多版本)
前言
多版本就是字面意思:在相同的bucket中保留對象的多個版本兼耀。功能目的是為了可以恢復(fù)因意外刪除或覆蓋操作而失去的對象。
好久之前玩過這個功能炕倘,但是沒有記錄下來钧大,下面簡單記錄下。
1罩旋、實驗環(huán)境
三個虛擬機組成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
搭建標(biāo)準(zhǔn)的三節(jié)點集群:
[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節(jié)點配置了一個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節(jié)點作為rgw的客戶端瘸恼,安裝awscli工具:
[root@ceph06 ~]# yum install -y awscli
2劣挫、實際操作
注意:多版本是針對某個bucket去操作的册养。
2.1东帅、上傳對象
先說結(jié)論:一個沒有開啟多版本的bucket,上傳相同對象名的對象會覆蓋球拦,但是開啟了多版本的bucket靠闭,會保留對象之前的版本帐我。
創(chuàng)建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下的對象信息愧膀,看到之前上傳的對象信息已經(jīng)被覆蓋了:
[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
}
]
}
從上面的信息中可以看出拦键,現(xiàn)在有兩個版本的名為test-key-1的對象了,VersionId分別是null和urD9LXRy9.ZgoDaYNagoZh911SqJlbG檩淋,其中VersionId是urD9LXRy9.ZgoDaYNagoZh911SqJlbG的對象的IsLatest字段為true芬为,表示改版本的對象是當(dāng)前最新的。
2.2蟀悦、獲取對象
先說結(jié)論:沒有開啟多版本時媚朦,相同對象名的對象只有一個,獲取時也就只能獲取這一個了日戈。開啟了多版本時询张,默認(rèn)獲取當(dāng)前最新版本的對象,也可以顯示指定--version-id參數(shù)來獲取指定version的對象浙炼。
默認(rèn)獲取當(dāng)前最新的版本的對象:
[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份氧、刪除對象
先說結(jié)論:沒有開啟多版本時,刪除對象時將永久刪除弯屈,且不可恢復(fù)蜗帜。開啟了多版本時,分兩種情況资厉,一種是不指定--version-id刪除對象钮糖,此時rgw不會永久刪除該對象,只是給該對象打上一個被刪除標(biāo)記酌住,還有一種是顯示指定--version-id參數(shù)來刪除對象店归,此時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下增加了一個標(biāo)記對象信息都哭,這個DeleteMarkers下面的信息只是用來標(biāo)記哪些對象被刪除過秩伞,實際不會占用存儲空間。
此時可以通過delete-object接口欺矫,然后指定DeleteMarkers里的這個version id來刪除這個標(biāo)記纱新,刪除后這個標(biāo)記對應(yīng)的對象就會變成當(dāng)前最新的了。另一種情況穆趴,比如上傳key為k1脸爱,內(nèi)容為11的對象,然后刪除k1未妹,此時生成了DeleteMarkers version id為v1簿废,再上傳key為k1空入,內(nèi)容為22的對象后,此時去刪除這個version id v1族檬,是不會把內(nèi)容為11的對象恢復(fù)成最新的歪赢,最新的還是內(nèi)容為22的對象,也就是說只有當(dāng)DeleteMarkers對應(yīng)的IsLatest是true的時候单料,刪除該version id對象才會恢復(fù)成當(dāng)前最新的埋凯。
然后嘗試獲取該對象:
[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
看到默認(rèn)是獲取不到對象了(可以指定version-id去獲取)扫尖,但是對象數(shù)據(jù)還是保留在rgw里面递鹉。開啟多版本時永久刪除對象方式就是指定version-id參數(shù):
[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、恢復(fù)對象
先說結(jié)論:恢復(fù)一個對象藏斩,推薦的方法是copy你想恢復(fù)的對象版本到當(dāng)前的bucket里面躏结,那么該對象就是當(dāng)前最新的了。
創(chuàng)建一個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
}
恢復(fù)對象到之前的版本:
[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的對象已經(jīng)是當(dāng)前最新的對象了(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媳拴、關(guān)閉多版本
先說結(jié)論:關(guān)閉多版本后,上傳對象會覆蓋當(dāng)前最新的對象信息兆览,并且刪除對象時會刪除version-id是null的對象信息屈溉。
創(chuàng)建一個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
關(guān)閉多版本:
[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\""
}
查看對象信息,可以看到關(guān)閉多版本之后,再次上傳對象會覆蓋之前的對象信息:
[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
}
]
}
關(guān)閉多版本:
[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
}
查看對象信息抬探,這里可以看到子巾,關(guān)閉多版本后,刪除對象時小压,如果不指定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
}
]
}