currently i am learning ipfs and ethereum based blockchain technology,I wanted to know can we store data on ethereum blockchain and yes then how. and also wanted to know how exactly ipfs work for blockchain.
As an addition to the answer of Tudor Constantin:
IPFS is pretty much a decentralized P2P Filesystem.
Here is a quick summary of how IPFS basically works:
- If you add a file you receive the corresponding hash of that file which is also the unique reference to it
- The file is then stored on different Nodes running IPFS
- Everyone who knows the hash of the file will be able to view/fetch it. If the data is sensitive you need to add somekind of encryption
- If you add the same file multiple times it will only be stored as one entry because the hash will be always the same. If you however change the file and read it you will obviously receive a new reference since the hash changes
This is a really neat way to store data in a decentralized manner because you can just put all your payload (for example large JSON-Files with your applications data) in IPFS and only store the reference to that file on the blockchain instead of putting all the data into the blockchain which is (as Tudor already stated) quite expensive.
So to summarize:
To safe data:
- post your payload to IPFS and receive the reference to your file
- store the reference in your smart contract on the blockchain
To fetch your data:
- fetch the reference from your smart contract
- fetch your data from IPFS
Use this as a pattern:
IPFS stores the actual content and Ethereum stores the immutable history of references.
Yes, we can store any data we want on the the blockchain. It's expensive to do so, but it's possible. That's why it's recommended to store just the minimum on the blockchain and the rest using another form of storage (decentralized or not), like (as you mentioned) IPFS, or Ethereum's Swarm.
This looks like a nice tutorial for working with IPFS from a dApp.
You can think of IPFS/swarm as the storage layer of your dApp, where you can host your .js/html/css files and the user uploaded content, while the smart contract code as the business logic layer of your dApp.