8000 minio-py/minio/api.py at master · postalC/minio-py · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1811 lines (1494 loc) · 66.1 KB

api.py

File metadata and controls

1811 lines (1494 loc) · 66.1 KB
# hello/
# world/
objects = minio.list_objects_v2('foo', prefix='hello/')
for current_object in objects:
print(current_object)
# hello/world/
objects = minio.list_objects_v2('foo', recursive=True)
for current_object in objects:
print(current_object)
# hello/world/1
# world/world/2
# ...
objects = minio.list_objects_v2('foo', prefix='hello/',
recursive=True)
for current_object in objects:
print(current_object)
# hello/world/1
# hello/world/2
:param bucket_name: Bucket to list objects from
:param prefix: String specifying objects returned must begin with
:param recursive: If yes, returns all objects for a specified prefix
:return: An iterator of objects in alphabetical order.
"""
is_valid_bucket_name(bucket_name)
# Initialize query parameters.
query = {
'list-type': '2'
}
# Add if prefix present.
if prefix:
query['prefix'] = prefix
# Delimited by default.
if not recursive:
query['delimiter'] = '/'
continuation_token = None
is_truncated = True
while is_truncated:
if continuation_token is not None:
query['continuation-token'] = continuation_token
response = self._url_open(method='GET',
bucket_name=bucket_name,
query=query)
objects, is_truncated, continuation_token = parse_list_objects_v2(
response.data, bucket_name=bucket_name
)
for obj in objects:
yield obj
def stat_object(self, bucket_name, object_name):
"""
Check if an object exists.
:param bucket_name: Bucket of object.
:param object_name: Name of object
:return: Object metadata if object exists
"""
is_valid_bucket_name(bucket_name)
is_non_empty_string(object_name)
response = self._url_open('HEAD', bucket_name=bucket_name,
object_name=object_name)
etag = response.headers.get('etag', '').replace('"', '')
size = int(response.headers.get('content-length', '0'))
content_type = response.headers.get('content-type', '')
last_modified = response.headers.get('last-modified')
## Supported headers for object.
supported_headers = [
'cache-control',
'content-encoding',
'content-disposition',
## Add more supported headers here.
]
## Capture only custom metadata.
custom_metadata = dict()
for k in response.headers:
if k in supported_headers or k.lower().startswith('x-amz-meta-'):
custom_metadata[k] = response.headers.get(k)
if last_modified:
http_time_format = "%a, %d %b %Y %H:%M:%S GMT"
last_modified = strptime(last_modified, http_time_format)
return Object(bucket_name, object_name, last_modified, etag, size,
content_type=content_type, metadata=custom_metadata)
def remove_object(self, bucket_name, object_name):
"""
Remove an object from the bucket.
:param bucket_name: Bucket of object to remove
:param object_name: Name of object to remove
:return: None
"""
is_valid_bucket_name(bucket_name)
is_non_empty_string(object_name)
# No reason to store successful response, for errors
# relevant exceptions are thrown.
self._url_open('DELETE', bucket_name=bucket_name,
object_name=object_name)
def _process_remove_objects_batch(self, bucket_name, objects_batch):
"""
Requester and response parser for remove_objects
"""
# assemble request content for objects_batch
content = xml_marshal_delete_objects(objects_batch)
# compute headers
headers = {
'Content-Md5': get_md5_base64digest(content),
'Content-Length': len(content)
}
query = {'delete': ''}
content_sha256_hex = get_sha256_hexdigest(content)
# send multi-object delete request
response = self._url_open(
'POST', bucket_name=bucket_name,
headers=headers, body=content,
0