从Google存储桶下载文件时,我遇到了一个奇怪的问题。
如果我在Linux上并运行此代码,则64kb PDF文件需要5分钟的下载时间。
+-----------+-----+-------+
| Date | ID | Value |
+-----------+-----+-------+
| 1/25/2019 | 111 | 50 |
| 1/26/2019 | 111 | 100 |
| 1/27/2019 | 111 | 150 |
| 1/28/2019 | 111 | 150 |
| 1/29/2019 | 111 | 150 |
| 1/30/2019 | 111 | 150 |
| 1/31/2019 | 111 | 150 |
| 1/25/2019 | 222 | 500 |
| 1/26/2019 | 222 | 1000 |
| 1/27/2019 | 222 | 1500 |
| 1/28/2019 | 222 | 1500 |
| 1/29/2019 | 222 | 1500 |
| 1/30/2019 | 222 | 1500 |
| 1/31/2019 | 222 | 1500 |
+-----------+-----+-------+
这是def generate_document(request):
if not ensure_valid_user(request):
return redirect('/?result=0')
try:
long_name = request.GET['long_name']
short_name = request.GET['short_name']
file_data, size = CloudStorageManager.get_file(long_name)
response = HttpResponse(file_data, content_type='application/octet-stream')
response['Content-Disposition'] = 'attachment; filename={}'.format(short_name)
response['Content-Length'] = size
return response
except Exception as ex:
print(ex)
类中的重要方法:
CloudStorageManager
我迷失的是在Linux上,如果我从class CloudStorageManager:
# private key file, used for local testing
storage_client = storage.Client.from_service_account_json(
'CloudStorageAPIKey.json')
bucket = storage_client.get_bucket("my.private.bucket")
@staticmethod
def get_file(long_name):
bucket = CloudStorageManager.bucket
blob = bucket.blob(long_name)
file_string = blob.download_as_string()
return file_string, blob.size
方法中注释掉response['Content-Length'] = size
,下载将以正常速度进行,但是当我回到家并在Windows上用该行注释时,下载将再次耗时5分钟,并且可以与包含的行一起使用。
有人可以帮我解释我要去哪里吗?
答案 0 :(得分:1)
足够有趣
我通过分配以下来源的回复Content-Length
解决了该问题:
response['Content-Length'] = size
到
response['Content-Length'] = len(response.content)