Media

list_media_files

Client.list_media_files()

Gets a list of user’s media files.

Return type:types.GeneratorType
Returns:list of media files

Example: list media files and save any with the name dog in file name:

media_list = api.list_media_files()
for media in media_list:
    if 'dog' in media['media_name'].lower():
        stream, content_type = api.download_media_file(media['media_name'])
        with io.open(media['media_name'], 'wb') as file:
            file.write(stream.read())

upload_media_file

Client.upload_media_file(media_name, content=None, content_type='application/octet-stream', file_path=None)

Upload a file

Parameters:
  • media_name (str) – name of file on bandwidth server
  • content (str|buffer|bytearray|stream|file) – content of file to upload (file object, string or buffer). Don’t use together with file_path
  • content_type (str) – mime type of file
  • file_path (str) – path to file to upload. Don’t use together with content

Example: Upload text file:

api.upload_media_file('file1.txt', 'content of file', 'text/plain')

# with file path
api.upload_media_file('file1.txt', file_path='/path/to/file1.txt')

download_media_file

Client.download_media_file(media_name)

Download a file

Parameters:media_name (str) – name of file on bandwidth server

:rtype (stream, str) :returns stream to file to download and mime type

Example: list media files and save any with the name dog in file name:

media_list = api.get_media_files()
for media in media_list:
    if 'dog' in media['media_name'].lower():
        stream, content_type = api.download_media_file(media['media_name'])
        with io.open(media['media_name'], 'wb') as file:
            file.write(stream.read())