'requests.post'文件上传大文件:(超过~1.5 MB):Python

时间:2013-11-26 12:39:23

标签: python django google-app-engine python-requests

我在尝试使用requests.post进行文件上传。

编写程序。

import requests
def upload_file_to_gcs():
    url = 'http://127.0.0.1:8500/save-data-to-gcs/'
    f = {'file': ('Product_Master.csv', open('C:/Projects/bf/Product_Master.csv', 'rb')), 'file_name': 'Product_Master.csv'}
    r = requests.post(url, files=f)
    print r

upload_file_to_gcs()

以下是针对网址编写的程序:save-data-to-gcs

注意:在此我正在使用 request.FILES

阅读文件对象
def save_data_to_gcs(request):
    file_name = '/gs/bucket-name/' + request.FILES['file'].name # change bucket/object names to suit your needs
    writable_file_name = files.gs.create(file_name, mime_type='application/octet-stream',
                                     acl='public-read')
    with files.open(writable_file_name, 'a') as f:
        f.write(request.FILES['file'].read())
    files.finalize(writable_file_name)
    return HttpResponse('', mimetype='application/text')

以上程序适用于少于或等于~1.5 Mb尺寸文件。但如果我们超越~2.0 MB,则App Engine会抛出错误:

Exception in request:
Traceback (most recent call last):
  File "/base/data/home/apps/s~bfu/101.371906891057843424/common/zip-packages/django-1.1.zip/django/core/handlers/base.py", line 92, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/base/data/home/apps/s~bfu/101.371906891057843424/myapp/utils.py", line 50, in save_data_to_gcs
    logging.error(request.FILES['file'].name)
  File "/base/data/home/apps/s~bfu/101.371906891057843424/common/zip-packages/django-1.1.zip/django/utils/datastructures.py", line 203, in __getitem__
    raise MultiValueDictKeyError, "Key %r not found in %r" % (key, self)
MultiValueDictKeyError: "Key 'file' not found in <MultiValueDict: {}>"

我在这里遗漏了什么吗?请指导一下。

摘要:我正在尝试通过GCS(Google云端存储)上的python上传文件。

1 个答案:

答案 0 :(得分:1)

这对我来说效果很好(带有请求的Python3):

def upload_file(local_file, remote_file):  
    params = {"file": os.path.basename(remote_file),
              "folder": os.path.dirname(remote_file),
              "submit": "Submit"}
    with open(local_file, 'rb') as file_:
        try:
           response = requests.post(url=URL, data=params, auth=(USER, PASSWORD),
                                    files={"zip_file": file_}, verify=False)
        except TimeoutError:
            print("Connection timed out!")
        else:
            print(response)