502错误,下载其中包含多个图像的巨大zip文件时出现错误

问题描述

因此,我希望用户能够单击按钮并下载包含一堆图像的zip文件。单击该按钮后将调用一个视图,该按钮使用pysftp模块从SFTP服务器中获取图像并创建一个zip文件,并将响应发送给用户以进行下载。现在,在大多数情况下,此功能绝对可以正常工作,但在某些情况下,服务器会抛出502代理错误,这是因为图像尺寸过大(这是预感)或数量很大。 我认为这是一种超时,需要花费很多时间来查看事物。

如果有人可以提出解决方案,这将非常有帮助。我不希望在任何情况下都发生“ 502代理错误”。

仅供参考:仅当生产环境中的流量可能很高时,才会发生此错误

def download_images(request):
    if request.GET.get('profile_id') and request.GET.get('mattersheet_id'):
        profile_id = request.GET.get('profile_id')
        mattersheet_id = request.GET.get('mattersheet_id')

        cnopts = pysftp.Cnopts()
        cnopts.hostkeys = None
        sftp = pysftp.Connection(host=Conf.IMG_SERV_IP,username='blabla',cnopts=cnopts)
        file_dir = f'''{Conf.DIR_CATALOG_PRODUCT_PATH}/{profile_id}/{mattersheet_id}/products/'''
        # import pdb; pdb.set_trace()
        response = HttpResponse(content_type="application/force-download")
        zip_file = ZipFile(response,'w')
        products =  MatterSheetProducts.objects.filter(matter_sheet_id=mattersheet_id).values('product_code','product_name','product_id','product_category_name').distinct('product_category_name','product_name')            
        import time
        a = time.time()
        for product in products:
            try:
                if sftp.exists(f"{file_dir}{product.get('product_id')}.jpg"):
                    fh = sftp.open(f"{file_dir}{product.get('product_id')}.jpg",'rb')

                    category_name = product.get('product_category_name') if product.get('product_category_name') else ''
                    product_name  = product.get('product_name') if product.get('product_name') else ''
                    product_code  = product.get('product_code') if product.get('product_code') else ''

                    dash = '_' if product_code and product_name else ''

                    if category_name:
                        zip_file.writestr(f"{category_name}/{product_code}{dash}{product_name}.jpg",fh.read())
                    else:
                        zip_file.writestr(f"{product_code}{dash}{product_name}.jpg",fh.read())



            except:
                continue

        b = time.time()
        print(1073,b-a)
        response['Content-Type'] = 'application/force-download'
        response['Content-disposition'] = f"attachment; filename={mattersheet_id}_products.zip"
        return response

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)