天天看點

使用fastdfs+django+nginx存儲檔案

此次搭建的fastdfs主要是為了轉存由于時間過期會失效的URL中的檔案。在伺服器上搭建一個單機的fastdfs,然後通過django+nginx來将其包裝成服務。該服務接收URL,下載下傳URL中的檔案,存儲于fastdfs,傳回fastdfs的新URL給用戶端。用戶端能夠通路新的URL并下載下傳檔案。

1. 搭建

網上的搭建教程很多,在此不再多說,以下是搭建時參考過的。

使fastdfs給出的連結能夠在浏覽器通路,需要配置nginx

1. https://www.cnblogs.com/chiangchou/p/fastdfs.html#_label4_0 (查閱概念即可)

2. http://www.blogjava.net/Alpha/archive/2016/08/02/430008.html

3. https://blog.csdn.net/liu_zhaoming/article/details/72802936

需要注意的時,nginx中配置的監聽端口需要和client.conf中的http.tracker_server_port端口保持一緻。

2. django服務

fastdfs的存儲采用執行指令,從指令中正則得到URL的做法來實作django與fastdfs的交流,不需要下載下傳fastdfs的用戶端,非常地簡單粗暴!upload.py的内容如下:

import os
import time
import requests
import hashlib
import traceback
import sys
import re
import json
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse


@csrf_exempt
def start(request):
    date = time.strftime('%Y%m%d')
    UPLOAD_FILE_PATH = '/home/**/FDFS-upload/%s/' % date
    isExists = os.path.exists(UPLOAD_FILE_PATH)
    if not isExists:
        os.makedirs(UPLOAD_FILE_PATH)
    else:
        print('path isexist!')

    request_params = (request.body).decode('utf-8')
    request_params = json.loads(request_params)
    print(request_params)
    url = request_params['url']
    zip_file_tuple = download_log(url, UPLOAD_FILE_PATH)
    if zip_file_tuple:
        zip_file_name = zip_file_tuple[]
        md5sum = zip_file_tuple[]
    else:
        return JsonResponse({})
    file_name = os.path.join(UPLOAD_FILE_PATH, zip_file_name)
    print('file name:' + file_name)
    std = os.popen("fdfs_test /etc/fdfs/client.conf upload %s" %file_name).read()
    print('*********** fastdfs excute start ***********')
    print(std)
    print('*********** fastdfs excute end ***********')
    match = re.search('.*?example file url: (\S+)', std)
    if match:
        download_url = match.group()
        response = dict()
        response['url'] = download_url
        return JsonResponse(response)
    else:
        return JsonResponse({})


def download_log(uri, dest_dir):
    try:
        os.makedirs(dest_dir, exist_ok=True)
        zip_name = uri.rsplit('/', )[]
        zip_whole_name = os.path.join(dest_dir, zip_name)
        r = requests.get(uri)
        fd = open(zip_whole_name, 'wb')
        fd.write(r.content)
        fd.close()
        md5sum = hashlib.md5(r.content).hexdigest()
    except Exception as ex:
        traceback.print_exc(file=sys.stdout)
        return None
    if r.status_code == :
        return None
    return (zip_whole_name, md5sum)
           

3.使用

3.1 啟動

sudo /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

sudo /usr/bin/fdfs_storaged /etc/fdfs/storage.conf

ps -ef |grep fdfs

3.2 停止

千萬不要使用-9參數強殺,否則可能會導緻binlog資料丢失的問題。

直接kill即可讓server程序正常退出,可以使用killall指令,例如:

killall fdfs_trackerd

killall fdfs_storaged

也可以使用如下指令:

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop

3.3 重新開機

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

3.4 上傳檔案

3.5 删除檔案

4 題外話

我本意是想轉存檔案,傳回新的URL,似乎使用nginx的upload子產品也可以實作,但是我還未驗證,此處隻是一個記錄。

1. https://blog.csdn.net/blog_liuliang/article/details/78846579

繼續閱讀