In [24]:
import os
from minio import Minio
In [1]:
!pip install minio
Collecting minio
  Downloading https://files.pythonhosted.org/packages/34/9c/d22de883dc6f85035e478efcef2fe8287415aad0d2e90d2fc2a0fc24d8e6/minio-7.1.8-py3-none-any.whl (75kB)
     |████████████████████████████████| 81kB 13.1MB/s 
Requirement already satisfied: certifi in /opt/conda/lib/python3.7/site-packages (from minio) (2019.9.11)
Requirement already satisfied: urllib3 in /opt/conda/lib/python3.7/site-packages (from minio) (1.24.2)
Installing collected packages: minio
Successfully installed minio-7.1.8
In [ ]:
 
In [3]:
client = Minio(
    "qbittorrent:3000",
    "minioadmin",
    "minioadmin",
    secure=False,
)
In [4]:
filename = "thalia_caetano15_279970453_1051143635478514_141905778914952102_n.mp4"
In [5]:
client.fput_object("test", filename, filename)
Out[5]:
<minio.helpers.ObjectWriteResult at 0x7f0270eb8f60>
In [11]:
for obj in client.list_objects("test", recursive=True):
    print(obj.object_name)
thalia_caetano15_279970453_1051143635478514_141905778914952102_n.mp4
In [44]:
a = client.stat_object("test", "shoudl_break")
---------------------------------------------------------------------------
S3Error                                   Traceback (most recent call last)
<ipython-input-44-c4b41b348fb4> in <module>
----> 1 a = client.stat_object("test", "shoudl_break")

/opt/conda/lib/python3.7/site-packages/minio/api.py in stat_object(self, bucket_name, object_name, ssec, version_id, extra_query_params)
   1862             object_name,
   1863             headers=headers,
-> 1864             query_params=query_params,
   1865         )
   1866 

/opt/conda/lib/python3.7/site-packages/minio/api.py in _execute(self, method, bucket_name, object_name, body, headers, query_params, preload_content, no_body_trace)
    404                 query_params=query_params,
    405                 preload_content=preload_content,
--> 406                 no_body_trace=no_body_trace,
    407             )
    408         except S3Error as exc:

/opt/conda/lib/python3.7/site-packages/minio/api.py in _url_open(self, method, region, bucket_name, object_name, body, headers, query_params, preload_content, no_body_trace)
    378             self._region_map.pop(bucket_name, None)
    379 
--> 380         raise response_error
    381 
    382     def _execute(

S3Error: S3 operation failed; code: NoSuchKey, message: Object does not exist, resource: /test/shoudl_break, request_id: 16EE8DBE52F0349F, host_id: None, bucket_name: test, object_name: shoudl_break
In [18]:
a = client.stat_object("test", filename)
In [19]:
dir(a)
Out[19]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_bucket_name',
 '_content_type',
 '_etag',
 '_is_delete_marker',
 '_is_latest',
 '_last_modified',
 '_metadata',
 '_object_name',
 '_owner_id',
 '_owner_name',
 '_size',
 '_storage_class',
 '_version_id',
 'bucket_name',
 'content_type',
 'etag',
 'fromxml',
 'is_delete_marker',
 'is_dir',
 'is_latest',
 'last_modified',
 'metadata',
 'object_name',
 'owner_id',
 'owner_name',
 'size',
 'storage_class',
 'version_id']
In [20]:
type(a)
Out[20]:
minio.datatypes.Object
In [29]:
a.metadata
Out[29]:
HTTPHeaderDict({'Server': 'nginx/1.19.2', 'Date': 'Fri, 13 May 2022 03:11:09 GMT', 'Content-Type': 'application/octet-stream', 'Content-Length': '3447049', 'Connection': 'keep-alive', 'Accept-Ranges': 'bytes', 'Content-Security-Policy': 'block-all-mixed-content', 'ETag': '"cd605ac35ed46d4ad185b5bee0e856b8"', 'Last-Modified': 'Fri, 13 May 2022 03:07:34 GMT', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 'Vary': 'Origin, Accept-Encoding', 'X-Amz-Request-Id': '16EE8B7A1DCD5CF7', 'X-Content-Type-Options': 'nosniff', 'X-Xss-Protection': '1; mode=block'})
In [43]:
a.metadata.get('ETag')
Out[43]:
'"cd605ac35ed46d4ad185b5bee0e856b8"'
In [21]:
a.size
Out[21]:
3447049
In [26]:
os.stat(filename)
Out[26]:
os.stat_result(st_mode=33188, st_ino=1202574, st_dev=64768, st_nlink=1, st_uid=0, st_gid=0, st_size=3447049, st_atime=1652411253, st_mtime=1652411142, st_ctime=1652411142)
In [28]:
os.stat(filename).st_size
Out[28]:
3447049
In [35]:
os.popen(f"md5sum {filename}").read()
Out[35]:
'cd605ac35ed46d4ad185b5bee0e856b8  thalia_caetano15_279970453_1051143635478514_141905778914952102_n.mp4\n'
In [34]:
!md5sum thalia_caetano15_279970453_1051143635478514_141905778914952102_n.mp4
cd605ac35ed46d4ad185b5bee0e856b8  thalia_caetano15_279970453_1051143635478514_141905778914952102_n.mp4
In [ ]:
 
In [39]:
# https://stackoverflow.com/questions/3431825/generating-an-md5-checksum-of-a-file
import hashlib

def hash_bytestr_iter(bytesiter, hasher, ashexstr=False):
    for block in bytesiter:
        hasher.update(block)
    return hasher.hexdigest() if ashexstr else hasher.digest()

def file_as_blockiter(afile, blocksize=65536):
    with afile:
        block = afile.read(blocksize)
        while len(block) > 0:
            yield block
            block = afile.read(blocksize)

def getmd5sum(filepath):
    return hash_bytestr_iter(file_as_blockiter(open(filepath, 'rb')), hashlib.md5(), True)
In [40]:
getmd5sum(filename)
Out[40]:
'cd605ac35ed46d4ad185b5bee0e856b8'
In [ ]:
 
In [45]:
def check_diff_size(bucket, key, filepath):
    try:
        obj = client.stat_object(bucket, key)
        obj_size = obj.size
        
        fileobj = os.stat(filepath)
        fileobj_size = fileobj.st_size
        
        return obj_size == fileobj_size
    except:
        return False
In [50]:
check_diff_size("test", filename, filename)
Out[50]:
True
In [ ]:
 
In [ ]:
 

File handling

In [ ]:
 
In [6]:
import io
import json
In [ ]:
 
In [5]:
data = {
    "a": 2,
    "b": 3
}
In [7]:
buf = io.BytesIO("asdfasdf".encode('utf-8'))
In [10]:
size = buf.getbuffer().nbytes
In [11]:
client.put_object("test", "testfile", buf, size)
Out[11]:
<minio.helpers.ObjectWriteResult at 0x7fcdc818c7f0>
In [15]:
client.get_object("test", "testfile").read().decode('utf-8')
Out[15]:
'asdfasdf'
In [ ]:
 
In [53]:
def minio_save_key(bucket, key, content):
    buf = io.BytesIO(content.encode('utf-8'))
    size = buf.getbuffer().nbytes()
    client.put_object(bucket, key, buf, size)
In [54]:
def minio_get_key(bucket, key):
    return client.get_object(bucket, key).read().decode('utf-8')
In [ ]:
 
In [ ]: