Skip to main content
 首页 » 编程设计

python之'requests.post' 文件上传大文件 : (More than ~1. 5 MB):Python

2024年11月01日11mate10pro

我在这里尝试使用 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() 

这是针对 url 编写的程序: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(谷歌云存储)上的 python 上传文件。

请您参考如下方法:

这对我来说效果很好(带有请求的 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)