Files
Stores allow you to exchange and save files. In this section, we'll take a closer look at file structures and how to manage them effectively. We'll also cover some best practices when working with Stores.
About Files
Along with a file's main content, additional metadata is stored to allow easy file management. Below is the structure of the metadata associated with each file:
| field | type | encrypted | description |
|---|---|---|---|
info | ServerFileInfo | yes | additional information assigned by the server e.g. author, creationDate, storeID and fileID |
publicMeta | binary | no | additional public information about the message, also accessible through Bridge API |
privateMeta | binary | yes | additional information about the message |
size | number | yes | The size of file in getBytes |
authorPubKey | string | yes | The public key of the author of the file |
statusCode | number | no | 0 if the file was decrypted successfully |
Sample code on this page is based on the initial assumptions
Uploading a Simple File
To place a file in the Store, you need to create a file handle using the createFile(...) function. The function accepts a StoreId,
public and private metadata,the total size of the file that will be uploaded, and set up a flag that tells whether the file has the random write feature enabled.
Then you need to call writeToFile(...) providing the created file handle and a portion of data to write. For small files, you can write the entire file with one request. For large files, it's more optimal to write them piece by piece, loading them in portions, for example from the disk, and then calling writeToFile(...) on each piece.
To complete the entire file upload process - call closeFile(...) with the file handle as a parameter.
Streaming
For larger files or scenarios where you need more control over the upload process (e.g. managing upload progress, pausing, or canceling uploads), we recommend using streaming. Streaming allows you to upload the file in chunks, enabling better interactivity, monitoring, and control.
Streaming is especially useful when:
- You need to upload large files and a single upload might take a long time;
- You need the ability to cancel the upload in the middle of the process.
Uploading large file - writing to Store chunk by chunks:
Let's say you have an Html input element in the page body:
Now, access that above input element from the JS code and get a file from there:
Reading a file
To read a file, you can do it with a single request or - which is usually a better solution for large files - by reading it piece by piece.
You must start by opening the file for reading using the openFile(fileId) function. As a result, you'll receive a file handle that
you then use when calling readFromFile(...) to read portions of data from the file. At the same time, you have the ability to move through
the file and indicate from which position you want to read it using the seekInFile(...) function.
Downloading Files
The code example below shows how to download a file from a Store in PrivMX:
Listing Files
Listing files in a chosen Store:
As a result you will receive an object:
A detailed description of the File object fields can be found in API Reference.
Modifying Files
File modification can be considered in two categories:
- Modifying file information.
- Modifying data in the file.
To update information about a file, you need to use the updateFileMeta(...).
Deleting a File
To delete a file, you need to use the deleteFile(...) method.
Notes
- Metadata: helps you identify files later on by storing additional information about them. You can use both private metadata (which might contain sensitive or internal data) and public metadata (which can be shared publicly).
- Error Handling: During the streaming process, it's important to handle potential errors, such as network interruptions, by using try-catch blocks and the ability to abort the upload.
- Progress Monitoring: Streaming enables you to monitor the progress of the file upload, allowing you to display the current upload percentage to the user.
We use cookies on our website. We use them to ensure proper functioning of the site and, if you agree, for purposes such as analytics, marketing, and targeting ads.