In this series we will discuss the in chain And off-chain methods of storing file documents on the blockchain.
And later we will learn how to store files like images, PDFs or any other digital assets off-chain using the InterPlanetary File System (IPFS) network powered by Morality And Infura IPFS.
By the end of this tutorial series, you will be able to store and access files on the IPFS network.
Blockchain as a database
As a database, a blockchain is an immutable digital ledger of transactions distributed across multiple computer networks. It allows us to store data, i.e. NFT metadata (including files) and retrieve them the same way as any other database.
File storage on the Blockchain (On-Chain)
On-chain refers to verified activities or transactions that take place directly on the blockchain. In this case, uploading files directly to a blockchain is also an on-chain activity.
However, should the files be stored directly on the chain? Is it a good idea to keep files on chain? And how much does blockchain storage cost?
Cost of storing files on the Blockchain (On-Chain)
The cost of storing large files on the blockchain can be very high. According to IBM’s “Storage Needs for Blockchain Technology – Point of View” on page 9a gigabyte of storage costs around $100 on blockchain, 500 times more expensive than traditional storage.
Performance of file storage on the Blockchain (On-Chain)
In addition to being expensive, storing a large number of files on a blockchain can increase the access latency files (increase the time required to upload/download files from the blockchain).
File storage requires low latency for fast access. However, when there is an increase in latency due to large files, it can slow down the performance of the blockchain system and make maintenance very difficult.
It is not advisable to store non-transactional data such as files, contracts, documents, PDFs and personal information directly on the blockchain, but you should instead consider storing them off-chain.
Storage of files outside the blockchain (off-chain)
The term “off-chain” refers to activities or transactions that take place outside of the blockchain. In this context, an off-chain asset is a file that is not directly uploaded to the blockchain.
Since it is not advisable to store non-transactional data, the file is uploaded to another server or database (IPFS, MongoDB, Oracle, etc.) and the HashID generated for the uploaded document will be stored on the blockchain as metadata.
What is IPFS?
Web3 database 🤯
THE Interplanetary File System (IPFS) is a decentralized storage system. A peer-to-peer (p2p) protocol and network for storing, accessing, and sharing data in a distributed file system like a blockchain.
IPFS is based on content-based identity (CID) or content-based addressing which is created to be faster than the traditional location-based addressing method for saving files.
What is location-based addressing?
HTTP: “Where” the content you want is 🤷♂️
Location-based addressing is the traditional method of accessing content such as photos, music and files on the Internet and requires specifying where the content is hosted by providing an absolute path or address to the content , as shown below :
The limitation of location-based addressing is that it uses a centralized server and the content will be unavailable if the server hosting it goes down.
In a centralized server such as Twitter and Facebook, they independently control how their users' saved content is delivered (i.e. URL) and you can only access a Twitter profile picture via the 'Twitter URL and not the Facebook URL.
What is content-based addressing?
IPFS: “What” content do you want 🤔
In content-based addressing or content-addressed storage (CAS), each downloaded content has its own unique identifier called Chopwhich can be compared to a fingerprint, and two contents cannot share the same hash.
Content such as photos, music, and files are accessed via their unique IPFS hash rather than their location.
Any content uploaded to IPFS is accessible through the supported IPFS public gateway. For example, when a file is uploaded via Moralis IPFS, then we can use the Infura IPFS URL to access the file.
The uploaded content is stored in an IPFS object which is a data structure with two fields, one data which can hold up to 256 KB of blob data, and connectionswhich is an array of links to other IPFS objects.
{
Data: "",
Links: (
{
Name: “”,
Hash: “”,
Size: 256000
}
)
}
When the content size exceeds 256 KB, IPFS will automatically split it into multiple objects and create an empty IPFS to link all the hashes together.
Content stored on IPFS cannot be modified like on the blockchain. Instead, IPFS supports versioning of your content called the IPFS commit object, which is connected to previous version objects.
IPFS was originally created by Juan Benet, the founder of Protocol Labs in February 2015.
Where is IPFS data stored?
Cached folders 🗂
Data stored on IPFS is saved locally in a computer's cache folder and served to others who request it through an IPFS gateway. The data is also cached on the new user's computer.
Cached data is deleted during garbage collection, which can result in data loss. In order to save your data permanently on IPFS, you need to pin it to one of the IPFS networks.
What are the advantages of IPFS?
-
Fast recording recovery.
-
It works with all technologies.
-
Only one instance of content can be stored (no duplicates).
-
The uploaded contents are authentic with their unique identifiers.
-
Downloaded content cannot be edited.
Difference between IPFS and HTTP storage
IPFS (content-based addressing) | HTTP (location-based addressing) |
It is based on a decentralized server | It is based on a centralized origin server |
It uses content addressing | It uses location addressing |
All objects with the same content are only stored once | All objects with the same content can be stored multiple times |
Files are shared across multiple nodes and are always accessible | Files are not accessible when server is down |
It has a high market | Its market is weak |
Immutable (versioning) | Mutable |
Wrap
InterPlanetary File System (IPFS) is a reliable, decentralized storage system. It is also widely considered the future of file storage.
In this article, we learned about the differences between HTTP and IPFS storage, as well as the in chain And off-chain (IPFS) methods of storing files on the blockchain.
Where are you going next?
Now that you know how files are stored on the blockchain and how IPFS works:
This article is part of the Hashnode Web3 Blog, where a team of selected editors offers new resources to help you discover the world of web3. Visit us to learn more about NFTs, DAOs, blockchains and the decentralized future.