私はPython 3.6.2でHTTP.clientを使ってAPIと通信しています。Python http.client PUTメソッドを使用してバイナリ/ビデオファイルをアップロードするには?
ファイルをアップロードするには、3段階の処理が必要です。
私は正常にPOSTメソッドを使用して管理することができ、サーバーは私が期待したとおりにデータを返します。
実際のファイルをアップロードする必要があるステージはPUTメソッドです。実際のファイルへのポインタを格納するようにコードを構文解析する方法はわかりません。ファイルはmp4ビデオファイルです。ここで は私のnoobの注釈付きコードのスニペット:)
#define connection as HTTPS and define URL
uploadstep2 = http.client.HTTPSConnection("grabyo-prod.s3-accelerate.amazonaws.com")
#define headers
headers = {
'accept': "application/json",
'content-type': "application/x-www-form-urlencoded"
}
#define the structure of the request and send it.
#Here it is a PUT request to the unique URL as defined above with the correct file and headers.
uploadstep2.request("PUT", myUniqueUploadUrl, body="C:\Test.mp4", headers=headers)
#get the response from the server
uploadstep2response = uploadstep2.getresponse()
#read the data from the response and put to a usable variable
step2responsedata = uploadstep2response.read()
である私は戻って、この段階で取得しています応答が 「エラー400不正な要求 - ファイル情報を取得できませんでした。」です
これは、コードのbody = "C:\ Test.mp4"セクションに関連していると確信しています。
PUTメソッド内でファイルを正しく参照する方法を教えてください。あなたが期待するよう事前に
おかげ
Ledge。魅力のように働いた、ありがとう! – yekootmada