Unlock the Power of Decentralized Storage with Spheron:
Step-by-step Guide to using Spheron Storage SDK in Your dApp/App
Spheron, a leading web3 infrastructure company, offers a powerful Storage SDK that enables developers to seamlessly integrate decentralized storage into their applications. By leveraging Spheron's Storage SDK, you can unlock the potential of decentralized storage, ensuring data integrity and enabling efficient access to files.
In this step-by-step guide, we will walk you through the process of integrating and using Spheron's Storage SDK in your dApp or app. Let's dive in!
Prerequisites:
Before we begin, ensure you have the following prerequisites:
Basic understanding of JavaScript and web development.
Node.js is installed on your local machine.
An active Spheron account and API key.
Step 1: Install the Spheron SDK:
To get started, open your terminal or command prompt and create a new directory for your project. Navigate to the project directory and run the following command to initialize a new Node.js project:
npm init -y
This command sets up a new Node.js project with default settings. Next, install the Spheron SDK package by running the following command:
npm install spheron-sdk
This command will download and install the Spheron SDK, which we will use for integrating decentralized storage into our application.
Step 2: Import and Initialize the Spheron SDK:
In your project directory, create a new JavaScript file (e.g., app.js) and open it in your preferred code editor. Add the following line at the top of your file to import the Spheron SDK:
const Spheron = require('spheron-sdk');
This line imports the Spheron SDK, allowing us to access its functionalities. Next, initialize the Spheron SDK with your API key by adding the following code:
const spheron = new Spheron({
apiKey: 'YOUR_API_KEY',
});
Make sure to replace 'YOUR_API_KEY' with the actual API key provided by Spheron. This step establishes the connection between your application and Spheron's decentralized storage infrastructure.
Step 3: Upload a File to Spheron Storage:
Now that we have the Spheron SDK imported and initialized, let's proceed with uploading a file to Spheron's decentralized storage. Add the following code to your JavaScript file:
javascriptCopy codeasync function uploadFileToSpheron(file) {
try {
const storage = spheron.storage();
// Upload the file to Spheron Storage
const uploadedFile = await storage.uploadFile(file);
console.log('File uploaded successfully:', uploadedFile.cid);
} catch (error) {
console.error('Error uploading file to Spheron Storage:', error);
}
}
// Call the upload function with the path to your file
uploadFileToSpheron('path/to/your/file.txt');
In this code snippet, we define an asynchronous function called uploadFileToSpheron
that takes the file path as an argument. Inside the function, we create a storage
object by calling spheron.storage
()
. Then, we use the uploadFile
method provided by the SDK to upload the file to Spheron's decentralized storage. If the upload is successful, we log the Content Identifier (CID) of the uploaded file to the console.
Make sure to replace 'path/to/your/file.txt'
with the actual path to the file you want to upload.
Step 4: Retrieve a File from Spheron Storage:
In addition to uploading files, Spheron's Storage SDK allows us to retrieve files from decentralized storage. Add the following code to your JavaScript file:
async function getFileFromSpheron(cid) {
try {
const storage = spheron.storage();
// Get the file from Spheron Storage
const file = await storage.getFile(cid);
console.log('File retrieved successfully:', file);
} catch (error) {
console.error('Error retrieving file from Spheron Storage:', error);
}
}
// Call the function with the CID of the file you want to retrieve
getFileFromSpheron('YOUR_FILE_CID');
In this code snippet, we define an asynchronous function called getFileFromSpheron
that takes the Content Identifier (CID) as an argument. Inside the function, we create a storage
object using spheron.storage
()
. Then, we use the getFile
method to retrieve the file from Spheron's decentralized storage based on its CID. If the retrieval is successful, we log the file data to the console.
Make sure to replace 'YOUR_FILE_CID'
with the actual CID of the file you want to retrieve.
Step 5: Explore Additional Functionality:
Spheron's Storage SDK offers a range of additional functionalities that you can explore to enhance your dApp or app. These include file deletion, metadata manipulation, and more. Take some time to review the SDK documentation to learn more about these features and how they can further empower your application.
Conclusion:
Integrating and using Spheron's Storage SDK in your dApp or app opens up a world of possibilities for decentralized and secure storage. By following this step-by-step guide, you have learned how to integrate the Spheron SDK into your project, upload files to decentralized storage, and retrieve files based on their CIDs. Explore the full potential of Spheron's Storage SDK.