pip install minio
官方API文档链接:https://www.bookstack.cn/read/MinioCookbookZH/24.md
from minio import Minio
minioClient = Minio(
endpoint='minio.xxx.com', # 文件服务地址
access_key='admin', # 用户名
secret_key='admin', # 密钥
secure=False) # 设为True代表启用HTTPS
minioClient.make_bucket(“mybucket”, location=“us-east-1”)
location存储的位置(分区地址),默认是us-east-1(美国东一区)
minioClient.bucket_exists(“mybucket”)
minioClient.remove_bucket(“mybucket”)
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)
minioClient.fget_object(‘mybucket’, ‘myobject’, ‘/tmp/myobject’)
put_object(bucket_name, file_name, file_data, length, content_type=‘application/octet-stream’, metadata=None)
若文件已存在,会直接覆盖
fput_object(bucket_name, object_name, file_path, content_type=‘application/octet-stream’, metadata=None)
remove_object(bucket_name, file_name)
remove_objects(bucket_name, file_iter) # file_iter为list;示例:[test1.txt, test2.txt]
a.获取文件url最长时间期限只能设置为7天?
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)