2013-11-26 58 views
2

這裏我試圖使用requests.post進行文件上傳。'requests.post'文件上傳大文件:(大於〜1.5 MB):Python

寫過程。

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: {}>" 

我在這裏失去了一些東西?請在同一個引導。

摘要:在這裏,我試圖通過Python上的GCS(谷歌雲存儲)上傳文件。

+0

您可以使用流式傳輸請求工具區大型多附件。請參閱https://toolbelt.readthedocs.org/en/latest/uploading-data.html#streaming-multipart-data-encoder – flup

回答

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)