Skip to content

Commit

Permalink
fix formatting and use pythonic approach
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurugana committed Apr 14, 2020
1 parent cb15f0a commit 0a12062
Show file tree
Hide file tree
Showing 28 changed files with 182 additions and 119 deletions.
3 changes: 2 additions & 1 deletion examples/aws_credentials.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2020 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion examples/bucket_exists.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
12 changes: 8 additions & 4 deletions examples/chain_credentials.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2020 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,10 +14,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# A Chain credentials provider, provides a way of chaining multiple providers together
# and will pick the first available using priority order of the 'providers' list
# A Chain credentials provider, provides a way of chaining multiple providers
# together and will pick the first available using priority order of the
# 'providers' list

from minio.credentials import Chain, EnvAWS, EnvMinio, IamEc2MetaData
from minio import Minio
from minio.credentials import (
Chain, EnvAWS, EnvMinio, IamEc2MetaData, Credentials)

client = Minio('s3.amazonaws.com',
credentials=Credentials(
Expand Down
13 changes: 6 additions & 7 deletions examples/copy_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2016 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2016 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,8 +17,7 @@
# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-testfile, my-bucketname and
# my-objectname are dummy values, please replace them with original values.

import time
from datetime import datetime
from datetime import datetime, timezone

from minio import Minio, CopyConditions
from minio.error import ResponseError
Expand All @@ -28,12 +28,11 @@

# client.trace_on(sys.stderr)
copy_conditions = CopyConditions()
# Set modified condition, copy object modified since 2014 April.
t = (2014, 4, 0, 0, 0, 0, 0, 0, 0)
mod_since = datetime.utcfromtimestamp(time.mktime(t))
# Set modified condition, copy object modified since 1st April 2014.
mod_since = datetime(2014, 4, 1, tzinfo=timezone.utc)
copy_conditions.set_modified_since(mod_since)

# Set unmodified condition, copy object unmodified since 2014 April.
# Set unmodified condition, copy object unmodified since 1st April 2014.
# copy_conditions.set_unmodified_since(mod_since)

# Set matching ETag condition, copy object which matches the following ETag.
Expand Down
4 changes: 3 additions & 1 deletion examples/copy_object_with_metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2016,2017,2018 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2016,2017,2018 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,6 +17,7 @@
# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-testfile, my-bucketname and
# my-objectname are dummy values, please replace them with original values.

from minio import Minio
from minio.error import ResponseError

client = Minio('s3.amazonaws.com',
Expand Down
7 changes: 4 additions & 3 deletions examples/fget_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,8 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname and my-objectname are
# dummy values, please replace them with original values.
# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname and my-objectname
# are dummy values, please replace them with original values.

from minio import Minio
from minio.error import ResponseError
Expand Down
7 changes: 4 additions & 3 deletions examples/fput_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,8 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname, my-objectname and
# my-filepath dummy values, please replace them with original values.
# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname, my-objectname
# and my-filepath dummy values, please replace them with original values.

from minio import Minio
from minio.error import ResponseError
Expand Down
7 changes: 4 additions & 3 deletions examples/get_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,8 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname, my-objectname and
# my-testfile are dummy values, please replace them with original values.
# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname, my-objectname
# and my-testfile are dummy values, please replace them with original values.

from minio import Minio
from minio.error import ResponseError
Expand Down
7 changes: 4 additions & 3 deletions examples/get_partial_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,8 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname, my-objectname and
# my-testfile are dummy values, please replace them with original values.
# Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname, my-objectname
# and my-testfile are dummy values, please replace them with original values.

from minio import Minio
from minio.error import ResponseError
Expand Down
3 changes: 2 additions & 1 deletion examples/list_buckets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion examples/list_incomplete_uploads.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion examples/list_objects.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion examples/make_bucket.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
3 changes: 2 additions & 1 deletion examples/presigned_get_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
23 changes: 13 additions & 10 deletions examples/presigned_post_policy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,8 +14,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Note: my-bucketname, my-objectname, YOUR-ACCESSKEYID, and YOUR-SECRETACCESSKEY
# are dummy values, please replace them with original values.
# Note: my-bucketname, my-objectname, YOUR-ACCESSKEYID, and
# YOUR-SECRETACCESSKEY are dummy values, please replace them with original
# values.

from datetime import datetime, timedelta

Expand All @@ -31,22 +33,23 @@
post_policy.set_content_length_range(10, 1024)

# set expiry 10 days into future.
expires_date = datetime.utcnow()+timedelta(days=10)
expires_date = datetime.utcnow() + timedelta(days=10)
post_policy.set_expires(expires_date)

client = Minio('s3.amazonaws.com',
access_key='YOUR-ACCESSKEYID',
secret_key='YOUR-SECRETACCESSKEY')

try:
url_str, signed_form_data = client.presigned_post_policy(post_policy)
curl_str = 'curl -X POST {0}'.format(url_str)
curl_cmd = [curl_str]
for field in signed_form_data:
curl_cmd.append('-F {0}={1}'.format(field, signed_form_data[field]))
url, signed_form_data = client.presigned_post_policy(post_policy)

curl_cmd = (
['curl -X POST {0}'.format(url)] +
['-F {0}={1}'.format(k, v) for k, v in signed_form_data.items()] +
['-F file=@<FILE>']
)

# print curl command to upload files.
curl_cmd.append('-F file=@<FILE>')
print(' '.join(curl_cmd))
except ResponseError as err:
print(err)
3 changes: 2 additions & 1 deletion examples/presigned_put_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) 2015 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2015 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
42 changes: 24 additions & 18 deletions examples/progress.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C)
# 2018 MinIO, Inc.
# MinIO Python Library for Amazon S3 Compatible Cloud Storage,
# (C) 2018 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -73,7 +73,8 @@ def __init__(self, interval=1, stdout=sys.stdout):

def set_meta(self, total_length, object_name):
"""
Metadata settings for the object. This method called before uploading object
Metadata settings for the object. This method called before uploading
object
:param total_length: Total length of object.
:param object_name: Object name to be showed.
"""
Expand All @@ -82,7 +83,6 @@ def set_meta(self, total_length, object_name):
self.prefix = self.object_name + ': ' if self.object_name else ''

def run(self):

displayed_time = 0
while True:
try:
Expand All @@ -92,22 +92,27 @@ def run(self):
elapsed_time = time.time() - self.initial_time
if elapsed_time > displayed_time:
displayed_time = elapsed_time
self.print_status(current_size=self.current_size, total_length=self.total_length,
displayed_time=displayed_time, prefix=self.prefix)
self.print_status(current_size=self.current_size,
total_length=self.total_length,
displayed_time=displayed_time,
prefix=self.prefix)
continue

current_size, total_length = task
displayed_time = time.time() - self.initial_time
self.print_status(current_size=current_size, total_length=total_length,
displayed_time=displayed_time, prefix=self.prefix)
self.print_status(current_size=current_size,
total_length=total_length,
displayed_time=displayed_time,
prefix=self.prefix)
self.display_queue.task_done()
if current_size == total_length:
self.done_progress()

def update(self, size):
"""
Update object size to be showed. This method called while uploading
:param size: Object size to be showed. The object size should be in bytes.
:param size: Object size to be showed. The object size should be in
bytes.
"""
if not isinstance(size, int):
raise ValueError('{} type can not be displayed. '
Expand All @@ -123,9 +128,8 @@ def done_progress(self):
self.current_size = 0

def print_status(self, current_size, total_length, displayed_time, prefix):

formatted_str = prefix + \
format_string(current_size, total_length, displayed_time)
formatted_str = prefix + format_string(
current_size, total_length, displayed_time)
self.stdout.write(_REFRESH_CHAR + formatted_str + ' ' *
max(self.last_printed_len - len(formatted_str), 0))
self.stdout.flush()
Expand Down Expand Up @@ -160,15 +164,17 @@ def format_string(current_size, total_length, elapsed_time):
n_to_mb / elapsed_time) if elapsed_time else _UNKNOWN_SIZE
frac = float(current_size) / total_length
bar_length = int(frac * _BAR_SIZE)
bar = _FINISHED_BAR * bar_length + \
_REMAINING_BAR * (_BAR_SIZE - bar_length)
bar = (_FINISHED_BAR * bar_length +
_REMAINING_BAR * (_BAR_SIZE - bar_length))
percentage = _PERCENTAGE_FORMAT % (frac * 100)
left_str = seconds_to_time(
elapsed_time / current_size * (total_length - current_size)) if current_size else _UNKNOWN_SIZE
left_str = (
seconds_to_time(
elapsed_time / current_size * (total_length - current_size))
if current_size else _UNKNOWN_SIZE)

humanized_total = _HUMANINZED_FORMAT % (
total_length / _KILOBYTE / _KILOBYTE) + _STR_MEGABYTE
humanized_n = _HUMANINZED_FORMAT % n_to_mb + _STR_MEGABYTE

return _DISPLAY_FORMAT % (
bar, humanized_n, humanized_total, percentage, elapsed_str, left_str, rate)
return _DISPLAY_FORMAT % (bar, humanized_n, humanized_total, percentage,
elapsed_str, left_str, rate)

0 comments on commit 0a12062

Please sign in to comment.