MinIO 可以被当做一个轻量级的云盘或文件数据库,默认存储在桶里的文件只能分享7天,但是当我想把它当做文件数据库时,就需要将文件分享设置为永久,那么该如何做呢?
官方文档在 MinIO Admin 中有对桶的策略进行设置:
将桶设置为可永久分享:
bucket_policy = {
'Version': '2012-10-17',
'Statement': [
{
'Effect': 'Allow',
'Principal': {'AWS': ['*']},
'Action': ['s3:GetBucketLocation', 's3:ListBucket'],
'Resource': [f'arn:aws:s3:::{bucket_name}']
},
{
'Effect': 'Allow',
'Principal': {'AWS': ['*']},
'Action': ['s3:GetObject'],
'Resource': [f'arn:aws:s3:::{bucket_name}/*']
}
]
}
至于配置中 Version 为什么是 '2012-10-17’呢,别问我,我也不知道。
因为我们公司使用 Flask 框架,所以我最后把它封装成一个 flask app 的应用:文章来源:https://www.toymoban.com/news/detail-515060.html
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import json
from minio import Minio
class MinioLib(object):
def __init__(self, app=None):
self.endpoint = None
self.access_key = None
self.secret_key = None
self.minio = None
if app:
self.init_app(app)
def init_app(self, app):
self.endpoint = app.config['MINIO_ENDPOINT']
self.access_key = app.config['MINIO_ACCESS_KEY']
self.secret_key = app.config['MINIO_SECRET_KEY']
self.minio = Minio(
endpoint=self.endpoint, access_key=self.access_key, secret_key=self.secret_key
)
def init_bucket_policy(self, bucket_name):
""" 初始化桶策略-设置新桶永久分享策略 """
bucket_policy = {
'Version': '2012-10-17',
'Statement': [
{
'Effect': 'Allow',
'Principal': {'AWS': ['*']},
'Action': ['s3:GetBucketLocation', 's3:ListBucket'],
'Resource': [f'arn:aws:s3:::{bucket_name}']
},
{
'Effect': 'Allow',
'Principal': {'AWS': ['*']},
'Action': ['s3:GetObject'],
'Resource': [f'arn:aws:s3:::{bucket_name}/*']
}
]
}
self.minio.set_bucket_policy(bucket_name=bucket_name, policy=json.dumps(bucket_policy))
def init_bucket(self, bucket_name):
""" 初始化桶 """
if self.minio.bucket_exists(bucket_name) is False:
# 创建新桶
self.minio.make_bucket(bucket_name)
# 设置新桶永久分享策略
self.init_bucket_policy(bucket_name)
def upload_file(self, bucket_name, local_path, cloud_path):
""" 上传文件 """
self.init_bucket(bucket_name)
result = self.minio.fput_object(bucket_name=bucket_name, object_name=cloud_path, file_path=local_path)
path_url = 'https://' + self.endpoint + '/' + result.bucket_name + '/' + result.object_name
return path_url
if __name__ == '__main__':
class APP(object):
config = {
'MINIO_ENDPOINT': 'xxx',
'MINIO_ACCESS_KEY': 'xxx',
'MINIO_SECRET_KEY': 'xxx'
}
app = APP()
minio = MinioLib()
minio.init_app(app)
bucket_name = 'avatar'
local_path = '/Users/zhangyi/Downloads/字节跳动技术沙龙.png'
cloud_path = 'file/it/字节跳动技术沙龙.png'
path_url = minio.upload_file(bucket_name, local_path, cloud_path)
print(path_url)
MinIO 官网文档:https://docs.min.io/docs/
MinIO 中文文档:http://docs.minio.org.cn/docs/文章来源地址https://www.toymoban.com/news/detail-515060.html
到了这里,关于MinIO 如何设置永久分享链接的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!