本文整理汇总了Python中oss2.ObjectIterator方法的典型用法代码示例。如果您正苦于以下问题:Python oss2.ObjectIterator方法的具体用法?Python oss2.ObjectIterator怎么用?Python oss2.ObjectIterator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类oss2
的用法示例。
在下文中一共展示了oss2.ObjectIterator方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_object_iterator
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import ObjectIterator [as 别名]
def test_object_iterator(self):
# ObjectIterator without payer setting, should be failed.
access_err_flag = False
try:
obj_iter = oss2.ObjectIterator(self.payer_bucket)
for obj in obj_iter:
pass
except oss2.exceptions.AccessDenied:
access_err_flag = True
self.assertEqual(access_err_flag, True)
# ObjectIterator with payer setting, should be failed.
headers = dict()
headers[OSS_REQUEST_PAYER] = "requester"
obj_iter = oss2.ObjectIterator(self.payer_bucket, headers=headers)
for obj in obj_iter:
pass
示例2: listdir
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import ObjectIterator [as 别名]
def listdir(self, name):
if name == ".":
name = ""
name = self._get_key_name(name)
if not name.endswith('/'):
name += "/"
logger().debug("name: %s", name)
files = []
dirs = []
for obj in ObjectIterator(self.bucket, prefix=name, delimiter='/'):
if obj.is_prefix():
dirs.append(obj.key)
else:
files.append(obj.key)
logger().debug("dirs: %s", list(dirs))
logger().debug("files: %s", files)
return dirs, files
示例3: listdir
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import ObjectIterator [as 别名]
def listdir(self, name):
name = self._normalize_name(self._clean_name(name))
if name and name.endswith('/'):
name = name[:-1]
files = []
dirs = set()
for obj in ObjectIterator(self.bucket, prefix=name, delimiter='/'):
if obj.is_prefix():
dirs.add(obj.key)
else:
files.append(obj.key)
return list(dirs), files
示例4: get_oss_version
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import ObjectIterator [as 别名]
def get_oss_version(project=None):
versions = []
try:
if project:
tt = time.strftime('%Y',time.localtime())
auth = oss2.Auth(oss_id, oss_key)
bucket = oss2.Bucket(auth, oss_url, 'ops')
db_packages = db_op.k8s_packages
packages = db_packages.query.with_entities(db_packages.package).filter(db_packages.deployment==project).all()
if packages:
package_name = packages[0][0].split('.')[0]
for obj in oss2.ObjectIterator(bucket):
if obj.key.endswith('.war') or obj.key.endswith('.tar.gz') or obj.key.endswith('.jar'):
obj_name = obj.key.split('/')[-1].replace('_','-')
project_name = project.replace('_','-')
if obj_name.startswith('%s-%s'%(package_name,tt)) or obj_name.startswith('%s-tag-%s'%(package_name,tt)) \
or obj_name.startswith('%s-%s'%(project_name,tt)):
try:
ver = obj_name.split(tt)[-1].split('.')[0]
version = '%s%s'%(tt,ver)
versions.append(version)
except:
pass
versions = list(set(versions))
versions.sort(reverse=True)
if len(versions) >15:
versions = versions[:15]
except Exception as e:
logging.error(e)
finally:
db_op.DB.session.remove()
return jsonify({project:versions})
示例5: test_object_iterator
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import ObjectIterator [as 别名]
def test_object_iterator(self):
prefix = self.random_key('/')
object_list = []
dir_list = []
# 准备文件
for i in range(20):
object_list.append(prefix + random_string(16))
self.bucket.put_object(object_list[-1], random_bytes(10))
# 准备目录
for i in range(5):
dir_list.append(prefix + random_string(5) + '/')
self.bucket.put_object(dir_list[-1] + random_string(5), random_bytes(3))
# 验证
objects_got = []
dirs_got = []
for info in oss2.ObjectIterator(self.bucket, prefix, delimiter='/', max_keys=4):
if info.is_prefix():
dirs_got.append(info.key)
else:
objects_got.append(info.key)
result = self.bucket.head_object(info.key)
self.assertEqual(result.last_modified, info.last_modified)
self.assertEqual(sorted(object_list), objects_got)
self.assertEqual(sorted(dir_list), dirs_got)
delete_keys(self.bucket, object_list)
示例6: test_object_iterator_chinese
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import ObjectIterator [as 别名]
def test_object_iterator_chinese(self):
for prefix in [self.random_key('中+文'), self.random_key(u'中+文')]:
self.bucket.put_object(prefix, b'content of object')
object_got = list(oss2.ObjectIterator(self.bucket, prefix=prefix, max_keys=1))[0].key
self.assertEqual(to_string(prefix), object_got)
示例7: clean_and_delete_bucket
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import ObjectIterator [as 别名]
def clean_and_delete_bucket(bucket):
# check if bucket is in versioning status
try:
result = bucket.get_bucket_info()
if result.versioning_status in [oss2.BUCKET_VERSIONING_ENABLE, oss2.BUCKET_VERSIONING_SUSPEND]:
next_key_marker = None
next_versionid_marker = None
is_truncated = True
while is_truncated is True:
objects = bucket.list_object_versions(key_marker=next_key_marker, versionid_marker=next_versionid_marker)
for obj in objects.versions:
bucket.delete_object(obj.key, params={'versionId': obj.versionid})
for del_marker in objects.delete_marker:
bucket.delete_object(del_marker.key, params={'versionId': del_marker.versionid})
is_truncated = objects.is_truncated
if is_truncated:
next_key_marker = objects.next_key_marker
next_versionid_marker = objects.next_versionid_marker
except:
pass
# list all upload_parts to delete
up_iter = oss2.MultipartUploadIterator(bucket)
for up in up_iter:
bucket.abort_multipart_upload(up.key, up.upload_id)
# list all objects to delete
obj_iter = oss2.ObjectIterator(bucket)
for obj in obj_iter:
bucket.delete_object(obj.key)
# list all live channels to delete
for ch_iter in oss2.LiveChannelIterator(bucket):
bucket.delete_live_channel(ch_iter.name)
# delete_bucket
bucket.delete_bucket()
示例8: test_put_get_list_delete
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import ObjectIterator [as 别名]
def test_put_get_list_delete(self):
for key in ['中文!@#$%^&*()-=文件\x0C-1.txt', u'中文!@#$%^&*()-=文件\x0C-1.txt']:
content = '中文内容'
self.bucket.put_object(key, content)
self.assertEqual(self.bucket.get_object(key).read(), to_bytes(content))
self.assertTrue(to_string(key) in list(info.key for info in oss2.ObjectIterator(self.bucket, prefix='中文')))
self.bucket.delete_object(key)
示例9: list_file
# 需要导入模块: import oss2 [as 别名]
# 或者: from oss2 import ObjectIterator [as 别名]
def list_file(self):
# 遍历Bucket里所有文件
for object_info in oss2.ObjectIterator(self.bucket):
print(object_info.key)