Python+FastDFS(分布式文件服务系统)(带安装包)

Python用FastDFS 包,使用分布式文件服务系统

*****适用场景 *
当你需要用到文件服务器时,并且你的开发语言是python2。
首先,你得有一个fastfds文件服务器,然后使用下面的demo就可以啦:

# -*- coding: utf-8 -*-
"""
Created on 2018/6/25

@author: cjh
连接文件服务器
"""
import time
import pandas as pd
from fdfs_client.client import *
from fdfs_client.exceptions import *
client = Fdfs_client('client.conf')		# 这个配置文件在下面


def upload_by_buffer(filebuffer, file_ext_name=None, meta_dict=None):
    """
    将文件流上传到存储服务器
    :param filebuffer: 二进制文件流,缓冲区
    :param file_ext_name: 字符串,文件扩展名
    :param meta_dict: 字典例如:{
    'ext_name':'jpg',
    'file_size':'10240B',
    'width':' 160px ',
    'hight' :'80px'
    }
    :return:
    {
    ' Group name':组名,
    ' Remote file_id ':文件ID,
    'Status':状态,
    ' Uploaded size ':文件大小,
    }

    """
    starttime = time.time()
    ret = {}
    try:
        ret = client.upload_by_buffer(filebuffer, file_ext_name, meta_dict)
    except Exception as e:
        ret['_message'] = e.message
    endtime = time.time()
    ret['_timed'] = endtime - starttime
    return ret


def upload_by_filename(filename, meta_dict=None):
    """
    将文件上传到存储服务器
    :param filename: 文件,缓冲区
    :param meta_dict: 字典例如:{
    'ext_name':'jpg',
    'file_size':'10240B',
    'width':' 160px ',
    'hight' :'80px'
    }
    :return:
    {
    ' group_name':组名,
    ' remote_file_id ':文件ID,
    ' status':状态,
    ' storage_ip ':,
    ' local_file_name ':,
    ' upload_size ':文件大小,
    }

    """
    starttime = time.time()
    ret = {}
    try:
        ret = client.upload_by_filename(filename, meta_dict)
    except Exception as e:
        ret['_message'] = e.message
    endtime = time.time()
    ret['_timed'] = endtime - starttime
    return ret


def delete_file(remote_file_id):
    """
    删除文件 file_id
    :param remote_file_id:
    :return:
    """
    starttime = time.time()
    ret = {}
    try:
        ret = client.delete_file(remote_file_id)
    except Exception as e:
        ret['_message'] = e.message
    endtime = time.time()
    ret['_timed'] = endtime - starttime
    return ret


def download_to_file(local_filename, remote_file_id, offset=0, down_bytes=0):
    """
    服务中使用,
    从存储服务器下载文件并存成文件
    :param local_filename: 文件的本地名称
    :param remote_file_id: 文件id
    分块下载
    :param offset: 偏移量
    :param down_bytes: 下载字节
    :return:
    ['Content', 'Remote file_id', 'Storage IP', '_timed', 'Download size']
    """
    starttime = time.time()
    ret = {}
    try:
        ret = client.download_to_file(local_filename, remote_file_id, offset, down_bytes)
    except Exception as e:
        ret['_message'] = e.message
    endtime = time.time()
    ret['_timed'] = endtime - starttime
    return ret


def download_to_buffer(remote_file_id, offset=0, down_bytes=0):
    """
    服务中使用,
    从存储服务器下载文件并存成文件流
    :param remote_file_id: 文件id
    分块下载
    :param offset: 偏移量
    :param down_bytes: 下载字节
    :return:
    ['Content', 'Remote file_id', 'Storage IP', '_timed', 'Download size']
    """
    starttime = time.time()
    ret = {}
    try:
        ret = client.download_to_buffer(remote_file_id, offset, down_bytes)
    except Exception as e:
        ret['_message'] = e.message
    endtime = time.time()
    ret['_timed'] = endtime - starttime
    return ret


if __name__ == '__main__':
	# 上传成功后,将返回值中的Remote file_id与文件服务器地址拼接,即可在线访问了。
	# 22122是默认的上传端口,要从文件服务器拿东西,还会有一个获取数据的端口。
	# 例如:IP:预览PORT+/+返回值中的Remote file_id
    
    # 文件上传
    print upload_by_filename(u'视频.mp4')
	
	# 文件上传
    # print upload_by_filename('dist.rar')
	
	# 文件流上传得需要前端获取文件流的demo

    # TODO 后台文件流获取
    # a = download_to_buffer('group1/M00/00/00/rB9ArVs0NPWAfIwXAA9iGBNe4f8979.txt')
    # print a.get('Content')

    ret1 = {
        'Status': 'Upload successed.',
        'Storage IP': '***.95.***.42',
        'Remote file_id': 'group1\\M00/00/00/rB9ArVswu-mAOSgPAAAEYMjFRXI0591.py',
        '_timed': 0.023000001907348633,
        'Group name': 'group1',
        'Local file name': 'models.py',
        'Uploaded size': '1.00KB'
    }
    ret2 = {
        'Status': 'Upload successed.',
        'Storage IP': '***.95.***.42',
        'Remote file_id': 'group1\\M00/00/00/rB9ArVswvGyACRSdADpFDjQJbVE984.rar',
        '_timed': 0.35699987411499023,
        'Group name': 'group1',
        'Local file name': 'dist.rar',
        'Uploaded size': '3.00MB'
    }

    ret3 = {
        'Status': 'Upload successed.',
        'Storage IP': '***.95.***.42',
        'Remote file_id': 'group1\\M00/00/00/rB9ArVs0MU2AXLOnAA_D7DSRdv8052.txt',
        '_timed': 0.12999987602233887,
        'Group name': 'group1',
        'Local file name': '20180401.txt',
        'Uploaded size': '1008.00KB'
    }

配置文件:client.conf

# connect timeout in seconds
# default value is 30s
connect_timeout=30

# network timeout in seconds
# default value is 30s
network_timeout=60

# the base path to store log files
base_path=/home/fastdfs/tracker

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=***.95.***.42:22122	# 文件服务器 地址,22122是默认端口

#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info

# if use connection pool
# default value is false
# since V4.05
use_connection_pool = false

# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600

# if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false

# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false

# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf


#HTTP settings
http.tracker_server_port=80

#use "#include" directive to include HTTP other settiongs
##include http.conf

PS: fdfs_client-py-1.2.6,fdfs_client-py-1.2.7,vcforpython27.msi 包自己安哇,cd 到文件根目录下,python setup.py,fdfs_client直接安127新版本即可。

下载地址:
链接永久有效:https://pan.baidu.com/s/1yC90ToYJMtBPAH3iNmmrJQ 提取码:gosg

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈建华呦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值