List and manage stored files
This guide shows you how to list files uploaded to a channel and delete a file by its ID.
The examples on this page are written for the current SDK releases: C# 9.0.0, Dart 8.0.3, Go v10.0.0, Java 6.4.5, JavaScript 13.0.3, Kotlin 13.4.4, Objective-C 7.0.4, Python 10.7.2, and Swift 10.2.0. Each example assumes a PubNub client initialized for its SDK, unless the example sets one up itself.
Prerequisites:
- File Sharing must be enabled on your keyset in the Admin Portal.
- If Access Manager is enabled, the token must grant
readon the channel to list files anddeleteto remove them.
List files
A single list-files call returns up to 100 files for a channel. The response includes the file name, ID, size, and created timestamp for each file.
- JavaScript
- C#
- Go
- Objective-C
- Java
- Python
- Swift
- Dart
1const result = await pubnub.listFiles({ channel: 'my_channel' });
1PNResult<PNListFilesResult> listFilesResponse = await pubnub.ListFiles()
2 .Channel("my_channel")
3 .ExecuteAsync();
4PNListFilesResult listFilesResult = listFilesResponse.Result;
5PNStatus listFilesStatus = listFilesResponse.Status;
6if (listFilesResult != null)
7{
8 Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(listFilesResult));
9}
10else
11{
12 Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(listFilesStatus));
13}
1resListFile, statusListFile, errListFile := pn.ListFiles().
2 Channel("my_channel").Execute()
3fmt.Println(resListFile, statusListFile, errListFile)
4if resListFile != nil {
5 for _, m := range resListFile.Data {
6 fmt.Println(m.ID, m.Created, m.Name, m.Size)
7 }
8}
1PNListFilesRequest *request = [PNListFilesRequest requestWithChannel:@"my_channel"];
2request.limit = 100;
3request.next = ...;
4
5[self.client listFilesWithRequest:request
6 completion:^(PNListFilesResult *result, PNErrorStatus *status) {
7 if (!status.isError) {
8 /**
9 * Uploaded files list successfully fetched.
10 * result.data.files - List of uploaded files (information).
11 * result.data.next - Random 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.
12 * result.data.count - Total number of files uploaded to channel.
13 */
14 } else {
15 /**
show all 22 lines1pubnub.listFiles()
2 .channel("my_channel")
3 .async(result -> {
4 result.onSuccess(res -> {
5 System.out.println("files status: " + res.getStatus());
6 System.out.println("files status: " + res.getNext());
7 System.out.println("files status: " + res.getCount());
8 System.out.println("files status: " + res.getCount());
9 for (PNUploadedFile file : res.getData()) {
10 System.out.println("files fileId: " + file.getId());
11 System.out.println("files fileName: " + file.getName());
12 System.out.println("files fileSize: " + file.getSize());
13 System.out.println("files fileCreated: " + file.getCreated());
14 }
15 }).onFailure(exception -> {
show all 18 lines1# synchronous
2pubnub.list_files().channel("my_channel").sync()
3
4# multithreaded asynchronous
5def callback(result, status):
6 if status.is_error():
7 print(f"Request failed: {status.error_data}")
8 return
9 print(result)
10
11pubnub.list_files().channel("my_channel").pn_async(callback)
1
1import 'package:pubnub/pubnub.dart';
2
3void main() async {
4 var pubnub = PubNub(
5 defaultKeyset: Keyset(
6 subscribeKey: 'sub-c-abc-123', // Replace with your subscribe key
7 publishKey: 'pub-c-def-456' // Replace with your publish key
8 ),
9 );
10
11 try {
12 var files = await pubnub.files.listFiles('my_channel');
13
14 print('Number of files uploaded: ${files.count}');
15 for (var file in files.filesDetail) {
show all 21 linesIf more than 100 files exist on the channel, the response includes a more cursor. Pass it as next in a subsequent call to fetch the next page of results.
Delete a file
To delete a file, you need the file ID and file name returned by listFiles or from the original upload response.
- JavaScript
- C#
- Go
- Objective-C
- Java
- Kotlin
- Python
- Swift
- Dart
1
1
1
1PNDeleteFileRequest *request = [PNDeleteFileRequest requestWithChannel:@"channel"
2 identifier:@"<file-identifier>"
3 name:@"greetings.txt"];
4
5[self.client deleteFileWithRequest:request completion:^(PNAcknowledgmentStatus *status) {
6 if (!status.isError) {
7 // File successfully has been deleted.
8 } else {
9 /**
10 * Handle file delete error. Check 'category' property to find out possible issue
11 * because of which request did fail.
12 *
13 * Request can be resent using: [status retry]
14 */
15 }
show all 16 lines1
1
1pubnub.delete_file(
2 channel="ch1",
3 file_id=file_data['id'],
4 file_name=file_data['name']
5).sync()
6
7print("File deleted")
1
1await pubnub.files.deleteFile(
2 'my_channel', 'someFileID', 'cat_picture.jpg');
Deleting a file removes it from File Sharing storage. It does not remove the file event from Message Persistence history. If Message Persistence is enabled, the file event record remains in history after the file is deleted.
Related tasks
- Send a file to a channel. Upload a file and get back the file ID used for deletion.
- Receive and download files. Handle the file event on the subscriber side.