Add a file to IPFs
New Test.txt file
Open the terminal, switch to the desktop, create a new folder test, switch to test, create a new file through VI test.txt, file input Hello World save and exit.
$ cd Desktop/$ mkdir test$ cd test/$ vi test.txt$ cat test.txtHello World
Adding Test.txt to the IPFs node
ipfs addAdd with command:
$ cd Desktop/test$ ipfs add test.txtadded QmUXTtySmd7LD4p6RG6rZW6RuUuPZXTtNMmRQ6DSQo3aMw test.txt$ ipfs cat QmUXTtySmd7LD4p6RG6rZW6RuUuPZXTtNMmRQ6DSQo3aMwHello World
- The execution
ipfs add test.txt command is test.txt added to the ipfs current node;
- Once added to the node,
test.txt a unique file is generated hash QmUXTtySmd7LD4p6RG6rZW6RuUuPZXTtNMmRQ6DSQo3aMw ;
ipfs cat QmUXTtySmd7LD4p6RG6rZW6RuUuPZXTtNMmRQ6DSQo3aMwview by command.️: The data has been added to the current local ipfs node, but is not synchronized to the ipfs network, so it needs to be synchronized to the ipfs network.
Synchronize the nodes, create a new terminal, and execute the ipfs daemon command.
$ ipfs daemonInitializing daemon...Successfully raised file descriptor limit to 2048.Swarm listening on /ip4/127.0.0.1/tcp/4001Swarm listening on /ip4/192.168.17.52/tcp/4001Swarm listening on /ip6/::1/tcp/4001Swarm listening on /p2p-circuit/ipfs/QmZXXApnUUTD75bph88eNii3Sc97fiumTX7AMBv5CKHGdfSwarm announcing /ip4/127.0.0.1/tcp/4001Swarm announcing /ip4/192.168.17.52/tcp/4001Swarm announcing /ip6/::1/tcp/4001API server listening on /ip4/127.0.0.1/tcp/5001Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080Daemon is ready
- The browser accesses the https://ipfs.io/ipfs/QmUXTtySmd7LD4p6RG6rZW6RuUuPZXTtNMmRQ6DSQo3aMw, displaying the data as
Hello World , indicating that it has been synced to ipfs网络 .
Creating a directory from IPFs
The ipfs files relevant commands are created under the ipfs root directory, and the files are test.txt moved or copied to the folder we created.
$ ipfs files mkdir /Jisen$ ipfs files cp /ipfs/QmUXTtySmd7LD4p6RG6rZW6RuUuPZXTtNMmRQ6DSQo3aMw /Jisen/test.txt$ ipfs files lsJisen$ ipfs files ls /Jisentest.txt$ ipfs files read /Jisen/test.txtHello World
️: cp does not change the file hash, mv will change the hash address.
Add a new directory to IPFs
Use ipfs add -r to upload an entire directory
$ ipfs add -r ipfs-testadded QmRkk3DQWpJ6A98NJr75fXLeX6GT3Hk3Mmqir6kRWvb3ZP ipfs-test/cryptowallet.pngadded QmZbi1n5wBUx1mx8azhuhBpEq2fewFexrHmRXLeWHofZ7h ipfs-test/dribble_ill.pngadded QmRNFmmrD9yFMn9EP4AiUZmqj9nnmrLQz9q3qHo2u9w7tT ipfs-test/ipfs_test.txtadded QmabKwXZfpT8uZvJRKKW7fHN3Rg5ZjuRMPFnSc1VzMQ7ao ipfs-test
Accessing file data through a path ipfs_test.txt
$ ipfs cat QmRNFmmrD9yFMn9EP4AiUZmqj9nnmrLQz9q3qHo2u9w7tTWelcome IPFS!$ ipfs cat /ipfs/QmRNFmmrD9yFMn9EP4AiUZmqj9nnmrLQz9q3qHo2u9w7tTWelcome IPFS!$ ipfs cat /ipfs/QmabKwXZfpT8uZvJRKKW7fHN3Rg5ZjuRMPFnSc1VzMQ7ao/ipfs_test.txtWelcome IPFS!
View data by hash IPFs network data
- Access directory: Https://ipfs.io/ipfs/QmabKwXZfpT8uZvJRKKW7fHN3Rg5ZjuRMPFnSc1VzMQ7ao
- Access files by directory: Https://ipfs.io/ipfs/QmabKwXZfpT8uZvJRKKW7fHN3Rg5ZjuRMPFnSc1VzMQ7ao/ipfs_test.txt
- Direct access via file hash: Https://ipfs.io/ipfs/QmRNFmmrD9yFMn9EP4AiUZmqj9nnmrLQz9q3qHo2u9w7tT
Publish simple Web page to IPFs
Create a new site folder, and then follow the steps below to site create index.html and file in the folder style.css .
Create a index.html file
<!DOCTYPE html>
Create a Style.css file
h1 { color: green;}
Add to IPFs
$ ipfs add -r site/added QmS4a85WLxie6Zy6Tntg6CccyGHyV4823Vo1vkjGBYhetP site/index.htmladded QmZL2UBTwnhcLv66fARL9UV8W8a9ZA4iwTLcaUCsB1u1yW site/style.cssadded QmPAygbESghBjPbLhGJAda5rA9GCBMHMGfgwLoB61DfnxX site
Network synchronization
$ ipfs daemon
- Visit Website: Https://ipfs.io/ipfs/QmPAygbESghBjPbLhGJAda5rA9GCBMHMGfgwLoB61DfnxX
Publish to Ipns
When the content of the modified Web page is added back, it ipfs hash will change, we can publish the webpage to IPNS , in Ipns, allow us to refer to the domain name space of the node IPFS hash , that is, we can pass 节点ID to the project root directory To IPFS HASH bind, we will visit the Web page directly through 节点ID access, when we update the page, re-publish to the IPNS .
$ ipfs name publish QmPAygbESghBjPbLhGJAda5rA9GCBMHMGfgwLoB61DfnxXPublished to QmZXXApnUUTD75bph88eNii3Sc97fiumTX7AMBv5CKHGdf: /ipfs/QmPAygbESghBjPbLhGJAda5rA9GCBMHMGfgwLoB61DfnxX$ ipfs id{ "ID": "QmZXXApnUUTD75bph88eNii3Sc97fiumTX7AMBv5CKHGdf" ......}
When we execute the ipfs name publish command, we return it 节点ID , and you can ipfs id check to see if it is your node ID.
️: Verify
$ ipfs name resolve QmZXXApnUUTD75bph88eNii3Sc97fiumTX7AMBv5CKHGdf/ipfs/QmPAygbESghBjPbLhGJAda5rA9GCBMHMGfgwLoB61DfnxX
Next, you can access it by IPNS : HTTPS://IPFS.IO/IPNS/QMZXXAPNUUTD75BPH88ENII3SC97FIUMTX7AMBV5CKHGDF
️: When visiting, note that the link above is ipns not ipfs .
Finally, ️ is required: If the site data is modified, it needs to be republished to Ipns.