Openstack Rest API使用python将文件发送到Openstack容器 [英] Openstack Rest API send a file with python to a openstack container
问题描述
我正在尝试使用REST API将视频文件上传到Openstack容器. 这是我要上传到服务器的Python代码.
I am trying to upload a video file to Openstack container using REST API. This is my Python code to upload to the server.
res = requests.put(publicURL+'/'+output_container_name+'/'+toUpload,
headers={'X-Auth-Token': token},
files={'file': open(toUpload,'rb')})
您在代码中看到的所有变量都已定义.实际上,我可以看到我的文件上传到了容器中,但是当我下载文件时,却无法播放视频. 当我使用文本编辑器打开视频文件时,我会在文件的开头和结尾看到这些标题.
All the variables that you see in code are defined. In fact I can see my file uploaded to the container but when I download it, I cannot play the video. When I open the video file with a text editor I see these headers at the beginning and at the end of the file.
--0b2e78b52dad4b45a43575c1c42b0b9d
Content-Disposition: form-data; name="file"; filename="input_output.mp4"
.
.
. Normal video content
.
.
--0b2e78b52dad4b45a43575c1c42b0b9d--
如何摆脱文件中的这些标头?
How can I get rid of these header in the file?
即使我手动删除了标头,使用diff检查它们时文件中仍然存在一些差异.视觉上看不到差异,行数相同,并且所有内容看起来都相同.
Even when I remove the headers manually there is still some differences in the files when I check them with diff. The Differences are not visible visually the number of lines are the same and everything look the same.
推荐答案
尝试使用Python OpenStack SDK.
Give the Python OpenStack SDK a try.
pip install openstacksdk
用于上传文件的代码.
import sys
from openstack import connection
from openstack import profile
from openstack import utils
utils.enable_logging(True, stream=sys.stdout)
prof = profile.Profile()
prof.set_region(prof.ALL, "RegionOne")
conn = connection.Connection(
auth_url='https://my.identity.endpoint/v2.0/',
profile=prof,
username="my_username",
password="my_password")
c = conn.object_store.create_container(name='videos')
obj = conn.object_store.create_object(container=c.name,
name='input_output.mp4',
data=open('input_output.mp4', 'r'),
content_type='video/mp4')
print(obj)
更多可能也有帮助的信息:
More info that might be helpful too:
- http://python-openstacksdk.readthedocs.org/en/latest/
- https://pypi.python.org/pypi/openstacksdk
这篇关于Openstack Rest API使用python将文件发送到Openstack容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!