Peristent Storage
Last updated
Was this helpful?
Last updated
Was this helpful?
You need persistent storage to develop your application that solves a real-world problem, but you don't want a full-blown database. In this tutorial, we'll show you how to create a persistent storage Data Capsule that you can use with your backend applications running on Code Capsules.
A Persistent Storage Capsule is like a virtual cloud hard drive. The key benefit is that you can attach multiple backend or docker capsules (workloads) to read/write to the same persistent storage capsule, allowing for horizontal and vertical scaling of your backend/docker capsules/workloads.
Log in to your Code Capsules account and navigate to the Space your Data Capsule will be contained in. Click the yellow +
button on the bottom left of the screen, select "New Capsule", then select "Persistent Storage" option from the dropdown.
Choose your payment option, then click the "Create Capsule" button.
You need to bind the Data Capsule to a Backend Capsule hosted on Code Capsules before you can connect to it and use it.
Navigate to the Backend Capsule and click "Config" to open the Capsule's config tab. Scroll down to the "Bind Data Capsule" section, where your recently created Data Capsule will show.
Click "Bind" to bind your Data and Backend Capsules. During the bind process, Code Capsules creates a PERSISTENT_STORAGE_DIR
environment variable to let your Backend Capsule know where your Data Capsule resides in order to access its features. Once the two Capsules have been bound, you can scroll to the top of the Configure tab to find the value of this variable.
The next step is to use this environment variable in code in order to read and write to our Data Capsule. Copy the value of the PERSISTENT_STORAGE_DIR
variable and paste it in your code as the value of the db_directory
variable. Alternatively, reference it directly in your code using os.getenv
for Python or process.env
for Node.js.
If your Backend Capsule is a Python application, use the following code to connect to your Data Capsule:
If your Backend Capsule is a Node.js application, use the following code to connect to your Data 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"
This doesn't mean your files are indexed on the internet - it exposes them over WebDAV protocol with authentication credentials.
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 in the Mac Finder locally.
Open File Explorer
Right-click on "This PC" in the sidebar
Click on "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 (e.g., Nautilus, Dolphin, 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