I want to create smart contract which can upload and download file from blockchain. Is that possible? If so, how?
[Ethereum] How to upload and download files to the blockchain
blockchaincontract-designstorage
Related Solutions
Summary
Q: How can I specify an external harddrive as the download target for the Mist blockchain?
A: The answer for the Mac version is in Method 1 in In Mist browser how can I move the data (blockchain) to another drive on Mac OSX?. The Linux version is below.
Q: Even if the blockchain data gets corrupted somehow, it won't affect my wallets, right? I can keep them in a folder on my computer instead of the external harddrive, yes or no?
A: Yes. Just make sure that you backup ~/.ethereum/keystore
.
Q: I. e. I would prefer having the blockchain data on an external drive and the wallet files on my laptop harddrive. Possible or not?
A: Yes.
The Details
The default subdirectory where your Ethereum data files are stored on Linux is ~/.ethereum
.
Within this subdirectory, your blockchain data is stored in the subdirectory chaindata
, i.e., ~/.ethereum/chaindata
.
Let's assume that your new drive is mounted on /mnt/extdrive1
for this example.
Copy your ~/.ethereum/chaindata
subdirectory into your new location /mnt/extdrive1/Ethereum/chaindata
using the following command:
mkdir /mnt/extdrive1/Ethereum
cp -rpv ~/.ethereum/chaindata /mnt/extdrive1/Ethereum/
A copy of your Ethereum chaindata
subdirectory will now be located in /mnt/extdrive1/Ethereum/chaindata
.
Rename your default Ethereum chaindata
subdirectory for testing:
mv ~/.ethereum/chaindata ~/.ethereum/OldChaindata_to_delete_when_tested
Soft link your new chaindata
subdirectory back to the default location:
ln -s /mnt/extdrive1/Ethereum/chaindata ~/.ethereum/chaindata
Test out your Mist browser and delete your old chaindata directory ONLY AFTER your testing is successful:
rm -rf ~/.ethereum/OldChaindata_to_delete_when_tested
References
No, you will have to store your data somewhere else and store the hash of this data and its location in the blockchain. You should have enough space to store it but remember the shorter your data is the less gas you need. So use URL shorteners.
Storing data into a transaction costs gas an blocks have a dynamic gas limit. You would have to make the gas limit raise by sending bigger and bigger data to force the network to adapt. But on each transaction you would be limited to a fixed amount of gas and thus a fixed amount of data. On feb 2016 the gas limit was about 3 millions, meaning a block was limited to about 89Kb of data. (see Is there a limit for transaction size? ) :
the transaction size limit currently is at around 89kB (3 million gas). But read on.
There is no direct or fixed limit neither for transaction sizes nor for block sizes. This is a strength of the Etherem network, it does scale.
That does not mean that there are no limits. There is the block gas limit of currently 3,141,592 gas which can be spent maximum per block.
As of writing, gas limit is about 4712388 gas which should limit data to about 140Kb. See https://ethstats.net/
Soon we expect some decentralized secure storage like swarm or IPFS to be easily usable. Look at https://github.com/ethereum/go-ethereum/wiki/Swarm---distributed-preimage-archive where you will also find a list of alternatives at the end of the page.
Best Answer
So files, in the traditional sense that I assume you are talking about when you use the word "files", are not stored on the blockchain; data is. All data is stored in HEX format and the more data there is, the more gas it needs to run. For this reason it's not really efficient or cost-effective to store "files" on the blockchain.
Instead, it's recommended to store files via IPFS or another decentralized / p2p / etc. system. If you want to learn more about referencing data stored in one contract from another contract, please check out the following:
Interactions Between Contracts
and the followup question:
How to reference coin contract already onchain?