这篇文章主要讲解了“基于boto的s3cmd versioning功能怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“基于boto的s3cmd versioning功能怎么使用”吧!
tips
1. 开启versioning特性是针对整个bucket的,不能针对单独的object启停该特性。
2. 开启versioning以后,所有的操作都要基于object对应的version-id进行操作。
3. object的ACL权限设置取决于最新的object版本
4. 开启versioning以后,如果需要删除object,应该先删除对应object的所有version,之后再删除object。
5. 开启versioning以后,object的每个version都会占用一个独立的object空间,所以合理规划避免空间的浪费。
6. 测试jewel(ceph 10.2.6)的AWS2和AWS4都可以使用该特性,暂未发现有bug。
boto用例及说明
# -*- coding: utf-8 -*-
from boto.s3.connection import S3Connection
import boto
import os
import requests
os.environ['S3_USE_SIGV4'] = 'True'
endpoint = 's3.ceph.work'
bucket_name = 'version-test4'
access_key = ''
secret_key = ''
key_name = 'version_object2'
conn = boto.connect_s3(
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
host=endpoint,
is_secure=False,
calling_format=boto.s3.connection.SubdomainCallingFormat(),
validate_certs=True,
)
content_info = 'content_'
#新建bucket并启用versioning特性
bucket = conn.create_bucket(bucket_name)
print "get versioning status={}".format(bucket.get_versioning_status())
bucket.configure_versioning(True)
print "afert set versioning=enable,get versioning status={}".format(bucket.get_versioning_status())
#新建一个obj的5个不同版本
for i in range(5):
content_ = "{}{}".format(content_info,i)
key_ = bucket.new_key(key_name)
key_.set_metadata('metadata',i)
key_.set_contents_from_string(content_)
#获取obj的versioning列表及相关属性
bucket = conn.get_bucket(bucket_name)
version_list_ = []
for i in bucket.list_versions(prefix=key_name):
key_ = bucket.get_key(key_name,version_id=i.version_id)
print "content={} version_id={} etag={} acl={} metadata={}".format(key_.get_contents_as_string(version_id=i.version_id),i.version_id,i.etag,bucket.get_acl(key_name,version_id=i.version_id),key_.get_metadata('metadata'))
version_list_.append(i.version_id)
#带verioning的obj的ACL设置
key_ = bucket.get_key(key_name)
print "get key acl={}".format(key_.get_acl())
key_.set_acl('public-read')
print "after set key acl=public-read,get key acl={}".format(key_.get_acl())
url_ = "http://{}.{}/{}".format(bucket_name,endpoint,key_name)
resp = requests.get(url_)
#下载会失败
if resp.status_code == 200:
print "before set acl=public-read -> download successful!"
else:
print "before set acl=public-read -> download failed"
#设置最新的version-id对应的obj的ACL
bucket.set_acl('public-read', key_name, version_id=version_list_[0])
resp = requests.get(url_)
#成功下载
if resp.status_code == 200:
print "after set acl=public-read ->download successful!"
else:
print "after set acl=public-read -> download failed"
#当前obj的ACL就是最新version-id的ACL
key_ = bucket.get_key(key_name)
print "get key acl = {}".format(key_.get_acl())
key_ = bucket.get_key(key_name,version_id=version_list_[0])
print "get key version_id={} acl= {}".format(version_list_[0],key_.get_acl())
#生成当前obj的URL
key_ = bucket.get_key(key_name)
print "generate_url={}".format(key_.generate_url(300))
#version 回滚操作,获取当前object信息
key_ = bucket.get_key(key_name)
print "before roll back, content={} metadata={}".format(key_.get_contents_as_string(),key_.get_metadata('metadata'))
#回滚到指定version_id,并获取当前对应版本的object信息
key_ = bucket.get_key(key_name,version_id=version_list_[-1])
metadata_ = {'metadata': key_.get_metadata('metadata')}
print "roll back to version_id={} content_={} metadata={}".format(version_list_[-1],key_.get_contents_as_string(version_id=version_list_[-1]),key_.get_metadata('metadata'))
bucket.copy_key(key_name, bucket_name, key_name ,metadata=metadata_, src_version_id=version_list_[-1])
#回滚完成,检查当前object信息
key_ = bucket.get_key(key_name)
print "after roll back, content={} metadata={}".format(key_.get_contents_as_string(),key_.get_metadata('metadata'))
#删除key
key_ = bucket.get_key(key_name)
key_.delete()
bucket = conn.get_bucket(bucket_name)
#obj对应的 versioning 依然存在
for i in bucket.list_versions(prefix=key_name):
key_ = bucket.get_key(key_name)
print "key {} was deleted,but version_id={} remain".format(i.name,i.version_id)
#就算关闭versioning, obj对应的 versioning 依然存在
bucket.configure_versioning(False)
for i in bucket.list_versions(prefix=key_name):
key_ = bucket.get_key(key_name)
print "get key={} version_id={}".format(i.name,i.version_id)
#批量删除versioning
for i in bucket.list_versions(prefix=key_name):
bucket.delete_key(key_name,version_id=i.version_id)
print "deleted versioning -> {}".format(i.version_id)
# print i.name,i.version_id
#彻底清除
for i in bucket.list_versions(prefix=key_name):
key_ = bucket.get_key(key_name)
print i.name,i.version_id
感谢各位的阅读,以上就是“基于boto的s3cmd versioning功能怎么使用”的内容了,经过本文的学习后,相信大家对基于boto的s3cmd versioning功能怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是天达云,小编将为大家推送更多相关知识点的文章,欢迎关注!