分散式檔案系統fastdfs安裝以及python呼叫
fastfds的安裝和使用
一、所需依賴
- 作業系統:centos7.x(注意的是centos使用yum安裝相關依賴)
- fastdfs:V6.06.tar.gz
- libfastcommon:V1.0.43.tar.gz
- fastdfs-nginx-module:fastdfs-nginx-module-1.22.tar.gz
- nginx:nginx-1.18.0.tar.gz
切記:版本一定要對應好,不然會遇到很多難以名狀的問題。
二、fastdfs基礎知識
1、什麼是fastdfs
(1)fastdfs是一個輕量級的開源分散式檔案系統;
(2)fastdfs主要解決了大容量的檔案儲存和高併發訪問的問題,檔案存取時實現了負載均衡;
(3)fastdfs實現了軟體方式的RAID,可以使用廉價的IDE硬碟進行儲存支援儲存伺服器線上擴容支援相同內容的檔案只儲存一份,節約磁碟空間;
(4)fastdfs只能通過Client API訪問,不支援POSIX訪問方式;
(5)fastdfs特別適合大中型網站使用,用來儲存資原始檔(如:圖片、文件、音訊、視訊等等)。
2、fastdfs框架圖
其中:
*Tracker Server:*跟蹤伺服器,主要做排程工作,在訪問上起負載均衡的作用。記錄storage server的狀態,是連線Client和Storage server的樞紐。
*Storage Server:*儲存伺服器,檔案和meta data都儲存到儲存伺服器上。
*group:*
*meta data:*檔案相關屬性,鍵值對(Key Value Pair)方式,如:width=1024,heigth=768。
3、上傳檔案流程
(1)client詢問tracker上傳到的storage,不需要附加引數;
(2)tracker返回一臺可用的storage;
(3)client直接和storage通訊完成檔案上傳。
4、下載檔案流程
(1)client詢問tracker下載檔案的storage,引數為檔案標識(組名和檔名);
(2)tracker返回一臺可用的storage;
(3)client直接和storage通訊完成檔案下載。
5、同步機制
(1)同一組內的storage server之間是對等的,檔案上傳、刪除等操作可以在任意一臺storage server上進行;
(2)檔案同步只在同組內的storage server之間進行,採用push方式,即源伺服器同步給目標伺服器;
(3)源頭資料才需要同步,備份資料不需要再次同步,否則就構成環路了;
(4)上述第二條規則有個例外,就是新增加一臺storage server時,由已有的一臺storage server將已有的所有資料(包括源頭資料和備份資料)同步給該新增伺服器。
6、tracker server目錄結構
--data
storage_groups.dat:儲存分組資訊
storage_servers.dat:儲存伺服器列表
--logs
trackerd.log:tracker server日誌檔案
7、storage_server目錄結構
--data
fdfs_storaged.pid:當前storage server的pid
storage_stat.dat:當前storage server統計資訊
--sync:存放資料同步相關檔案
binlog_index.dat:當前的binlog檔案索引號
binlog.###:存放更新操作記錄(日誌)
\({ip_addr}_\){port}.mark:存放同步的完成情況
--logs
storaged.log:storage server日誌檔案
以上理論參考:
ChinaUnix論壇:http://bbs.chinaunix.net/forum-240-1.html
github下載地址:https://github.com/happyfish100
指令碼之家:https://www.jb51.net/article/197165.html
三、安裝
碰到許可權問題,在指令前面加sudo。可以找一個地方建立一個資料夾,專門存放上述的壓縮包,然後操作時在同級目錄下解壓即可。
總體框架:
- 一臺伺服器作為tracker,同時該伺服器下有一個storage,該storage為group1
- 還有一臺伺服器只作為storage,且有4個組,分別是group1-group4,每個組下面都有一個storage,其中group1的storage和tracker所在的伺服器的storage同屬於一個組。
1、安裝libfastcommon
tar -zxvf V1.0.43.tar.gz
cd libfastcommon-1.0.43
./make.sh
sudo ./make.sh install
2、安裝fastfds
tar -zxvf V6.06.tar.gz
cd fastdfs-6.06
./make.sh
sudo ./make.sh install
對於包含tracker的伺服器:注意這裡可能存在許可權問題,可以先切換到root使用者。
2.1、配置tracker
cd /etc/fdfs/
cp tracker.conf.sample tracker.conf
vim tracker.conf
修改:base_path=/data02/file_server/tracker(必須手動先建立tracker目錄)
啟動tracker:/etc/init.d/fdfs_trackerd start
==========================================================
其它指令:
停止tracker:/etc/init.d/fdfs_trackerd stop
重啟tracker:/etc/init.d/fdfs_trackerd restart
檢視tracker程序:ps aux | grep tracker
2.2、配置storage
cd /etc/fdfs/
cp storage.conf.sample storage.conf
修改:(storage目錄需要手動建立)
base_path=/data02/file_server/storage
store_path0=/data02/file_server/storage
tracker_server=192.168.0.101:22122(這裡192.168.0.101是本機ip,22122和tracker.conf裡面的port保持一致)
啟動:/etc/init.d/fdfs_storaged start
==========================================================
其它指令:
停止storage:/etc/init.d/fdfs_storaged stop
重啟storage:/etc/init.d/fdfs_storaged restart
檢視storage程序:ps aux | grep storage
檢視fastfds相應的配置資訊:
fdfs_monitor /etc/fdfs/storage.conf
2.3、測試上傳
選擇192.168.0.101 tracker機器當做fdfs_client。
cd /etc/fdfs/
cp client.conf.sample client.conf
vim client.conf
修改:
base_path = /data02/file_server
tracker_server=192.168.0.101:22122
測試上傳檔案:
fdfs_upload_file /etc/fdfs/client.conf net.png
3、安裝nginx以及fastdfs-nginx-module
3.1、未安裝nginx
假設之前我們並沒有安裝過nginx,我們可以這麼操作:
tar -zxvf nginx-1.18.0.tar.gz
tar -zxvf fastdfs-nginx-module-1.22.tar.gz
cd nginx-1.18.0
./configure --add-module=../fastdfs-nginx-module-1.22/src/
說明:以上這句指令是將fastdfs-nginx-module模組加入的nginx配置中。如果提示缺少相關檔案,可執行以下指令:
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel。--add-module的值是fastdfs-nginx-module的路徑。
sudo make
sudo make install
接下來,我們拷貝相關檔案到指定目錄:
cd fastdfs-nginx-module-1.22/src
cp mod_fastdfs.conf /etc/fdfs/
cd /etc/fdfs/
vim mod_fastdfs.conf
修改:
tracker_server=192.168.0.101:22122(對應tracker.conf的ip地址以及port)
store_path0=/data02/file_server/storage(對應storage.conf裡面的store_path0)
storage_server_port=23000
group_name=group1
group_count=1
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data02/file_server/storage
cd fastdfs-6.06/conf/
cp http.conf /etc/fdfs/
cp mime.types /etc/fdfs/
cd /etc/fdfs/
vim http.conf
修改:http.anti_steal.token_check_fail=/data02/file_server/fastdfs-6.06/conf/
接下來我們要修改nginx的相關配置:預設nginx配置在/use/local/nginx/conf/nginx.conf
vim /use/local/nginx/conf/nginx.conf
修改:
user root;
listen 8888;(這裡的和storage.conf裡面的http.server_port = 8888是一致的)
server_name 192.168.0.101;
並在該塊中新增:
location ~/group1/ {
ngx_fastdfs_module;
}
儲存後,接下來我們就可以測試nginx了。
檢查配置檔案是否可用:sudo /usr/local/nginx/sbin/nginx -t
啟動nginx:sudo /usr/local/nginx/sbin/nginx
============================================
其它指令:
sudo /usr/local/nginx/sbin/nginx -s reload # 重啟nginx
sudo /usr/local/nginx/sbin/nginx -s stop # 立刻停止nginx
sudo /usr/local/nginx/sbin/nginx -s quit # nginx執行完後停止
補充:如果存在多個nginx的環境,我們可以指定nginx以及其配置檔案的方式來啟動,比如:
/data02/file_server/nginx-1.18.0/objs/nginx -c /data02/file_server/nginx-1.18.0/conf/nginx.conf
當然你要確保nginx.conf也是修改過的。
3.2、已安裝過nginx
如果我們已經安裝過nginx,則進行如下操作:
ps aux | grep nginx(檢視nginx的程序,並找到nginx的位置以及其配置檔案,比如現在找到了nginx以及nginx.conf)
nginx -V(檢視之前已經安裝的nginx的配置)
下載一個和該nginx相同版本的壓縮包並解壓,比如:nginx-1.18.0
cd nginx-1.18.0
./configure 剛才用-V得到的引數 --add-module=../fastdfs-nginx-module-1.22/src/
sudo make(這步之後不要用sudo make install指令)
然後會在objs/下生成編譯好的nginx檔案。
接下來先停止之前執行的nginx:nginx -s stop
備份一下原始的nginx:cp nginx nginx.bak
然後將新編譯好的nginx複製進去:cp nginx-1.18.0/objs/nginx /usr/local/nginx/sbin/
接著修改nginx配置檔案:vim /use/local/nginx/conf/nginx.conf
增加:
server {
listen 8888;
server_name 192.168.0.101;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
# 攔截包含 /group1/M00 請求,使用 fastdfs 這個 Nginx 模組進行轉發
location ~/group1/ {
ngx_fastdfs_module;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
最後啟動nginx:sudo /usr/local/nginx/sbin/nginx
3.3、補充
如果不想替換原本的nginx,那麼我們可以修改自己下載的nginx檔案,在nginx-1.18.0/conf/nginx.conf
裡面進行相應的修改,然後這樣啟動:sudo nginx-1.18.0/objs/nginx -c nginx-1.18.0/conf/nginx.conf
。為了不和已有的nginx衝突,可能在./configure新增配置的時候指定相關檔案的路徑,比如:
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/data02/fastdfs/fastdfs-nginx-module/src
如果在./configure時出現缺少相關依賴,可對照以下進行安裝:
錯誤./configure: error: the HTTP rewrite module requires the PCRE library.
解決:安裝pcre-devel:yum -y install pcre-devel
錯誤:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
–without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
–with-http_ssl_module –with-openssl= options.
解決:yum -y install openssl openssl-devel
錯誤:./configure: error: the HTTP XSLT module requires the libxml2/libxslt 缺少libxml2
解決:yum -y install libxml2 libxml2-devel && yum -y install libxslt-devel
錯誤資訊:./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.
解決方法:http_image_filter_module是nginx提供的整合圖片處理模組,需要gd-devel的支援 yum -y install gd-devel
錯誤資訊:./configure: error: perl module ExtUtils::Embed is required 缺少ExtUtils
解決方法:yum -y install perl-devel perl-ExtUtils-Embed
錯誤資訊:./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library. 缺少GeoIP
解決方法:yum -y install GeoIP GeoIP-devel GeoIP-data
錯誤資訊:./configure: error: the Google perftools module requires the Google perftools
library. You can either do not enable the module or install the library.
解決方法:yum -y install gperftools
最後測試上傳檔案後在:http:192.168.0.101:8888/group1/M00/00/00/wKgACWHCyqeAR1X_AAAFTB0Vk_Y126.jpg可以訪問到了。
4、在另一臺伺服器上搭建storage
該臺伺服器對應Ip:192.168.0.9。首先要明確的是除了安裝tracker,storage,nginx,fastdfs-nginx-module都是要重新走一遍的,只是配置的時候有所不同。
4.1、相關依賴
將之前的libfastcommon、fastdfs、nginx、fastdfs-nginx-module拷貝到該伺服器上。
4.2、配置storage
首先安裝好libfastcommon以及fastdfs,參考之前的步驟就行。然後開始配置storage:
cd /etc/fdfs/
cp storage.conf.sample storage.conf
vim storage.conf
修改:(storage目錄需要手動建立)
group=group1
port=23000
base_path=/data/data02/file_server/opt/storage
store_path0=/data/data03/storage
tracker_server=192.168.0.101:22122(這裡指定tracker)
http.server_port=8688
# 接下來我們同樣增加加個storage配置檔案
cp storage.conf.sample storage2.conf
vim storage2.conf
修改:(storage目錄需要手動建立)
group=group2
port=23002
base_path=/data/data05/storage
store_path0=/data/data05/storage
tracker_server=192.168.0.101:22122(這裡指定tracker)
http.server_port=8688
cp storage.conf.sample storage3.conf
vim storage3.conf
修改:(storage目錄需要手動建立)
group=group3
port=23003
base_path=/data/data06/storage
store_path0=/data/data06/storage
tracker_server=192.168.0.101:22122(這裡指定tracker)
http.server_port=8688
cp storage.conf.sample storage4.conf
vim storage4.conf
修改:(storage目錄需要手動建立)
group=group4
port=23004
base_path=/data/data09/storage
store_path0=/data/data09/storage
tracker_server=192.168.0.101:22122(這裡指定tracker)
http.server_port=8688
啟動時我們要分別啟動每一個storage:(配置完後暫時先不啟動,等後續其它的一起配置完畢)
fdfs_storaged /etc/fdfs/storage.conf start
fdfs_storaged /etc/fdfs/storage2.conf start
fdfs_storaged /etc/fdfs/storage3.conf start
fdfs_storaged /etc/fdfs/storage4.conf start
==========================================================
其它指令:
停止storage:fdfs_storaged /etc/fdfs/storage.conf stop
重啟storage:fdfs_storaged /etc/fdfs/storage.conf restart
檢視storage程序:ps aux | grep storage
當我們看到有4個storage程序的時候證明配置成功了。
4.3、配置nginx
./configure --add-module=../fastdfs-nginx-module-1.22/src/
sudo make (這裡可繼續選擇安裝,我就不安裝了)
不安裝時使用該nginx-1.18.0下的配置。
cd fastdfs-nginx-module-1.22/src
cp mod_fastdfs.conf /etc/fdfs/
cd /etc/fdfs/
vim mod_fastdfs.conf
修改:
tracker_server=192.168.0.101:22122
storage_server_port=23000
group_name=group1/group2/group3/group4
store_path0=/data/data02/file_server/opt/storage
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/data/data03/storage
[group2]
group_name=group2
storage_server_port=23002
store_path_count=1
store_path0=/data/data05/storage
[group3]
group_name=group3
storage_server_port=23003
store_path_count=1
store_path0=/data/data06/storage
[group4]
group_name=group4
storage_server_port=23004
store_path_count=1
store_path0=/data/data09/storage
接下來:
cd fastdfs-6.06/conf/
cp http.conf /etc/fdfs/
cp mime.types /etc/fdfs/
cd /etc/fdfs/
vim http.conf
修改:http.anti_steal.token_check_fail=/data/data02/file_server/soft/fastdfs-6.06/conf/
然後修改nginx配置:
vim nginx-1.18.0/conf/nginx.conf
修改:
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8688;
server_name 192.168.0.9;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location ~/group2/ {
ngx_fastdfs_module;
}
location ~/group3/ {
ngx_fastdfs_module;
}
location ~/group4/ {
ngx_fastdfs_module;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
5、啟動所有服務
(1)啟動t192.168.0.101伺服器:tracker以及storage
sudo /etc/init.d/fdfs_trackerd start
sudo /etc/init.d/fdfs_storaged start
(2)啟動192.168.0.9伺服器:storage
fdfs_storaged /etc/fdfs/storage.conf start
fdfs_storaged /etc/fdfs/storage2.conf start
fdfs_storaged /etc/fdfs/storage3.conf start
fdfs_storaged /etc/fdfs/storage4.conf start
然後使用fdfs_monitor /etc/fdfs/storage.conf
檢視,看到有如下結果表示成功:
tracker server is 192.168.0.101:22122
group count: 4
Group 1:
group name = group1
Storage 1:
id = 192.168.0.101
ip_addr = 192.168.0.101 ACTIVE
Storage 2:
id = 192.168.0.9
ip_addr = 192.168.0.9 ACTIVE
Group 2:
group name = group2
Storage 1:
id = 192.168.0.9
ip_addr = 192.168.0.9 ACTIVE
Group 3:
group name = group3
Storage 1:
id = 192.168.0.9
ip_addr = 192.168.0.9 ACTIVE
Group 4:
group name = group4
Storage 1:
id = 192.168.0.9
ip_addr = 192.168.0.9 ACTIVE
(3)啟動192.168.0.9伺服器:nginx
sudo /data/data02/file_server/soft/nginx-1.18.0/objs/nginx -c /data/data02/file_server/soft/nginx-1.18.0/conf/nginx.conf
(4)啟動192.168.0.101伺服器:nginx
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
用ps aux | grep nginx可檢視nginx程序,需要注意的是有兩個程序:
root 18429 0.0 0.0 28144 680 ? Ss 12月22 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
rhino 18430 0.0 0.0 28924 2392 ? S 12月22 0:00 nginx: worker proces
如果只有一個master程序,則說明是有問題的,可以去日誌檢視原因:/usr/local/nginx/logs/error.log
四、python操作fastfds進行上傳和下載
首先需要安裝包:pip install py3Fdfs
然後找一個位置,新建配置檔案:fastfdfs_client.conf,裡面配置:
connect_timeout=30
network_timeout=60
tracker_server = 192.168.0.101:22122
# tracker伺服器的埠
http.tracker_server_port = 8888
然後在同級目錄下新建一個python檔案:test_fastdfs.py
# 在522伺服器上可操作:
from fdfs_client.client import get_tracker_conf, Fdfs_client
import base64
from PIL import Image
from io import BytesIO
def main():
tracker_conf = get_tracker_conf('fastfdfs_client.conf')
client = Fdfs_client(tracker_conf)
# 檔案上傳,結果返回:{'Group name': b'group1', 'Remote file_id': b'group1/M00/00/00/wKgf3F5MAe2AV_23AAAADL_GVeU370.txt', 'Status': 'Upload successed.', 'Local file name': 'test.txt', 'Uploaded size': '12B', 'Storage IP': b'192.168.31.220'}
# 上傳後需要記錄該檔案在哪,以Remote file_id標識
# upload_by_filename函式根據檔名上傳
print(dir(client))
result = client.upload_by_filename('2579937-d92ba54f63a2d63e.png')
print(result)
# upload_by_buffer將二進位制檔案上傳,並新增字尾進行儲存
s = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAACK1BMVEVHcEwYd/8Wd/8Wd/8Vd/8XeP8Af/8Wdv8Wd/8Wdv8Wd//e6/8bev+NvP+10/8vhf9io/8aef/d6//z+P8whv/v9f85i/9uqv+51f9Ekv9Tmv+61v/O4v9+s/9CkP82iv8kf/9Klf8zh/9xrP8de/+Lu//t9P/I3//Z6P/y9//R5P/2+f+exv/u9f8rg//e7P9sqf+31f9Hk/+MvP95sP8xhv86jP8shP/b6f/c6v+fx/8yh//v9v+UwP9Fkv8gff/q8//k7/81if9Ol/8ifv9QmP+Sv//0+P8ffP/D3P84iv+GuP8YeP8cev/W5/////84i/9Rmf/l7/+Ft/+qzf8ee/+Vwf/a6v/Q4//S5f/P4/9IlP/f7P+axP+uz//X5/+tz/+Pvf82if+Wwv/T5f+Juv9WnP/G3f9Smv+gx//g7P8pgv/U5v9pp//N4f8/j/8thP9mpv+11P9coP8qgv+AtP/r8/8efP9npv8qg/9jpP8uhf8ogf8gfP8Zef9rqf/8/f8bef9Xnf9bn/9wq/9Aj/9mpf9kpP/5+/8ngP+cxf+pzf94sP/1+f9+tP+ozP9HlP97sv/M4f/n8f/w9v/Y6P9zrf+rzv9doP/W5v/A2v++2f83iv/l8P/o8v/o8f+71/9ho/+pzP/U5f/+/v/q8v+Rv/80iP9Mlv9Ckf+Xwv+/2f/j7//4+/88jf+Atf+Huf93r//7/P+y0v+51v9anv9PmP9qqP9lpf9BPI6rAAAACnRSTlMAK9S8j5MGf/L8h4HvUAAAAsZJREFUWMOl1vdbUlEYwPFX07KDQAyhSLFkKBC4KkPBlWluc+fOvdt72N577733rj+vc+6FGHLPvXDuD9wHHj7f+57DwwUAfMQlxCeh2I5FmC9MXIwQi1+AmDwk0t6haxL1cbT5dZM3mkQ8JNB8VnF7R90Jqod4in9i0m5eKV9+jOYhiXJ9i3Ua+3qqB9r82g7s088n07xgQKd+PNrzFHt7f24yxQsFdLeKa23PbJ3p9ryWlHmFIC8QaCu7Rvbv+qXhvNWr3r3EhYyiyF5ogtOaBrJ+NfZrMt++yq1R74zsIwbIxGeIt3N+rdz7wrFEwEcKFGTp8SP27m7O51sy1II+QqBg3Q6NHpkr67XZmeIeIvhCtEFDZkDa7FZRPy/gwh4FCn1yER8eUF6+yJ29Vbv4gv0N3YcFlLJe7pyj2cS/oO2x0T2EeTsq+ez3uysREpk/LKCUFXa9H3MaB79/IU9rrPuONIh5CPHNz4s+Vc+UGk229eSFA3v2HhLzwYELvW0frIrZ8b/+wnFHhagPDphRydjwgGvWNMkXrnrmxH1QoA6hbsfoyNDUxK8/TqNJ89pzUoIPBJKX5iBksBp+/kYqxXSpccadI8VDwKeuQKTw9YdKUb0xzWlEkjwXqMhX8X7ZBDI4v2FfXqa4Is1zgYMyBe9T0vAMjlTst0i8vm8J5Wc1Uz5PVqH/KN0Dv/4umULl82SGO9I9+PZPKbvveUDum+ZT+hJDq3QP/v2/2/jI23nTWdV4tHkwiuuTgInfv3t4Feica78KReXJEg5z+9f/kBQEfz/onwLZv75xZVhBkucCRrL/7bdRaEGa5wIucgcdkZM7Qm20PvBlGrLgh23maH0gMNAS/fpDAu7tMfn/AXPW1pg8UP//xBiIygOrB1YPrB5YPbB6YPXA6oHVA6sHVg+sHlg9sHpg9cDqgdUDqwdWD6z+H8aOr+GHK13YAAAAAElFTkSuQmCC'
data = s.split(',')[1]
image_data = base64.b64decode(data)
result = client.upload_by_buffer(image_data, 'jpg')
print(result)
# 檔案下載,結果返回:{'Remote file_id': b'group1/M00/00/00/wKgf3F5MAe2AV_23AAAADL_GVeU370.txt', 'Content': 't.txt', 'Download size': '12B', 'Storage IP': b'192.168.31.220'}
# 第一個引數是儲存的檔名,第二個引數是之前上傳得到的標識
result = client.download_to_file('t.png', b'group1/M00/00/00/wKgAZWHAQjiAAm4PAAFEJ0JhBgk569.png')
print(result)
#檔案刪除,結果返回:('Delete file successed.', b'group1/M00/00/00/wKgf3F5MAe2AV_23AAAADL_GVeU370.txt', b'192.168.31.220')
result = client.delete_file(b'group1/M00/00/00/wKgf3F5MAe2AV_23AAAADL_GVeU370.txt')
if __name__ == '__main__':
main()
具體函式可參考:https://pypi.org/project/py3Fdfs/2.1.0/
參考
https://www.cnblogs.com/zhangmingda/p/12622590.html