Python Quickstart Guide
MinIO Python Client SDK for Amazon S3 Compatible Cloud Storage

MinIO Python Client SDK 提供高级 API,可用于访问任意 MinIO 对象存储或其他兼容 Amazon S3 的服务。
本快速入门指南介绍如何安装 MinIO Python Client SDK、连接对象存储服务并创建一个示例文件上传器。
以下示例使用:
- Python version 3.7+
- MinIO
mc命令行工具 - MinIO
play测试服务器
play 服务器是位于 https://play.min.io 的公开 MinIO 集群。 该集群运行 MinIO 的最新稳定版本,可用于测试与开发。 示例中的访问凭证对公众开放,上传到 play 的所有数据都应视为公开且全网可读。
如需查看完整的 API 与示例列表,请参阅 Python Client API Reference
安装 MinIO Python SDK
Python SDK 要求 Python 版本 3.7+。 你可以使用 pip 安装 SDK,或从 minio/minio-py GitHub 仓库 安装:
使用 pip
pip3 install minio
使用 GitHub 源码安装
git clone https://github.com/minio/minio-py
cd minio-py
python setup.py install
创建 MinIO Client
要连接目标服务,请使用 Minio() 方法并传入以下必需参数来创建 MinIO Client:
| Parameter | Description |
|---|---|
endpoint | 目标服务的 URL。 |
access_key | 服务中某个用户账户的 Access key(用户 ID)。 |
secret_key | 用户账户的 Secret key(密码)。 |
例如:
from minio import Minio
client = Minio("play.min.io",
access_key="Q3AM3UQ867SPQQA43P2F",
secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
)
示例:文件上传器
此示例执行以下操作:
- 使用提供的凭证连接到 MinIO
play服务器。 - 如果不存在名为
python-test-bucket的存储桶,则创建该存储桶。 - 从
/tmp上传名为test-file.txt的文件,并将其重命名为my-test-file.txt。 - 使用
mc ls验证文件已创建。
file_uploader.py
# file_uploader.py MinIO Python SDK example
from minio import Minio
from minio.error import S3Error
def main():
# Create a client with the MinIO server playground, its access key
# and secret key.
client = Minio("play.min.io",
access_key="Q3AM3UQ867SPQQA43P2F",
secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
)
# The file to upload, change this path if needed
source_file = "/tmp/test-file.txt"
# The destination bucket and filename on the MinIO server
bucket_name = "python-test-bucket"
destination_file = "my-test-file.txt"
# Make the bucket if it doesn't exist.
found = client.bucket_exists(bucket_name)
if not found:
client.make_bucket(bucket_name)
print("Created bucket", bucket_name)
else:
print("Bucket", bucket_name, "already exists")
# Upload the file, renaming it in the process
client.fput_object(
bucket_name, destination_file, source_file,
)
print(
source_file, "successfully uploaded as object",
destination_file, "to bucket", bucket_name,
)
if __name__ == "__main__":
try:
main()
except S3Error as exc:
print("error occurred.", exc)
运行此示例:
- 在
/tmp中创建一个名为test-file.txt的文件。 如果要使用不同路径或文件名,请修改source_file的值。 - 使用以下命令运行
file_uploader.py:
python file_uploader.py
如果服务器上不存在该存储桶,输出类似如下内容:
Created bucket python-test-bucket
/tmp/test-file.txt successfully uploaded as object my-test-file.txt to bucket python-test-bucket
- 使用
mc ls验证已上传文件:
mc ls play/python-test-bucket
[2023-11-03 22:18:54 UTC] 20KiB STANDARD my-test-file.txt
更多参考
进一步了解
参与贡献
许可证
此 SDK 按 Apache License, Version 2.0 分发,更多信息请参阅 LICENSE 和 NOTICE。
Portions of this page are adapted from the MinIO Object Storage Documentation, © 2020–Present MinIO, Inc., licensed under CC BY 4.0, converted and maintained by the Silo project. Attribution