zoukankan      html  css  js  c++  java
  • 获取fastdfs所有文件

    工作中,mysql中存储的图片链接信息和FastDFS实际存储的图片数量不一致,此时应该与mysql中有存储记录的图片保持一致,我们要在FastDFS服务器中删除哪些无用的图片。于是乎自己写了一个脚本。

    1. 读取mysql中的所有FastDFS存储图片信息---->存储到redis,保存为set

    2. 读取FastDFS中的图片信息---->存储到redis,保存为set

    3. 两个set,以FastDFS的set和mysql的set做差集运算得到多出来的图片set

    4. 在FastDFS服务器上删除上一步中包含的图片

    fastdfs提供的脚本不能列出所有的文件,所以我自己写了一个python脚本遍历出所有文件
    pythonToRedis.py
    把所有文件写入到了本地文件,当然也可以存入关系型数据库或者Redis中(用set去重,做一些交差补运算)

    import re
    import sys
    import os
    
    def allFiles():
        path = '/data/fastdfs/storage/data' # basepath配置路径
        rounds = 1
        fdfspath = 'group1/M00'
    
        with open('/home/timing/shelles/data.txt','w') as file_url:
            
            for dirpath, dirnames, filenames in os.walk(path):
                if rounds == 1:
                    rounds+=1
                elif (dirpath == path + '/sync'):
                    continue
                else:
                    for file in filenames:
                        try:
                            paths = re.search(r'/data/fastdfs/storage/data(.*)',dirpath).group(1)
                            fullpath = os.path.join(fdfspath + paths, file)
                            print(fullpath)
                            file_url.write(fullpath + '
    ')
                        except:
                            pass
    
                    rounds+=1
            file_url.close()
     
    def toRedis():
        with open('/home/redis/tuna/shelles/data.txt', 'r') as logfile:
            for line in logfile:
                print(line)
                redis_client.sadd('dfs_picture',line.replace('
    ', ''))
            logfile.close()
    
    if __name__ == '__main__':
        if(sys.argv[1] == 'allfiles'):
            allFiles()
    elif(sys.argv[1] == 'toredis'):
    toRedis() else: print("USAGE:allfiles|toredis")
  • 相关阅读:
    SoapUI 使用笔记
    git 使用笔记(二)
    git 使用笔记(一)
    jquery 拓展
    hdu 1024 Max Sum Plus Plus (DP)
    hdu 2602 Bone Collector (01背包)
    hdu 1688 Sightseeing (最短路径)
    hdu 3191 How Many Paths Are There (次短路径数)
    hdu 2722 Here We Go(relians) Again (最短路径)
    hdu 1596 find the safest road (最短路径)
  • 原文地址:https://www.cnblogs.com/xiaolinstudy/p/7777123.html
Copyright © 2011-2022 走看看