python怎么添加包,python form表單上傳服務器文件_如何使用multipart/form POST將文件上傳到需要上載的文件的URL(chunks

 2023-12-25 阅读 27 评论 0

摘要:是否有機會通過API端點上傳文件,該端點將多部分/表單數據作為只有該文件URL的內容類型?在python怎么添加包。規則:將整個文件下載到內存中,然后由這個端點上傳不是一個選項(不能保證這個盒子會足夠大來容納一個臨時文件)。在問題:我想將文

是否有機會通過API端點上傳文件,該端點將多部分/表單數據作為只有該文件URL的內容類型?在

python怎么添加包。規則:

將整個文件下載到內存中,然后由這個端點上傳不是一個選項(不能保證這個盒子會足夠大來容納一個臨時文件)。在

問題:

我想將文件分塊從一個服務器(GET)流到另一個服務器(multipart/formdatapost)。這可能嗎?如何做到這一點?在

流量:

文件服務器<-GET-my_腳本.py-上傳后服務器

下面是下載到內存(RAM)選項的一個簡單示例(但這違反了規則):from io import BytesIO

import requests

from requests_toolbelt.multipart.encoder import MultipartEncoder

file_url = 'https://www.sysaid.com/wp-content/uploads/features/itam/image-banner-asset.png'

requested_file_response = requests.get(file_url, stream=True)

TOKEN_PAYLOAD = {

'grant_type': 'password',

'client_id': '#########',

'client_secret': '#########',

'username': '#########',

'password': '#########'

}

def get_token():

response = requests.post(

'https://upload_server/oauth/token',

params=TOKEN_PAYLOAD)

response_data = response.json()

token = response_data.get('access_token')

if not token:

print("token error!")

return token

token = get_token()

file_object = BytesIO()

file_object.write(requested_file_response.content)

# Form conctent

multipart_data = MultipartEncoder(

fields={

'--': (

'test.png',

file_object # AttributeError: 'generator' object has no attribute 'encode' when I try to pass generator here.

),

'id': '2217',

'fileFieldDefId': '4258',

}

)

# Create headers

headers = {

"Authorization": "Bearer {}".format(token),

'Content-Type': multipart_data.content_type

}

session = requests.Session()

response = session.post(

'https://upload_server/multipartUpdate',

headers=headers,

data=multipart_data,

)

答案是在一個類似于文件的對象創建中,用于流的目的

非常感謝你的幫助。干杯!在

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/196722.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息