简体   繁体   English

Openstack Rest API将带有python的文件发送到openstack容器

[英]Openstack Rest API send a file with python to a openstack container

I am trying to upload a video file to Openstack container using REST API. 我正在尝试使用REST API将视频文件上传到Openstack容器。 This is my Python code to upload to the server. 这是我上传到服务器的Python代码。

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? 如何摆脱文件中的这些标题?

EDIT: Even when I remove the headers manually there is still some differences in the files when I check them with diff. 编辑:即使我手动删除标题,当我用diff检查它们时,文件仍然存在一些差异。 The Differences are not visible visually the number of lines are the same and everything look the same. 视觉上看不到差异,线条数量相同,一切看起来都一样。

Give the Python OpenStack SDK a try. 试试Python OpenStack SDK。

pip install openstacksdk

The code for uploading a file. 上传文件的代码。

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: 更多可能也有帮助的信息:

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

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