Python删除fastdfs图片

删除fastdfs图片及刷新七牛CDN

python环境 python2.7(linux本身所带环境)

使用前需要安装依赖库qiniu,fdfs-client-py
client_file值为fastdfs的client配置文件,里面traker_server为要删除的fastdfs集群的tracker地址

1
pip install qiniu fdfs-client-py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from fdfs_client.client import *
import qiniu
from qiniu import CdnManager
import urlparse


def search_source_file(file_id):
"""查看fastdfs中文件是否存在"""
pass


def del_source_file(file_id):
"""
从fastdfs删除文件
"""
file_id = file_id
client_file = '/home/work/fastdfs/conf/client.conf'
client = Fdfs_client(client_file)
try:
ret_delete = client.delete_file(file_id)

if ret_delete[0] == 'Delete file successed.':
print 'Delete file ', file_id, 'Successed.'
return 0
except DataError:
print file_id, 'not found.'
return 500


def refresh_qiniu_url(url):
"""
刷新对应url的七牛CDN
"""
access_key = 'your_access_key'
secret_key = 'your_secret_key'
auth = qiniu.Auth(access_key=access_key, secret_key=secret_key)
cdn_manager = CdnManager(auth)

urls = [url]

refresh_url_result = cdn_manager.refresh_urls(urls)

if refresh_url_result[0]['code'] == 200:
print 'Refresh ', url, 'Successed.'
return 0
return refresh_url_result





if __name__ == '__main__':
if len(sys.argv)<=1:
print 'Usage:'+'\n\tpython ' + sys.argv[0] + ' http://youdomin.domin/'+ "FileID"
print "eq:\tpython delete_img.py http://youdomin.domin/fastdfs3/M00/DA/D2/ChONol3d8o-AEuDaAABwz-gtyfs212.png"
else:
url = sys.argv[1]
img_domin = urlparse.urlparse(url).netloc
img_path = urlparse.urlparse(url).path

file_id = img_path[1:]

if del_source_file(file_id) != 0:
exit(500)
refresh_qiniu_url(url)