问题
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?
EDIT: 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.
回答1:
Give the Python OpenStack SDK a try.
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:
- http://python-openstacksdk.readthedocs.org/en/latest/
- https://pypi.python.org/pypi/openstacksdk
来源:https://stackoverflow.com/questions/32786418/openstack-rest-api-send-a-file-with-python-to-a-openstack-container