下面是关于Python实现上传Minio和阿里OSS文件的攻略,包含了两个实例说明。
Minio
安装Minio
Minio是一款轻量级的对象存储解决方案,易于使用和部署。首先需要在本地或服务器上安装Minio,安装方式可参考官方文档 https://docs.min.io/cn/minio-quickstart-guide.html。
Python SDK
进入官网https://docs.min.io/docs/python-client-api-reference.html,下载minio的python-sdk。
连接Minio
连接方法如下:
from minio import Minio
from minio.error import ResponseError
try:
minio_client = Minio(
"localhost:9000",
access_key="minioadmin",
secret_key="minioadmin",
secure=False)
print("连接成功")
except ResponseError as e:
print(e)
上传文件
上传方法如下:
from minio import Minio
from minio.error import ResponseError
try:
minio_client = Minio(
"localhost:9000",
access_key="minioadmin",
secret_key="minioadmin",
secure=False)
result = minio_client.fput_object(
"testbucket",
"testfile.txt",
"/path/to/file.txt")
print("上传成功")
except ResponseError as e:
print(e)
此示例中,将本地文件/path/to/file.txt
上传到名为testbucket
的Minio桶中,文件在Minio中的名称为testfile.txt
。
阿里OSS
安装OSS SDK
进入官网https://help.aliyun.com/document_detail/32026.html,下载aliyun-oss-python-sdk。
连接OSS
连接方法如下:
import oss2
access_key_id = '<access_key_id>'
access_key_secret = '<access_key_secret>'
bucket_name = '<bucket_name>'
endpoint = '<endpoint>'
auth = oss2.Auth(access_key_id, access_key_secret)
bucket = oss2.Bucket(auth, endpoint, bucket_name)
print("连接成功")
上传文件
上传方法如下:
import oss2
access_key_id = '<access_key_id>'
access_key_secret = '<access_key_secret>'
bucket_name = '<bucket_name>'
endpoint = '<endpoint>'
auth = oss2.Auth(access_key_id, access_key_secret)
bucket = oss2.Bucket(auth, endpoint, bucket_name)
result = bucket.put_object('testfile.txt', 'content of file')
print("上传成功")
此示例中,将字符串'content of file'
上传到名为bucket_name
的OSS桶中,文件在OSS中的名称为testfile.txt
。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python实现上传Minio和阿里Oss文件 - Python技术站