Upload and Retrieve the Temporary File - Python
This guide demonstrates how to upload and retrieve files from VideoSDK's temporary file storage system using Python.
This is a server-side implementation where a Python participant(bot) will join an existing meeting, capture an image once the participant's video stream is available, and fetch the image once the video stream is disabled.
upload_base64
- By using the
upload_base64
method of theMeeting
class, you can upload your file to VideoSDK's temporary storage. - The
upload_base64
method works withbase64
encoded data. Therefore, it is necessary to convert your file intobase64
format. - You have to pass
base64_data
,token
, andfile_name
as parameters to theupload_base64
method. - The method will return the corresponding
file_url
, which you can then use to retrieve the file.
fetch_base64
- By using the
fetch_base64
method of theMeeting
class, you can retrieve your file from VideoSDK's temporary storage. - You have to pass the
url
(returned by theupload_base64
method) and thetoken
as parameters to retrieve the file. - The method will return the image in the form of
base64
encoded string.
Example
This example demonstrates how to use these methods in a server-side Python script.
The Python participant(bot) will join an existing meeting, capture an image once the participant's video stream is available, and fetch the image once the video stream is disabled.
from videosdk import MeetingConfig, VideoSDK, MeetingEventHandler, ParticipantEventHandler, Meeting, Participant
import asyncio
loop = asyncio.get_event_loop()
VIDEOSDK_TOKEN = "<VIDEOSDK_TOKEN>"
MEETING_ID = "<MEETING_ID>"
NAME = "VideoSDK Python"
file_url: str = None
async def capture_image_and_upload(meeting: Meeting, participant: Participant):
global file_url
img_str = await participant.async_capture_image()
# upload_base64
file_url = meeting.upload_base64(
base64_data=img_str,
token=VIDEOSDK_TOKEN,
file_name="videosdk_testing.jpg"
)
print("Uploaded URL:", file_url)
class MyParticipantEventHandler(ParticipantEventHandler):
def __init__(self, meeting: Meeting, participant: Participant) -> None:
super().__init__()
self.meeting = meeting
self.participant = participant
def on_stream_enabled(self, stream) -> None:
print("Stream enabled:", stream.kind)
if stream.kind == "video":
asyncio.ensure_future(capture_image_and_upload(
self.meeting, self.participant))
def on_stream_disabled(self, stream) -> None:
print("Stream disabled:", stream.kind)
if stream.kind == "video":
# fetch_base64
base64_data = self.meeting.fetch_base64(
url=file_url, token=VIDEOSDK_TOKEN)
print("Base64 data:", base64_data)
class MyMeetingEventHandler(MeetingEventHandler):
def __init__(self, meeting: Meeting) -> None:
super().__init__()
self.meeting = meeting
def on_participant_joined(self, participant) -> None:
print("Participant joined:", participant)
participant.add_event_listener(
MyParticipantEventHandler(
meeting=self.meeting,
participant=participant
))
async def main():
meeting_config = MeetingConfig(
token=VIDEOSDK_TOKEN,
meeting_id=MEETING_ID,
name=NAME,
mic_enabled=False,
webcam_enabled=False,
)
meeting = VideoSDK.init_meeting(**meeting_config)
meeting.add_event_listener(MyMeetingEventHandler(
meeting=meeting
))
await meeting.async_join()
if __name__ == "__main__":
loop.run_until_complete(main())
loop.run_forever()
note
- The file stored in the system will be automatically deleted once the current room/meeting comes to an end.
- Ensure that you have the necessary packages installed and environment variables set correctly before running the script.
API Reference
The API references for all the methods and events utilized in this guide are provided below.
Got a Question? Ask us on discord