File Sharing API for PubNub Python SDK

Allows users to upload and share files. You can upload any file of up to 5 MB in size. This feature is commonly used in social apps to share images, or in medical apps to share medical records for patients.

When a file is uploaded on a channel, it's stored and managed using a storage service, and associated with your key. Subscribers to that channel receive a file event which contains a file ID, filename, and optional description.

Request execution and return values

You can decide whether to perform the Python SDK operations synchronously or asynchronously.

  • .sync() returns an Envelope object, which has two fields: Envelope.result, whose type differs for each API, and Envelope.status of type PnStatus.

    pubnub.publish() \
    .channel("myChannel") \
    .message("Hello from PubNub Python SDK") \
    .sync()
  • .pn_async(callback) returns None and passes the values of Envelope.result and Envelope.status to a callback you must define beforehand.

    def my_callback_function(result, status):
    print(f'TT: {result.timetoken}, status: {status.category.name}')

    pubnub.publish() \
    .channel("myChannel") \
    .message("Hello from PubNub Python SDK") \
    .pn_async(my_callback_function)

Send file

Upload file / data to specified channel.

Method(s)

pubnub.send_file() \
.channel(String) \
.file_name(String) \
.message(Dictionary) \
.should_store(Boolean) \
.ttl(Integer) \
.file_object(Python Object File or bytes) \
.meta(Dictionary) \
.custom_message_type(String)
ParameterTypeRequiredDefaultDescription
channelStringYesChannel for the file.
file_nameStringYesName of the file to send.
messageDictionaryOptionalMessage which should be sent along with file to specified channel.
should_storeBooleanOptionalTrueWhether PubNub published file message should be stored in channel history.
ttlIntegerOptionalHow long message should be stored in channel's storage.
file_objectbytes or Python file objectYesInput stream with file content.
metaDictionaryOptionalMeta data object which can be used with the filtering ability.
custom_message_typeStringOptionalA case-sensitive, alphanumeric string from 3 to 50 characters describing the business-specific label or category of the message. Dashes - and underscores _ are allowed. The value cannot start with special characters or the string pn_ or pn-.

Examples: text, action, poll.
Deprecated parameter

The cipher_key parameter in this method is deprecated. We recommend that you configure the crypto module on your PubNub instance instead.

If you pass cipher_key as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Basic Usage


Returns

The send_file() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNSendFileResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNSendFileResult

Property NameTypeDescription
nameStringName of the uploaded file.
file_idStringID of the uploaded file.
timestampStringThe timetoken at which the message was published.

List channel files

Retrieve list of files uploaded to Channel.

Method(s)

pubnub.list_files() \
.channel(String)
ParameterTypeRequiredDefaultDescription
channelStringYesChannel to get the list of files.

Basic Usage


Returns

The list_files() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNGetFilesResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNGetFilesResult

Property NameTypeDescription
nextStringRandom string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off.
prevStringRandom string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data.
countIntNumber of files returned.
dataListList of channel files.

data contains the following properties:

Property NameTypeDescription
idLongId of the uploaded file.
nameStringName of the upload file.
sizeStringSize of the uploaded file.
createdStringTime of creation.

Get File Url

Generate URL which can be used to download file from target Channel.

Method(s)

pubnub.get_file_url() \
.channel(String) \
.file_id(String) \
.file_name(String)
ParameterTypeRequiredDescription
channelStringYesName of channel to which the file has been uploaded.
file_nameStringYesName under which the uploaded file is stored.
file_idStringYesUnique identifier for the file, assigned during upload.

Basic Usage


Returns

The get_file_url() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNGetFileDownloadURLResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNGetFileDownloadURLResult

Property NameTypeDescription
file_urlStringURL to be used to download the requested file.

Download file

Download file from specified Channel.

Method(s)

pubnub.download_file() \
.channel(String) \
.file_id(String) \
.file_name(String)
ParameterTypeRequiredDescription
channelStringYesName of channel to which the file has been uploaded.
file_nameStringYesName under which the uploaded file is stored.
file_idStringYesUnique identifier for the file, assigned during upload.
Deprecated parameter

The cipher_key parameter in this method is deprecated. We recommend that you configure the crypto module on your PubNub instance instead.

If you pass cipher_key as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.

Basic Usage


Returns

The download_file() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNDownloadFileResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNDownloadFileResult

Property NameTypeDescription
databytesPython bytes object.

Delete file

Delete file from specified Channel.

Method(s)

pubnub.delete_file() \
.channel(String) \
.file_id(String) \
.file_name(String)
ParameterTypeRequiredDescription
channelStringYesThe channel from which to delete the file.
file_idStringYesUnique identifier of the file to be deleted.
file_nameStringYesName of the file to be deleted.

Basic Usage


Returns

The download_file() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNDeleteFileResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNDeleteFileResult

Property NameTypeDescription
statusIntReturns a status code.

Publish file message

Publish file message from specified Channel.

Method(s)

pubnub.publish_file_message() \
.channel(String) \
.meta(Dictionary) \
.message(Dictionary) \
.file_id(String) \
.custom_message_type(String) \
.file_name(String) \
.should_store(Boolean) \
.ttl(Integer)
ParameterTypeRequiredDefaultDescription
channelStringYesName of channel to publish file message.
metaDictionaryOptionalMeta data object which can be used with the filtering ability.
messageDictionaryOptionalThe payload.
file_idStringYesUnique identifier of the file.
custom_message_typeStringOptionalA case-sensitive, alphanumeric string from 3 to 50 characters describing the business-specific label or category of the message. Dashes - and underscores _ are allowed. The value cannot start with special characters or the string pn_ or pn-.

Examples: text, action, poll.
file_nameStringYesName of the file.
should_storeBooleanOptionalTrueSet to False to not store this message in history. By default, messages are stored according to the retention policy you set on your key.
ttlIntOptional0How long the message should be stored in the channel's history. If not specified, defaults to the key set's retention value.

Basic Usage

# synchronous:
envelope = pubnub.publish_file_message() \
.channel("test_channel") \
.meta({"test": "test"}) \
.message({"test_message": "test"}) \
.custom_message_type("file-message") \
.file_id("fileID") \
.file_name("knights_of_ni.jpg") \
.ttl(22) \
.sync()

# multithreaded asynchronous:
def callback(response, status):
pass

show all 24 lines

Returns

The publish_file_message() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNPublishFileMessageResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNPublishFileMessageResult

The publish_file_message() operation returns a PNPublishFileMessageResult which contains the following property:

Property NameTypeDescription
timestampStringThe timetoken at which the message was published.
Last updated on