简体   繁体   English

如何使用Python http.client PUT方法上传二进制/视频文件?

[英]How to upload a binary/video file using Python http.client PUT method?

I am communicating with an API using HTTP.client in Python 3.6.2. 我正在使用Python 3.6.2中的HTTP.client与API通信。

In order to upload a file it requires a three stage process. 为了上传文件,它需要三个阶段的过程。

I have managed to talk successfully using POST methods and the server returns data as I expect. 我已经成功地使用POST方法进行了交谈,并且服务器按预期返回了数据。

However, the stage that requires the actual file to be uploaded is a PUT method - and I cannot figure out how to syntax the code to include a pointer to the actual file on my storage - the file is an mp4 video file. 但是,需要上传实际文件的阶段是PUT方法-我无法弄清楚如何对代码进行语法化以在存储中包含指向实际文件的指针-该文件是mp4视频文件。 Here is a snippet of the code with my noob annotations :) 这是带有我的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()

The response I am getting back at this stage is an "Error 400 Bad Request - Could not obtain the file information." 我在此阶段返回的响应是“错误400错误的请求-无法获取文件信息。”

I am certain this relates to the body="C:\\Test.mp4" section of the code. 我确定这与代码的body =“ C:\\ Test.mp4”部分有关。

Can you please advise how I can correctly reference a file within the PUT method? 您能建议我如何在PUT方法中正确引用文件吗?

Thanks in advance 提前致谢

uploadstep2.request("PUT", myUniqueUploadUrl, body="C:\Test.mp4", headers=headers)

will put the actual string "C:\\Test.mp4" in the body of your request, not the content of the file named "C:\\Test.mp4" as you expect. 会将实际的字符串"C:\\Test.mp4"放在请求的正文中,而不是您期望的名为"C:\\Test.mp4"的文件的内容。

You need to open the file, read it's content then pass it as body. 您需要打开文件,读取文件内容,然后将其作为主体传递。 Or to stream it, but AFAIK http.client does not support that, and since your file seems to be a video, it is potentially huge and will use plenty of RAM for no good reason. 或进行流式传输,但是AFAIK http.client不支持该流式传输,并且由于您的文件似乎是视频,因此它可能很大,并且会在没有充分理由的情况下使用大量RAM。

My suggestion would be to use requests , which is a way better lib to do this kind of things: 我的建议是使用requests ,这是一种更好的lib来做这种事情的方法:

import requests
with open(r'C:\Test.mp4'), 'rb') as finput:
   response = requests.put('https://grabyo-prod.s3-accelerate.amazonaws.com/youruploadpath', data=finput)
   print(response.json())

I do not know if it is useful for you, but you can try to send a POST request with requests module : 我不知道它是否对您有用,但是您可以尝试使用请求模块发送POST请求:

import requests
url = ""
data = {'title':'metadata','timeDuration':120}
mp3_f = open('/path/your_file.mp3', 'rb')
files = {'messageFile': mp3_f}

req = requests.post(url, files=files, json=data)
print (req.status_code)
print (req.content)

Hope it helps . 希望能帮助到你 。

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

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