Minio文件服務使用API文檔-Python

1.安裝包
 pip install minio
 官方API文檔鏈接:https://www.bookstack.cn/read/MinioCookbookZH/24.md
2.建立連接
from minio import Minio
minioClient = Minio(
                    endpoint='minio.xxx.com',           # 文件服務地址
                    access_key='admin',                       # 用戶名
                    secret_key='admin',                  # 密鑰
                    secure=False)                           # 設爲True代表啓用HTTPS
3.操作存儲桶(文件夾)
3.1創建桶

minioClient.make_bucket(“mybucket”, location=“us-east-1”)
location存儲的位置(分區地址),默認是us-east-1(美國東一區)

3.2判斷桶存在

minioClient.bucket_exists(“mybucket”)

3.3刪除桶

minioClient.remove_bucket(“mybucket”)

4.操作文件對象
4.1 下載文件
4.1.1 獲取文件數據
get_object(bucket_name, file_name, request_headers=None)   
示例:
   data = minioClient.get_object('mybucket', '0722.jpg')
   with open('my-testfile', 'wb') as file_data:
   for d in data.stream(32*1024):
       file_data.write(d)       
4.1.2 直接下載到本地指定目錄:

minioClient.fget_object(‘mybucket’, ‘myobject’, ‘/tmp/myobject’)

4.2上傳文件
4.2.1以數據流格式上傳

put_object(bucket_name, file_name, file_data, length, content_type=‘application/octet-stream’, metadata=None)

4.2.2 以文件路徑上傳

若文件已存在,會直接覆蓋
fput_object(bucket_name, object_name, file_path, content_type=‘application/octet-stream’, metadata=None)

4.3刪除文件

remove_object(bucket_name, file_name)

4.4 刪除多個文件

remove_objects(bucket_name, file_iter) # file_iter爲list;示例:[test1.txt, test2.txt]

5.問題
a.獲取文件url最長時間期限只能設置爲7天?
6.解決
a.通過桶權限設置方法,修改時間期限限制。
    set_bucket_policy(policy)
    示例:更改桶權限 設置公共可下載
      policy = '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetBucketLocation","s3:ListBucket"],"Resource":["arn:aws:s3:::%s"]},{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetObject"],"Resource":["arn:aws:s3:::%s/*"]}]}' % (bucket_name, bucket_name)
      minioClient.set_bucket_policy(bucket_name=bucket_name, policy=policy)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章