Minio对象属性
Minio是一个对象存储服务器,不是一个数据库。因此,它不是一个关系型数据库或非关系型数据库,而是一个面向对象的存储系统。在Minio中,每个对象都是一个二进制文件,可以包含任何类型的数据。
虽然Minio不是一个数据库,但是它允许您在对象中存储元数据,这些元数据可以用来描述对象的属性。
文件类型
Content-Type
(也称为 MIME 类型)是一个 HTTP 标头,用于指示资源的媒体类型。在上传文件到 MinIO 时,Content-Type
告诉服务器和客户端文件的格式,以便它们能正确处理文件。以下是一些常见文件类型对应的 Content-Type
:
文本文件
文本类型 | Content-Type | 图像类型 | Content-Type | |
---|---|---|---|---|
.txt | text/plain | .jpg 或 .jpeg | image/jpeg | |
.csv | text/csv | .png | image/png | |
.html | text/html | .gif | image/gif | |
.css | text/css | .bmp | image/bmp | |
.js | text/javascript | .svg | image/svg+xml | |
.xml | text/xml | .webp | image/webp | |
音频文件 | Content-Type | 视频文件 | Content-Type | |
.mp3 | audio/mpeg | .mp4 | video/mp4 | |
.wav | audio/wav | .avi | video/x-msvideo | |
.ogg | audio/ogg | .mov | video/quicktime | |
.flac | audio/flac | .mkv | video/x-matroska | |
压缩文件 | Content-Type | 办公文件 | Content-Type | |
.zip | application/zip | .pdf | application/pdf | |
.rar | application/vnd.rar | .doc | application/msword | |
.7z | application/x-7z-compressed | .docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document | |
.tar | application/x-tar | .xls | application/vnd.ms-excel | |
.gz | application/gzip | .xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | |
.bz2 | application/x-bzip2 | .ppt | application/vnd.ms-powerpoint | |
.pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation | |||
文件扩展名 | Content-Type | |||
.json | application/json | |||
.exe | application/octet-stream |
自动推断 Content-Type
import mimetypes
file_name = "example.jpg"
content_type, _ = mimetypes.guess_type(file_name)
print(f"推断的 Content-Type 为: {content_type}")
使用
pip install minio
client
from minio import Minio
# Initialize Minio client
client = Minio('localhost:9000',
access_key='ACCESS_KEY',
secret_key='SECRET_KEY',
secure=False)
bucket
# 创建存储桶
client.make_bucket('mybucket')
# 检查存储桶是否存在
client.bucket_exists(bucker_name)
# 列出所有桶
buckets = client.list_buckets()
for bucket in buckets:
print(bucket.name, bucket.creation_date)
# 存储桶策略信息
policy = client.get_bucket_policy(bucket_name)
# 存储桶通知配置
notification = client.get_bucket_notification(bucket_name)
# 删除存储桶
client.remove_bucket("my-bucket")
object
# 上传文件1
client.fput_object(
bucket_name = 'mybucket', # 存储桶
object_name = 'myfile', # 文件名
file_path = '/path/to/local/file', # 文件路径
content_type = '',
metadata = {} # 自定义字典数据
)
# 上传文件2
file_data = open(file_path, 'rb')
with open(file_path, 'rb') as file_data:
file_stat = os.stat(file_path)
client.put_object(
bucket_name,
object_name,
file_data,
file_stat.st_size,
)
# 上传文件3
data_stream = io.BytesIO(data) # data 为2进制数据
data_length = len(data)
# 上传数据
client.put_object(
bucket_name, object_name, data_stream, data_length
)
# 下载文件1
client.fget_object(
"my-bucket", #
"my-object", #
"downloaded-file",
)
# 下载文件2
response = client.get_object(bucket_name, object_name)
with open(file_path, 'wb') as file_data:
for data in response.stream(32 * 1024):
file_data.write(data)
# 下载文件3
response = client.get_object(bucket_name, object_name)
# 创建 BytesIO 对象用于存储下载的数据
data_stream = io.BytesIO()
# 逐块读取数据并写入 BytesIO 对象
for data in response.stream(32 * 1024):
data_stream.write(data)
# 将文件指针移动到开头
data_stream.seek(0)
# 读取数据
downloaded_data = data_stream.read()
# 删除对象
client.remove_object("my-bucket", "my-object.txt")
# 对象状态
print(f"存储桶名称: {obj_stat.bucket_name}")
print(f"对象名称: {obj_stat.object_name}")
print(f"最后修改时间: {obj_stat.last_modified}")
print(f"ETag: {obj_stat.etag}")
print(f"对象大小: {obj_stat.size} 字节")
print(f"内容类型: {obj_stat.content_type}")
print(f"自定义元数据: {obj_stat.metadata}")
# 列出存储桶中的所有对象
objects = client.list_objects(bucket_name, recursive=True)
print(f"存储桶 {bucket_name} 中的所有对象名称:")
for obj in objects:
print(obj.object_name)