示例#1
0
    def _save(self,name,content,html=None):
        # will be used by Storage.save()
        # name:file who will be saved's name

        client_obj=Fdfs_client(self.client_conf)
        print(type(content))

        if html==None:
            ret=client_obj.upload_by_buffer(content.read())

            if ret.get('Status')!='Upload successed.':
                raise Exception('upload failure')


            filename=ret.get('Remote file_id')

            return filename
        elif html==True:
            ret = client_obj.upload_by_filename(name)
            print(ret)

            if ret.get('Status') != 'Upload successed.':
                raise Exception('upload failure')

            filename = ret.get('Remote file_id')

            return filename
示例#2
0
文件: pipelines.py 项目: YCBDM/Scarpy
    def process_item(self, item, spider):
        # 启动Fastdfs客户端
        dfs_client = Fdfs_client('C:/Program Files/Python37/client.conf')

        # 连接kafka
        def reporthook(a, b, c):
            """
            显示下载进度
            :param a: 已经下载的数据块
            :param b: 数据块的大小
            :param c: 远程文件大小
            :return: None
            """
            print("\rdownloading: %5.1f%%" % (a * b * 100.0 / c), end="")

        # 将FastDFS的图片地址替换成原来网页中的地址
        def replace_url():
            pass

        for src in item['img_url']:
            work_path = self.path
            try:
                # 下载图片
                urllib.request.urlretrieve(src,
                                           "D:/FastDFS_pic/%s" % self.page,
                                           reporthook)

                # 将本地图片上传到FastDFS
                result = dfs_client.upload_by_filename("D:/FastDFS_pic/%s" %
                                                       self.page)

                # 获取FastDFS路径
                Fastdfs_url = result.get('Storage IP') + '\\' + result.get(
                    'Remote file_id')

                # 用FastDFS替换掉item中原来的img_url
                print('this is  原来的img_url:' + item['img_url'][self.page])
                item_Fastdfs = item
                (item_Fastdfs['img_url'][self.page]) = Fastdfs_url
                print('this is  替换后的Fastdfs_url:' +
                      item_Fastdfs['img_url'][self.page])
                self.page += 1
            except Exception as e:
                print(e)
        # 记得删除下载的图片
        # pass
        # 最后将item传给kafka
        producer = KafkaProducer(
            bootstrap_servers=
            "10.197.236.154:9092,10.197.236.169:9092,10.197.236.184:9092")
        msg = json.dumps(dict(item_Fastdfs))
        print(
            "this is item_Fastdfs!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
            + str(item_Fastdfs))
        producer.send('ScarpyHikcreateBigdata', msg, partition=0)
        print(
            "this is item_Fastdfs!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "
            + str(item_Fastdfs))
        producer.close()
        return item
示例#3
0
def upload_image():
    # 1. 导入FastDFS客户端扩展
    from fdfs_client.client import Fdfs_client
    # 2. 创建FastDFS客户端实例
    client = Fdfs_client('client.conf')
    # 3. 调用FastDFS客户端上传文件方法
    ret = client.upload_by_filename('test.png')
    print(ret['Remote file_id'].replace('\\', '/'))
示例#4
0
def upload_file(file_path):
    client_path = os.path.join(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
        'client.conf')
    client = Fdfs_client(get_tracker_conf(client_path))
    res = client.upload_by_filename(file_path)
    if res['Status'] == 'Upload successed.':
        return res['Remote file_id'].decode()
    else:
        raise Exception(res['Status'])
示例#5
0
def save_file_to_storage(file_dir):
    client = Fdfs_client('client.conf')
    # 3. 调用FastDFS客户端上传文件方法
    # client.upload_appender_by_filename()
    file_list = file_name(file_dir)
    try:
        for file in file_list:
            ret = client.upload_by_filename(file_dir + f'/{file}')
    except:
        print('error')
示例#6
0
    def upload_image(self):
        mysql = DataBaseHandle(host="localhosts", user="******", password="******", database="blog")

        client = Fdfs_client("./client.conf")
        for filename in os.listdir(self.filepath):
            image_url = client.upload_by_filename("/home/mrc/blog/image_storage/" + filename)

            query = 'insert into top_image(url) value(%s)'
            url = self.ip_port + image_url['Remote file_id']

            mysql.insertDB(query, url)
        mysql.closeDB()
示例#7
0
def upload_fdfs(path: str):
    """
    #上传FDFS,api
    :param path:文件的绝对路径
    :return: 上传后FDFS返回的信息
    """

    trackers = get_tracker_conf(TRCKER_CONF)
    client = Fdfs_client(trackers)
    ret = client.upload_by_filename(path)
    if ret.get('Status') == 'Upload successed.':

        return ret
    return
示例#8
0
文件: pipelines.py 项目: YCBDM/Scarpy
    def process_item(self, item, spider):
        # 启动Fastdfs客户端
        dfs_client = Fdfs_client('C:/Program Files/Python37/client.conf')
        # 下载并上传图片到FastDFS
        for src in item['image_urls']:

            FastDFS_urllist = []  # 用来装FastDFS地址
            try:
                # 下载图片
                urllib.request.urlretrieve(
                    src, "D:/FastDFS_pic/%s" %
                    self.page, self.reporthook)
                # 将本地图片上传到FastDFS
                result = dfs_client.upload_by_filename(
                    "D:/FastDFS_pic/%s" % self.page)
                # 获取FastDFS路径
                Fastdfs_url = result.get(
                    'Storage IP') + '\\' + result.get('Remote file_id')
                # 将每一次取到的FastDFS地址存入FastDFS_urllist
                FastDFS_urllist.append(Fastdfs_url)
                self.page += 1
            except Exception as e:
                print(e)
        logging.info("?????????????????????????????????????????????????????")
        logging.info(FastDFS_urllist)

        soup = BeautifulSoup(
            item['article_html'],
            'html.parser',
            from_encoding='utf-8')
        imgurlset = soup.find(id="js_content").find_all(name='img')
        for eachimgurl, eachfasturl in zip(imgurlset, FastDFS_urllist):
            eachimgurl['src'] = eachfasturl
        FastDFS_article = soup.prettify()  # str格式的html,整个替换后的文档
        logging.info(FastDFS_article)
        # 将item里面的文章替换掉
        FastDFS_item = item
        FastDFS_item['article_html'] = FastDFS_article

        # 记得删除下载的图片
        # os.remove("D:/FastDFS_pic/")
        # 最后将FastDFS_item传给kafka
        producer = Kafka_producer("10.197.236.154,10.197.236.169,10.197.236.184", 9092, "HikBigdata")
        params = json.dumps(dict(FastDFS_item))
        paramsitem = json.dumps(dict(item))
        print(params)
        producer.sendjsondata(params)
        producer.sendjsondata(paramsitem)
        producer.producer.close(timeout=10)
        return item
示例#9
0
def upload_fdfs():
    # 指定client.conf的位置
    client = Fdfs_client(FDFS_CLIENT_CONF)

    filename = os.path.join(BASE_DIR, 'test_image.png')
    result_1 = client.upload_by_filename(filename)
    # {'Group name': 'group1', 'Remote file_id': 'group1/M00/00/00/rBMABF7I3e2AKOgfAAD7LBrSuwQ072.png',
    # 'Status': 'Upload successed.', 'Local file name': '/my_django/awa.png', 'Uploaded size': '62.00KB',
    # 'Storage IP': '172.19.0.4'}
    url_path = '/' + result_1.get('Remote file_id')
    print(url_path)

    # 2 -->
    with open(filename, 'rb') as f:
        result_2 = client.upload_by_buffer(f.read())
    url_path2 = '/' + result_2.get('Remote file_id')
    print(url_path2)
示例#10
0
    def _save(self, name, content):
        """
        在FastDFS中保存文件
        :param name: 传入的文件名
        :param content: 文件内容
        :return: 保存到数据库中的FastDFS的文件名
        """
        client = Fdfs_client(self.client_conf)

        ret = client.upload_by_filename(content)
        if ret.get("Status") != "Upload successed.":
            logger.error("upload file failed!!!")
            raise Exception("upload file failed")

        logger.info(ret)
        file_name = ret.get("Remote file_id")

        return file_name
示例#11
0
class Fdfs():
    def __init__(self, client_file):
        self.client = Fdfs_client(client_file)

    def upload(self, upload_file):
        try:
            ret_upload = self.client.upload_by_filename(upload_file)
            file_id = ret_upload['Remote file_id'].replace('\\', '/')
            return file_id

        except Exception as e:
            return None

    def uploadbyBuffer(self, file, suffix):
        try:
            ret_upload = self.client.upload_by_buffer(file, suffix)
            file_id = ret_upload['Remote file_id'].replace('\\', '/')
            return file_id
        except Exception as e:
            print(e)
            return None

    def downloadbyBuffer(self, file_id):
        try:
            ret_download = self.client.download_to_buffer(file_id)
            ret_content = ret_download['Content']
            return ret_content
        except Exception as e:
            return None

    def download(self, download_file, file_id):
        try:
            ret_download = self.client.download_to_file(download_file, file_id)
            ret_content = ret_download['Content']
            return ret_content
        except Exception as e:
            return None

    def delete(self, file_id):
        try:
            ret_delete = self.client.delete_file(file_id)
            return ret_delete
        except Exception as e:
            return None
示例#12
0
    def post(self):
        file = self.request.files

        #用时间生成随机字符串作为文件名,以保证文件名的唯一
        filename = time.strftime('%Y%m%d%H%M%S') + '.jpg'
        file_img = file.get('file')
        if file_img:
            # 获取图片
            img = file_img[0]['body']
            #创建一个路径存储图片
            with open(
                    os.path.join(
                        os.path.dirname(
                            os.path.dirname(os.path.dirname(__file__))),
                        'bufferimage/' + filename), 'wb') as f:
                f.write(img)

            #配置文件路径
            client = Fdfs_client(
                os.path.join(
                    os.path.dirname(os.path.dirname(
                        os.path.dirname(__file__))), 'fastdfs/client.conf'))
            # 把图片上传到服务器
            ret = client.upload_by_filename(
                os.path.join(
                    os.path.dirname(os.path.dirname(
                        os.path.dirname(__file__))),
                    'bufferimage/' + filename))
            ip = ret['Storage IP']
            file_id = ret['Remote file_id']
            file_path = 'http://' + ip + '/' + file_id
            print(file_path)

            #保存图片路径
            mobile = self.get_secure_cookie('mobile').decode('utf8')
            cursor = self.db.cursor()
            sql = "update user set file_id='" + file_path + "' where mobile='" + mobile + "'"
            self.db.ping(reconnect=True)
            cursor.execute(sql)
            self.db.commit()
            self.db.close()
        self.write({'info': 0})
示例#13
0
def download_img(url):
    global client
    Default_Header['User-Agent'] = random.choice(UA_LIST)
    res = requests.get(url, stream=True, headers=Default_Header, timeout=6)
    # print(res.status_code)
    if not res.content or res.status_code != 200:
        print('downerror:', url)
        return None
    else:
        with open(img_name, 'wb') as file:
            file.write(res.content)
        try:
            result = client.upload_by_filename(img_name)
            file_url = result['Remote file_id']
        except:
            client = Fdfs_client('./client.conf')
            result = client.upload_by_filename(img_name)
            file_url = result['Remote file_id']
        # print('file_url:', file_url)
        return file_url
示例#14
0
def get_fdfs_url(file):
    """
    上传文件或图片到FastDFS
    :param file: 文件或图片对象,二进制数据或本地文件
    :return: 文件或图片在FastDFS中的url
    """

    # 创建FastDFS连接对象
    fdfs_client = Fdfs_client(settings.FASTDFS_CONF_PATH)

    """
    client.upload_by_filename(文件名),
    client.upload_by_buffer(文件bytes数据)
    """
    # 上传文件或图片到fastDFS
    if isinstance(file, InMemoryUploadedFile):
        result = fdfs_client.upload_by_buffer(file.read())
    else:
        result = fdfs_client.upload_by_filename(file)
    """
    result = {
    'Group name': 'group1',  # FastDFS服务端Storage组名
    'Remote file_id': 'group1/M00/00/00/wKgThF0LMsmATQGSAAExf6lt6Ck10.jpeg',  # 文件存储的位置(索引),可用于下载
    'Status': 'Upload successed.',  # 文件上传结果反馈
    'Local file name': '/home/python/Desktop/upload_Images/02.jpeg',  # 所上传文件的真实路径
    'Uploaded size': '76.00KB',  # 文件大小
    'Storage IP': '192.168.19.132'}  # FastDFS服务端Storage的IP
    """

    # 判断是否上传成功,result为一个字典
    if result['Status'] != 'Upload successed.':
        return Response(status=403)
    # 获取文件或图片上传后的路径
    file_url = result['Remote file_id']

    return file_url
示例#15
0
    def post(self):
        file = self.request.files

        file_img = file.get('file')

        #判断是否有图片
        if file_img:
            #生成随机字符串
            filename = str(time.strftime('%Y%m%d%H%M%S')) + '.jpg'
            img = file_img[0]['body']
            print(img)
            with open(
                    os.path.join(
                        os.path.dirname(
                            os.path.dirname(os.path.dirname(__file__))),
                        'bufferimage/' + filename), 'wb') as f:
                f.write(img)
            #配置文件路径
            client = Fdfs_client(
                os.path.join(
                    os.path.dirname(os.path.dirname(
                        os.path.dirname(__file__))), 'fastdfs/client.conf'))
            # 把图片上传到服务器
            ret = client.upload_by_filename(
                os.path.join(
                    os.path.dirname(os.path.dirname(
                        os.path.dirname(__file__))),
                    'bufferimage/' + filename))
            ip = ret['Storage IP']
            file_id = ret['Remote file_id']
            file_path = 'http://' + ip + '/' + file_id
            print(file_path)

            self.write({'code': 200, 'file_path': file_path})
        else:
            self.write({'code': 300})
示例#16
0
workon bj18_py3

安装一个包fdfs client-py-master
(bj18_py3) yyc@yyc-Lenovo-G50-45:~/bj18$ unzip fdfs_client-py-master.zip
解压
这里报错版本太低,直接下面命令

pip install py3Fdfs
Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from fdfs_client.client import Fdfs_client, get_tracker_conf
>>> tracker_path = get_tracker_conf('/etc/fdfs/client.conf')
>>> client = Fdfs_client(tracker_path)
>>> ret = client.upload_by_filename('./test')
>>> print(ret)
{'Group name': b'group1', 'Remote file_id': b'group1/M00/00/00/wKgfnl_z4IuAO4FrAAAABr99wgM4277861', 'Status': 'Upload successed.', 'Local file name': './test', 'Uploaded size': '6B', 'Storage IP': b'192.168.31.158'}
>>> 


默认上传文件

通过一个自定义一个存储类必须是Storage的子类

@deconstructible
class FDFSStorage(Storage):
    """fast dfs 文件存储类"""

    def _open(self, name, mode='rb'):
        """打开文件时候使用"""
        pass
示例#17
0
from fdfs_client.client import Fdfs_client
# 创建fast客户 加载客户端配置文件
client = Fdfs_client('./client.conf')
# 上传
ret = client.upload_by_filename('/home/liao/Desktop/Django-Framework-Logo.png')
print(ret)
"""
{
'Group name': 'group1', 
'Remote file_id': 'group1/M00/00/00/wKhn0lzH0w6AN8ZBAAC4j90Tziw25.jpeg', 
'Status': 'Upload successed.', 
'Local file name': '/Users/chao/Desktop/01.jpeg', 
'Uploaded size': '46.00KB', 
'Storage IP': '192.168.103.210'}
"""

示例#18
0
        def post(self):
            uri = self.request.uri
            
            avatar_sha1=self.get_argument("f_file.sha1",None)
            
            if avatar_sha1:
                user_key = self.get_secure_cookie("user_key")
                res = {}                
                
                upload_file_name=self.get_argument("f_file.name",None)
                ext = upload_file_name.split('.')[1]            
                img_path_t=os.path.join("/tmp/upload_temp_dir/",str(avatar_sha1))            
            
                img_path = "%s.%s" % (img_path_t,ext)
                
                upload_file_path=self.get_argument("f_file.path",None)
                
                os .rename(upload_file_path,img_path)
                
                
                client = Fdfs_client(globalSetting['fdfs_client_conf'])            
                ret = client.upload_by_filename(img_path)
                
                remote_file_id = ret['Remote file_id']
                storage_ip = ret['Storage IP']
                pic_url = "http://%s:%d/%s" % (storage_ip,globalSetting['fdfsPort'],remote_file_id)
                print pic_url
                from models.avatar import Avatar
                avatar = Avatar()
                avatar.key = user_key
                avatar.data['url'] = pic_url
                avatar.post()
                
                res['avatar'] = pic_url
                res['error'] = ''
                
                return self.dumpJson(res)
        
            upload_file_sha1=self.get_argument("ifile.sha1",None)
            #upload_file_size=self.get_argument("ifile.size",None)
            upload_file_path=self.get_argument("ifile.path",None)
            upload_file_name=self.get_argument("ifile.name",None)
            print upload_file_name
            ext = upload_file_name.split('.')[1]            
            
            img_path_t=os.path.join("/tmp/upload_temp_dir/",str(upload_file_sha1))            
            
            img_path = "%s.%s" % (img_path_t,ext)
            img_path_170x170 = "%s_170x170.%s" % (img_path_t,ext)
            
            try:
                i=Img()
                i.open('/usr/bin')
                i.convert_resize(input_file=upload_file_path,output_file=img_path,output_size="500x")                
                i.convert_thumbnail(input_file=img_path,output_file=img_path_170x170,output_size="170x170")                
            except Exception as what:
                #data=file(upload_file_path,'rb').close()                
                print what
                pass                       
            
            client = Fdfs_client(globalSetting['fdfs_client_conf'])            
            ret = client.upload_by_filename(img_path)

            remote_file_id = ret['Remote file_id']
            storage_ip = ret['Storage IP']
            pic_url = "http://%s:%d/%s" % (storage_ip,globalSetting['fdfsPort'],remote_file_id)
            
            ret = client.upload_by_filename(img_path_170x170)            
            remote_file_id = ret['Remote file_id']
            storage_ip = ret['Storage IP']
            thumb_url = "http://%s:%d/%s" % (storage_ip,globalSetting['fdfsPort'],remote_file_id)
            
            user_key = self.get_secure_cookie("user_key")
            
            board  = Board()
            query = {}
            query['q'] = "user:%s" % user_key            
            _data = board.solr(query)     
            
            self.render('upload_form.html',boards = _data,pic_url=pic_url,thumb_url=thumb_url)
示例#19
0
from fdfs_client.client import Fdfs_client
# 2. 创建FastDFS客户端实例
client = Fdfs_client('client.conf')
# 3. 调用FastDFS客户端上传文件方法
ret = client.upload_by_filename('/Users/apple/program/images/banner01.jpg')

print(ret)
示例#20
0
from fdfs_client.client import Fdfs_client

# 创建对象
client = Fdfs_client('./client.conf')
ret = client.upload_by_filename('/home/python/Desktop/upload_Images/01.jpeg')

# print(ret)
# {
#     'Group name': 'group1',
#     'Remote file_id': 'group1/M00/00/00/wKhug1zOn5GAGbM0AAC4j90Tziw02.jpeg',
#     'Status': 'Upload successed.',
#     'Local file name': '/home/python/Desktop/upload_Images/01.jpeg',
#     'Uploaded size': '46.00KB',
#     'Storage IP': '192.168.110.131'}
示例#21
0
from fdfs_client.client import Fdfs_client
# 2. 创建FastDFS客户端实例
client = Fdfs_client('client.conf')
# 3. 调用FastDFS客户端上传文件方法
ret = client.upload_by_filename('/home/libin/Desktop/2.jpg')
print(ret)
示例#22
0
from fdfs_client.client import Fdfs_client

client = Fdfs_client('client.conf')

ret = client.upload_by_filename('/Users/chenwei/Desktop/1.jpg')
print(ret)
示例#23
0
from fdfs_client.client import Fdfs_client

if __name__ == '__main__':
    # 创建连接对象
    client = Fdfs_client(
        '/home/python/Desktop/AdvancePython/day19_meiduo/meiduo_mall_123/meiduo_mall/meiduo_mall/utils/fdfs/client.conf'
    )
    # 上传文件
    ret = client.upload_by_filename('client.conf')
    # 响应值
    print(ret)
'''
mysql -h127.0.0.1 -uroot -pmysql meiduo_tbd39 < goods_data.sql
{
    'Storage IP': '192.168.47.128',
    'Group name': 'group1',
    'Uploaded size': '8.00KB',
    'Status': 'Upload successed.',
    'Local file name': '/home/python/Desktop/1.jpg',
    'Remote file_id': 'group1/M00/00/00/wKgvgFyVsQKANIKnAAAhg8MeEWU833.jpg'
}

'''
示例#24
0
class ItemCrawler:
    def __init__(
        self, item_id, num_id, crawl_path, server_path=None, org_server_path=None, statshost=None, statsport=0
    ):
        self.item_id = item_id
        self.results = {}
        self.headers = {"Referer": "http://item.taobao.com/item.htm?id=%s" % num_id, "User-Agent": DEFAULT_UA}
        self.success = False
        self.crawl_path = crawl_path
        self.server_path = server_path
        self.org_server_path = org_server_path
        self.image_set = set()
        self.fdfs_client = Fdfs_client("/etc/fdfs/client.conf")
        self.statshost = statshost
        self.statsport = statsport

    def crawl(self, image_rows, thumb_sizes, is_commit=False, conn=None, is_remove=False):
        self.prepare_path(thumb_sizes)
        for item in image_rows:
            self.crawl_image(item, thumb_sizes, conn)
        self.check_success(thumb_sizes)
        if is_commit:
            if self.success:
                self.commit(conn)
            else:
                self.commit_fail(conn)
        if is_remove:
            try:
                shutil.rmtree(self.crawl_tmp_path)
            except:
                pass

    def prepare_path(self, thumb_sizes):
        self.crawl_tmp_path = "%s/%s" % (self.crawl_path, self.item_id)
        self.static_url = "static/%s" % self.item_id
        try:
            shutil.rmtree(self.crawl_tmp_path)
        except:
            pass

        try:
            os.makedirs(self.crawl_tmp_path)
        except:
            pass

        for width, height in thumb_sizes:
            try:
                os.makedirs("%s/%s/%sx%s" % (self.crawl_path, self.item_id, width, height))
            except:
                pass

    def check_success(self, thumb_sizes):
        self.success_type1_count = 0
        self.success_count = 0
        self.type1_count = 0
        self.count = 0
        has_type1_pos1 = False
        for pos, img in self.results.items():
            self.count += 1
            if img.image_type == 1:
                self.type1_count += 1
                if img.pos == 1:
                    has_type1_pos1 = True
            if not img.disabled and len(img.thumbs) == len(thumb_sizes):
                self.success_count += 1
                if img.image_type == 1:
                    self.success_type1_count += 1
        if self.success_type1_count > 0 and has_type1_pos1:
            self.success = True
        else:
            self.success = False
        self.summary = {
            "suc1": self.success_type1_count,
            "count1": self.type1_count,
            "suc": self.success_count,
            "count": self.count,
        }

    def commit_fail(self, conn):
        conn.execute(
            'update crawl_html set is_image_crawled=0,last_modified=now(),crawl_image_info="%s" where item_id=%s'
            % (self.summary, self.item_id)
        )
        # conn.execute("update item set crawl_status=1 where id=%s" % self.item_id)

    def commit(self, conn):
        try:
            trans = conn.begin()
            conn.execute("delete from item_images where item_id=%s;" % self.item_id)
            for pos, img in self.results.items():
                if img.disabled or not img.url or not img.fullpath:
                    continue

                if len(img.thumbs) == 0:
                    continue

                if len(img.thumbs) > 1:
                    raise Exception("Thumbs size list len MUST BE ONE")

                for thumbsize, thumb_filename in img.thumbs.items():
                    # uploading to fastdfs
                    ret = self.fdfs_client.upload_by_filename(thumb_filename)
                    if ret["Status"] == "Upload successed.":
                        group_name = ret["Group name"]
                        remote_file = ret["Remote file_id"]
                        # update database
                        conn.execute(
                            "insert into item_images (item_id,url,local_url,type,width,height,pos,is_downloaded,has_thumbnail,uniq_url, fastdfs_group, fastdfs_filename) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                            self.item_id,
                            img.url,
                            img.get_server_path(),
                            img.image_type,
                            img.width,
                            img.height,
                            img.pos,
                            1,
                            1,
                            img.uniq_url,
                            group_name,
                            remote_file,
                        )
                        logger.debug("upload to fastdfs %s -> %s", thumb_filename, remote_file)
                    else:
                        logger.warn("upload to fastdfs failed, reason: %s", ret)
                        raise Exception("upload failed %s" % ret)
            conn.execute(
                'update crawl_html set is_image_crawled=1,crawl_image_info="%s" where item_id=%s'
                % (self.summary, self.item_id)
            )
            conn.execute("update item set crawl_status=2 where id=%s" % self.item_id)
            trans.commit()
        except Exception, e:
            logger.info("commit %s failed error: %s", self.item_id, traceback.format_exc())
            trans.rollback()
            raise
示例#25
0
class RequestSelf(object):
    def __init__(self):
        self.savepath = Config.BASE_DIR + '/file'
        self.client = Fdfs_client('./utils/client.conf')
        self.fastdfs = '''http://10.225.136.172:8889/'''

    def getargs(self):
        args = request.args.to_dict()
        return args

    def getbody(self):
        data = request.data.decode()
        return data

    def getjson(self):
        try:
            data = request.json
        except:
            data = {}
        return data

    def all_path(self, dirname):
        result = []
        for maindir, subdir, file_name_list in os.walk(dirname):
            for filename in file_name_list:
                if not filename.endswith('py'):
                    continue
                apath = os.path.join(maindir, filename)
                result.append(apath)
        return result

    def savefile(self, path, content):
        content = base64.b64decode(content.encode())
        os.makedirs(os.path.dirname(path), exist_ok=True)
        savepath = os.path.join(path)
        try:
            with open(savepath, 'wb') as f:
                f.write(content)
        except:
            pass
        return savepath

    def create(self, obj):
        try:
            obj.createTime = datetime.now()
            db.session.add(obj)
            db.session.commit()
        except Exception as e:
            print(str(e))
            with open('./createerror.log', 'w') as f:
                f.write(str(e))
            db.session.rollback()

    def update(self, obj):
        try:
            try:
                obj.updateTime = datetime.now()
                db.session.commit()
            except:
                db.session.commit()
        except Exception as e:
            print(str(e))
            with open('./updateerror.log', 'w') as f:
                f.write(str(e))
            db.session.rollback()

    def upload(self, path):
        res = self.client.upload_by_filename(path)
        return res

    def download(self, localpath, file_id):
        res = self.client.download_to_file(local_filename=localpath,
                                           remote_file_id=file_id)
        return res

    def verip(self, ip):
        try:
            ip, port = ip.split(":")
        except:
            return False
        p = re.compile(
            '^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$'
        )
        if not p.match(ip):
            return False
        try:
            port = int(port)
        except:
            return False
        if port > 65535 or port < 1000:
            return False
        return True

    @staticmethod
    def _fileback(uid, name, file):
        file_dir = os.path.join('./file', str(uid), name)
        shutil.copy(file, file_dir)

    @staticmethod
    def zipDir(dirpath, outFullName):
        """
        压缩指定文件夹
        :param dirpath: 目标文件夹路径
        :param outFullName: 压缩文件保存路径+xxxx.zip
        :return: 无
        """
        zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED)
        for path, dirnames, filenames in os.walk(dirpath):
            # 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
            fpath = path.replace(dirpath, '')
            for filename in filenames:
                zip.write(os.path.join(path, filename),
                          os.path.join(fpath, filename))
        zip.close()

    def page(self, page, pageSize, obj):
        page_max_size = 30
        if pageSize < page_max_size:
            page_max_size = pageSize
        num = len(obj)
        if num <= page_max_size:
            if page != 1:
                return []
            return obj
        elif page in [0, 1]:
            obj = obj[0:page_max_size]
            return obj
        elif (num - page_max_size * (page - 1)) <= page_max_size:
            return obj[(page - 1) * page_max_size:]
        else:
            return obj[(page - 1) * page_max_size:page_max_size * page]
示例#26
0
from fdfs_client.client import Fdfs_client

if __name__ == '__main__':
    # 创建连接对象
    client = Fdfs_client('client.conf')
    # 上传文件
    ret = client.upload_by_filename('/home/python/Desktop/1.jpg')
    # 响应值
    print(ret)
'''
mysql -h127.0.0.1 -uroot -pmysql meiduo_tbd39 < goods_data.sql
{
    'Storage IP': '192.168.47.128',
    'Group name': 'group1',
    'Uploaded size': '8.00KB',
    'Status': 'Upload successed.',
    'Local file name': '/home/python/Desktop/1.jpg',
    'Remote file_id': 'group1/M00/00/00/wKgvgFyVsQKANIKnAAAhg8MeEWU833.jpg'
}

'''
示例#27
0
from fdfs_client.client import Fdfs_client

# 创建FastDFS客户端
client = Fdfs_client("./client.conf")

# 使用FastDFS客户端上传文件(图片)
ret = client.upload_by_filename("/home/python/Desktop/upload_Images/kk02.jpeg")
print(ret)

"""
ret = {'Group name': 'group1', 
'Remote file_id': 'group1/M00/00/00/wKhvl1zuV_OAB1wqAAEXU5wmjPs10.jpeg',
 'Status': 'Upload successed.', 
 'Local file name': '/home/python/Desktop/upload_Images/kk02.jpeg', 
 'Uploaded size': '69.00KB', 
 'Storage IP': '192.168.111.151'}


ret = {
'Group name': 'Storage组名',
'Remote file_id': '文件索引,可用于下载',
'Status': '文件上传结果反馈',
'Local file name': '上传文件全路径',
'Uploaded size': '文件大小',
'Storage IP': 'Storage地址'
 }
 
"""
示例#28
0
 def save_fastfs(self):
     client = Fdfs_client('/etc/fdfs/client.conf')
     rv = client.upload_by_filename(self.tmp_path)
     return rv
示例#29
0
# 导入库
from fdfs_client.client import Fdfs_client
# 创建客户端,需要加载配置文件,配置文件的tracker必须设置正确
client = Fdfs_client('配置文件路径')

# 上传照片
client.upload_by_filename('路径')

# 返回结果
# getting connection
# <fdfs_client.connection.Connection object at 0x7f69cc63c828>
# <fdfs_client.fdfs_protol.Tracker_header object at 0x7f69cc63c7f0>
# {
# 'Group name': 'group1',
# 'Remote file_id': 'group1/M00/00/00/wKiOllvGHF6AS5ckAADIlkvVtHw058.jpg',
# 'Status': 'Upload successed.',
# 'Local file name': '/home/python/Desktop/images/timg.jpg',
# 'Uploaded size': '50.00KB',
# 'Storage IP': '192.168.142.150'
# }
示例#30
0
from fdfs_client.client import Fdfs_client

# 导入配置文件:
# 注意:windows环境下绝对路径会发生转义,需要加上 "r" 说明是原生字符串
client = Fdfs_client(r'client.conf')

# 上传图片:
# 注意:windows环境下绝对路径会发生转义,需要加上 "r" 说明是原生字符串
conf = client.upload_by_filename(r'E:\Document And Settings2\zy\Desktop\收款码.png')
print(conf)

# 192.168.0.169:8888/group1/M00/00/00/wKgAqV6RYJWAS677AABtCxL_mKk331.png
示例#31
0
from fdfs_client.client import Fdfs_client

client = Fdfs_client(
    r'F:\PyCharm 2018.1.4\elect_web\venv\Lib\fdfs_client\client.conf')
ret = client.upload_by_filename(r'D:\goods.jpg')
示例#32
0
from fdfs_client.client import Fdfs_client
client = Fdfs_client("client.conf")
ret = client.upload_by_filename('1.jpg')
print(ret)    

示例#33
0
#fastdfs example

from fdfs_client.client import Fdfs_client, get_tracker_conf

tracker_path = get_tracker_conf()
client = Fdfs_client(tracker_path)
ret = client.upload_by_filename('/home/arvin1995/Desktop/pycharm.png')