Deploy
Last updated
Was this helpful?
Last updated
Was this helpful?
You need persistent storage for your application, but you don't want a full-blown database.
This guide will show you how to create a persistent storage Data Capsule that you can use with backend applications running on Code Capsules.
A Persistent Storage Capsule works like a virtual hard drive in the cloud. The key benefit is that you can attach multiple Backend or Docker Capsules to the same Persistent Storage Capsule, enabling them to share data and scale horizontally or vertically.
To set up persistent file storage, you'll first create a Persistent Storage Capsule, then bind it to a Backend Capsule.
Log in to your Code Capsules account and navigate to the Space where you want to create a Data Capsule.
Click the yellow + button on the bottom-left corner of the dashboard. Select New Capsule and then Persistent Storage from the dropdown.
Select your payment method, then click Create Capsule.
You need to bind the Storage Capsule to a Backend Capsule hosted on Code Capsules before you can connect to and use it.
Navigate to the Backend Capsule and open the Config tab. Scroll down to the Bind Data Capsule section, where your recently created Storage Capsule is listed.
Click Bind to bind the Capsules.
During the bind process, Code Capsules creates a PERSISTENT_STORAGE_DIR
environment variable that your Backend Capsule will use to access the Storage Capsule.
When the two Capsules are bound, scroll to the top of the Config tab to view the value of the variable.
Now you can use this environment variable in your code to read and write to the Storage Capsule. Copy the value of the PERSISTENT_STORAGE_DIR
variable and assign it to the db_directory
variable. Alternatively, reference it directly using os.getenv
in Python or process.env
in Node.js.
If you're using Python in your Backend Capsule, use the following code to connect to the Storage Capsule:
If you're using Node.js in your Backend Capsule, use the following code to connect to the Storage Capsule:
By default, you can't browse files and folders in your Persistent Storage Capsule without building your own file browser. To solve this, Code Capsules provides native WebDAV access.
To enable file browsing:
Navigate to your Persistent Storage Capsule
Go to the Details tab
Toggle Public Access to Enabled
Enabling this setting doesn't make your files publicly accessible on the internet. They are served over the WebDAV protocol and require authentication credentials to access.
Open Finder
Click on Go in the menu bar and select Connect to Server
Enter the WebDAV server URL in the following format: https://server-address
Click Connect
Enter the username and password when prompted
The WebDAV server will now appear as a mounted drive in Finder.
Here you can see the files inside a WordPress Capsule accessed locally through Finder in macOS.
Open File Explorer
Right click on This PC in the sidebar
Click Add a network location
Click Next in the wizard until prompted to enter the Internet or network address
Enter the WebDAV server URL in the following format: https://server-address
Enter the username and password when prompted
Complete the setup wizard
The WebDAV server will now appear as a mapped drive in File Explorer.
Open your file manager (for example, Nautilus, Dolphin, or Thunar)
In your address bar, enter the WebDAV server URL in the following format:
davs://server-address
for Nautilus and Thunar
webdavs://@server-address
for Dolphin
Enter the username and password when prompted
The WebDAV server will now appear as a drive in your file manager.