File Sharing API for Python SDK

The PubNub Python SDK uploads, lists, downloads, and deletes files on channels with File Sharing, and send_file() publishes a file message that announces each upload.

File Sharing must be enabled for the keyset in the Admin Portal. A file can be up to 5 MB. When Access Manager (v2/v3) is enabled, file operations need channel permissions. For how File Sharing works, retention, and permissions, see Files.

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.

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

    1def my_callback_function(result, status):
    2 print(f'TT: {result.timetoken}, status: {status.category.name}')
    3
    4pubnub.publish() \
    5 .channel("myChannel") \
    6 .message("Hello from PubNub Python SDK") \
    7 .pn_async(my_callback_function)

A failed request raises a PubNubException from .sync(). The methods list_files(), get_file_url(), and delete_file() accept their parameters as keyword arguments or through builder methods of the same name. The methods send_file(), download_file(), and publish_file_message() take no arguments, so set their parameters through builder methods.

Send file​

send_file() uploads a file to a channel through a PubNub client for Python, then publishes a file message that announces the upload.

send_file() requests an upload URL from PubNub, uploads the file to the storage service, and publishes the file message with the file ID and name. For the publish step, see Publish file message.

Subscribers to the channel receive a file event with the file ID, the file name, and the optional message that came with the upload. The Python SDK does not deliver file events to listeners, so read the file message with fetch_messages() from Message Persistence instead.

To encrypt files, set cipher_key on PNConfiguration. The SDK then encrypts the file content with the configured crypto module, or with the legacy module if you set none. It also encrypts the file message, which holds message, the file ID, and the file name. The SDK sends channel and meta unencrypted.

required
channel required#
Type: str
Default: no default
ID of the channel to upload the file to. A request without a channel fails validation with the message Channel missing.
file_name required#
Type: str
Default: no default
Name of the file to upload, which PubNub stores it under. A request without a file name fails validation with the message File name is missing..
file_object required#
Type: Union[BinaryIO, bytes]
Default: no default
Content to upload, as a binary file object opened for reading or as bytes. A request without content fails validation with the message File object is missing..
message#
Type: Any
Default: no default
Message that travels with the file, such as a caption. Subscribers receive it in the file event.
should_store#
Type: bool
Default: False
Whether PubNub stores the file message in Message Persistence. true stores it and false skips storage. PubNub stores the message only when Message Persistence is enabled for the keyset. When omitted, the SDK sends store=0, so PubNub does not store the file message.
ttl#
Type: int
Default: 0
The number of hours that Message Persistence keeps the file message. When omitted, the SDK sends 0, and the retention of the keyset applies. PubNub ignores this value when it does not store the message.
meta#
Type: Any
Default: no default
Metadata that PubNub attaches to the file message. Subscribers can use it in message filters.
custom_message_type#
Type: str
Default: no default
A business-specific label for the message, for example text, action, or poll.

A custom message type accepted by the PubNub Publish, Signal, and File Sharing APIs must be a case-sensitive alphanumeric string of 3 to 50 characters. Dashes (-) and underscores (_) are allowed. The value cannot start with a special character or with the reserved prefixes pn_ or pn-.

The deprecated cipher_key builder method is documented in Deprecated cipher_key parameter.

Memory usage for large files

The SDK doesn't cap upload size. Your keyset's file size limit is 5 MB by default and can differ. The method send_file() reads the entire file into memory to encrypt and upload it, so memory use grows with file size.

Enforce your keyset's configured limit in your own upload path before calling send_file().

Returns​

send_file() returns an Envelope whose result is a PNSendFileResult and whose status is a PNStatus.

PNSendFileResult​

name#
Type: str
Name of the uploaded file, as PubNub stores it.
file_id#
Type: str
ID of the uploaded file, which PubNub assigned at upload.
timestamp#
Type: str
Timetoken at which PubNub published the file message.

Syntax

1pubnub.send_file() \
2 .channel(channel: str) \
3 .file_name(file_name: str) \
4 .file_object(file_object: Union[BinaryIO, bytes]) \
5 .message(message: Any) \
6 .should_store(should_store: bool) \
7 .ttl(ttl: int) \
8 .meta(meta: Any) \
9 .custom_message_type(custom_message_type: str) \
10 .sync() -> Envelope

List channel files​

list_files() returns the files that File Sharing stores for a channel, using a PubNub client for Python.

required
channel required#
Type: str
Default: no default
ID of the channel to list the files of. A request without a channel fails validation with the message Channel missing.
limit#
Type: int
Default: no default
Number of files to return in one call. A single list-files call returns up to 100 files for a channel.
next#
Type: str
Default: no default
Token from the next value of the previous response, which continues the list where that response ended.

Returns​

list_files() returns an Envelope whose result is a PNGetFilesResult and whose status is a PNStatus.

PNGetFilesResult​

data#
Type: list of dict
The files on the channel, one dict per file.
count#
Type: int
Number of files in this response.
next#
Type: str
Token for the next page, which you pass as next in the following call.

Each dict in data holds these keys:

KeyTypeDescription
idstrID of the file, which PubNub assigned at upload.
namestrName of the file, as PubNub stores it.
sizeintSize of the file in bytes.
createdstrTime at which PubNub stored the file, as an ISO 8601 string.

Syntax

1pubnub.list_files(
2 channel: str,
3 *,
4 limit: int = None,
5 next: str = None
6) -> ListFiles

Get file URL​

get_file_url() requests the download URL of a file on a channel from PubNub, using a PubNub client for Python.

The URL points to the file as PubNub stores it, so a file that the sender encrypted stays encrypted.

required
channel required#
Type: str
Default: no default
ID of the channel that holds the file. A request without a channel fails validation with the message Channel missing.
file_name required#
Type: str
Default: no default
Name of the file to build the URL for, as PubNub stores it. A request without a file name fails validation with the message File name is missing..
file_id required#
Type: str
Default: no default
ID of the file to build the URL for, which PubNub assigned at upload. A request without a file ID fails validation with the message File id is missing..

The sample reads id and name from file_data, an entry of the data list that list_files() returns.

Returns​

get_file_url() returns an Envelope whose result is a PNGetFileDownloadURLResult and whose status is a PNStatus.

PNGetFileDownloadURLResult​

file_url#
Type: str
URL from which to download the requested file.

Syntax

1pubnub.get_file_url(
2 channel: str,
3 file_name: str,
4 file_id: str
5) -> GetFileDownloadUrl

Download file​

download_file() downloads a file from a channel through a PubNub client for Python.

With cipher_key set on PNConfiguration, the SDK decrypts the file content with the configured crypto module, or with the legacy module if you set none.

required
channel required#
Type: str
Default: no default
ID of the channel to download the file from. A request without a channel fails validation with the message Channel missing.
file_name required#
Type: str
Default: no default
Name of the file to download, as PubNub stores it. A request without a file name fails validation with the message File name is missing..
file_id required#
Type: str
Default: no default
ID of the file to download, which PubNub assigned at upload. A request without a file ID fails validation with the message File id is missing..

The deprecated cipher_key builder method is documented in Deprecated cipher_key parameter.

Returns​

download_file() returns an Envelope whose result is a PNDownloadFileResult and whose status is a PNStatus.

PNDownloadFileResult​

data#
Type: bytes
Content of the downloaded file.

Syntax

1pubnub.download_file() \
2 .channel(channel: str) \
3 .file_name(file_name: str) \
4 .file_id(file_id: str) \
5 .sync() -> Envelope

Delete file​

delete_file() deletes a file from a channel through a PubNub client for Python.

required
channel required#
Type: str
Default: no default
ID of the channel to delete the file from. A request without a channel fails validation with the message Channel missing.
file_name required#
Type: str
Default: no default
Name of the file to delete, as PubNub stores it. A request without a file name fails validation with the message File name is missing..
file_id required#
Type: str
Default: no default
ID of the file to delete, which PubNub assigned at upload. A request without a file ID fails validation with the message File id is missing..

The sample reads id and name from file_data, an entry of the data list that list_files() returns.

Returns​

delete_file() returns an Envelope whose result is a PNDeleteFileResult and whose status is a PNStatus.

PNDeleteFileResult​

status#
Type: int
Status that PubNub returns for the deletion. The value 200 means success.

Syntax

1pubnub.delete_file(
2 channel: str,
3 file_name: str,
4 file_id: str
5) -> DeleteFile

Publish file message​

publish_file_message() publishes a file message for a file that PubNub already stores, using a PubNub client for Python.

The file message carries the file ID and the file name, which together identify the file for download.Call publish_file_message() when the upload succeeded but the publish step of send_file() failed, to publish the file message without uploading the file again.
required
channel required#
Type: str
Default: no default
ID of the channel to publish the file message to. A request without a channel fails validation with the message Channel missing.
file_id required#
Type: str
Default: no default
ID of the uploaded file, which PubNub assigned at upload. A request without a file ID fails validation with the message File id is missing..
file_name required#
Type: str
Default: no default
Name of the uploaded file, as PubNub stores it. A request without a file name fails validation with the message File name is missing..
message#
Type: Any
Default: no default
Message to publish with the file. Subscribers receive it in the file event.
should_store#
Type: bool
Default: False
Whether PubNub stores the file message in Message Persistence. true stores it and false skips storage. PubNub stores the message only when Message Persistence is enabled for the keyset. When omitted, the SDK sends store=0, so PubNub does not store the file message.
ttl#
Type: int
Default: 0
The number of hours that Message Persistence keeps the file message. When omitted, the SDK sends 0, and the retention of the keyset applies. PubNub ignores this value when it does not store the message.
meta#
Type: Any
Default: no default
Metadata that PubNub attaches to the file message. Subscribers can use it in message filters.
custom_message_type#
Type: str
Default: no default
A business-specific label for the file message, for example text, action, or poll.

The deprecated cipher_key builder method is documented in Deprecated cipher_key parameter.

Returns​

publish_file_message() returns an Envelope whose result is a PNPublishFileMessageResult and whose status is a PNStatus.

PNPublishFileMessageResult​

timestamp#
Type: str
Timetoken at which PubNub published the file message.

Syntax

1pubnub.publish_file_message() \
2 .channel(channel: str) \
3 .file_id(file_id: str) \
4 .file_name(file_name: str) \
5 .message(message: Any) \
6 .should_store(should_store: bool) \
7 .ttl(ttl: int) \
8 .meta(meta: Any) \
9 .custom_message_type(custom_message_type: str) \
10 .sync() -> Envelope

Deprecated cipher_key parameter​

The cipher_key builder method of send_file(), publish_file_message(), and download_file() is deprecated in the PubNub Python SDK and issues a warning. The cipher key in the client configuration replaces it.

The reference for apps that still call cipher_key, with the replacement, is File Sharing API: deprecated cipher_key parameter for Python SDK. New apps set cipher_key on PNConfiguration and, to use 256-bit AES-CBC, a crypto_module.

Was this page useful?

Last updated on