简体   繁体   中英

upload video file by path on python not working

I tried to upload video file using python, the problem is the system cannot find the file even though i write the path of file. my code is like this:

import os
import requests

#step 1
host = 'https://blablabla.com'

test = {
    "upload_phase" : "start",
    "file_size" : 1063565
}

params = {
    "access_token":my_access_token, 
    "fields":"video_id, start_offset, end_offset, upload_session_id", 
}

vids = requests.post(host, params=params, data=test)
vids = vids.json()

try:
    video_id= vids["video_id"],
    start_offset= vids["start_offset"],
    end_offset= vids["end_offset"],
    upload_session_id= vids["upload_session_id"]
except:
    pass

print(vids)

###############################################################################
#step 2
###############################################################################
test = {
    "upload_phase" : "transfer",
    "start_offset" : start_offset,
    "upload_session_id": upload_session_id,
    "video_file_chunk": os.path.realpath('/home/def/Videos/test.mp4')
}

params = {
    "access_token":my_access_token, 
    "fields":"start_offset, end_offset", 
}

vids = requests.post(host, params=params, data=test)
vids = vids.json()

try:
    start_offset= vids["start_offset"],
    end_offset= vids["end_offset"]
except:
    pass

print(vids)

Many way i tried, like os.path.abspath, os.path, os.path.dirname, os.path.basename, os.path.isfile, os.path.isabs, os.path.isdir it's still doesn't work. even i give import os.path or import os .

In your code you just send path to your file as the string to server, but not file itself. You should try something like:

my_file = {'file_to_upload': open(os.path.realpath('/home/def/Videos/test.mp4'),'rb')} 
# You should replace 'file_to_upload' with the name server actually expect to receive
# If you don't know what server expect to get, check browser's devconsole while uploading file manually
vids = requests.post(host, params=params, files=my_file)

Also note that you might need to use requests.Session() to be able to handle cookies, access token...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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