py操作七牛云存储、minio、fastdfs

一、py操作七牛云存储

1、先创建好存储桶

2、官文找到py sdk文档,代码如下

# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth, put_file, etag
import qiniu.config

#需要填写你的 Access Key 和 Secret Key
access_key = 's'
secret_key = 'xxx'

#构建鉴权对象
q = Auth(access_key, secret_key)

#要上传的空间
bucket_name = 'hhh'

#上传后保存的文件名
key = 'retu.py'

#生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)

#要上传文件的本地路径
localfile = './retu.py'

ret, info = put_file(token, key, localfile, version='v2')
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)

二、py操作 minio

py代码

#pip install minio

from minio import Minio

# 使用endpoint、access key和secret key来初始化minioClient对象。
minioClient = Minio('192.168.1.252:9000',
                    access_key='HJG',
                    secret_key='xxxx',
                    secure=False)
# 调用make_bucket来创建一个存储桶。
# minioClient.make_bucket("maylogs", location="us-east-1")
# test01 为桶名字
res = minioClient.fput_object('test01', 'lqz.jpg', './lqz.jpg')
print(res.object_name)
print('文件地址为【文件在浏览器打开会直接下载,放到index.html 中使用img引入查看】:\n', 'http://192.168.1.252:9000/test01/' + res.object_name) 

三、py操作 minio

1、py代码

# pip3 install py3Fdfs
from fdfs_client.client import get_tracker_conf, Fdfs_client

tracker_conf = get_tracker_conf('./client.conf')
client = Fdfs_client(tracker_conf)

#文件上传
result = client.upload_by_filename('./lqz.jpg')
print(result)
# {'Group name': b'group1', 'Remote file_id': b'group1/M00/00/00/rBMGZ.sqlite', 'Status': 'Upload successed.', 'Local file name': './db.sqlite3', 'Uploaded size': '128.00KB', 'Storage IP': b'101.133.225.166'}
# 访问地址即可下载:http://192.168.1.252:8888/group1/M00/00/00/CgAAzmSihyKAUybqAAH8LXKkrrY060.jpg


#文件下载
# result = client.download_to_file('./lqz.sqlite', b'group1/M00/00/00/rBMGZWCeGxaAFWqfAAIAABZebgw.sqlite')
# print(result)


# #文件删除
# result = client.delete_file(b'group1/M00/00/00/rBMGZWCeGhqAR_vRAAIAABZebgw.sqlite')
# print(result)
# ('Delete file successed.', b'group1/M00/00/00/rBMGZWCeGhqAR_vRAAIAABZebgw.sqlite', b'192.168.1.252')

# #列出所有的group信息
result = client.list_all_groups()
print(result)

2、client.conf

connect_timeout=30
network_timeout=60
tracker_server = ip:port
http.tracker_server_port = 8888

  

 

posted @ 2023-10-24 11:24  凡人半睁眼  阅读(97)  评论(0编辑  收藏  举报