简体   繁体   English

'requests.post'文件上传大文件:(大于1.5 MB):Python

[英]'requests.post' Files Upload Large File: (More than ~1.5 MB): Python

Here I'm trying to use requests.post for file upload. 在这里,我尝试使用requests.post进行文件上传。

Wrote procedure. 编写程序。

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()

Here is procedure written against url: save-data-to-gcs 这是针对url编写的过程: save-data-to-gcs

Note: In this i'm reading file object using request.FILES 注意:在此,我正在使用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')

Above procedures working for less or equal to ~1.5 Mb size files . 以上过程适用于小于或等于~1.5 Mb大小的文件 But if we go beyond ~2.0 MB then App Engine throwing an error: 但是,如果我们超出~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: {}>"

Am i missing something here?, Please guide on the same. 我在这里想念什么吗?,请在同一指导。

Summary : Here am trying to upload a file through python on GCS (Google Cloud Storage). 摘要 :这里试图在GCS(Google Cloud Storage)上通过python上传文件。

This works fairly well for me (Python3 with requests): 这对我来说效果很好(带有请求的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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM