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.2.0, Dart 9.2.0, Go v10.2.0, Java 14.2.0, JavaScript 14.0.0, Kotlin 14.2.0, Objective-C 7.1.0, Python 10.8.1, and Swift 11.1.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.