IPFS的相关操作命令

新增文件

  • 在桌面新建名字为1121的文件夹,在文件夹里面新建file.txt文件,在文件里面输入数据,保存退出

$ pwd
/Users/CHY/Desktop
$ mkdir 1121
$ cd 1121/
$ vi file.txt
$ cat file.txt 
哈哈,为什么只有我不快乐
  • 给文件输入内容的时候,使用i,开始输入

  • 输入完成之后,按下Exc退出编辑,在同时按shift+冒号,输入wq保存修改

查看ipfs帮助命令

$ ipfs help
USAGE
  ipfs - Global p2p merkle-dag filesystem.

  ipfs [--config=<config> | -c] [--debug=<debug> | -D] [--help=<help>] [-h=<h>] [--local=<local> | -L] [--api=<api>] <command> ...

SUBCOMMANDS
  BASIC COMMANDS
    init          Initialize ipfs local configuration
    add <path>    Add a file to IPFS
    cat <ref>     Show IPFS object data
    get <ref>     Download IPFS objects
    ls <ref>      List links from an object
    refs <ref>    List hashes of links from an object

  DATA STRUCTURE COMMANDS
    block         Interact with raw blocks in the datastore
    object        Interact with raw dag nodes
    files         Interact with objects as if they were a unix filesystem
    dag           Interact with IPLD documents (experimental)

  ADVANCED COMMANDS
    daemon        Start a long-running daemon process
    mount         Mount an IPFS read-only mountpoint
    resolve       Resolve any type of name
    name          Publish and resolve IPNS names
    key           Create and list IPNS name keypairs
    dns           Resolve DNS links
    pin           Pin objects to local storage
    repo          Manipulate the IPFS repository
    stats         Various operational stats
    p2p           Libp2p stream mounting
    filestore     Manage the filestore (experimental)

  NETWORK COMMANDS
    id            Show info about IPFS peers
    bootstrap     Add or remove bootstrap peers
    swarm         Manage connections to the p2p network
    dht           Query the DHT for values or peers
    ping          Measure the latency of a connection
    diag          Print diagnostics

  TOOL COMMANDS
    config        Manage configuration
    version       Show ipfs version information
    update        Download and apply go-ipfs updates
    commands      List all available commands

将文件添加到IPFS节点

$ ipfs add file.txt 
added QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T file.txt
$ cat file.txt 
哈哈,具体问题具体分析
$ ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T
哈哈,具体问题具体分析
  • 当执行完ipfs add file.txt这个命令以后,会将file.txt添加到ipfs当前的节点中,并且会对file.txt文件生成一个唯一的hash``QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T,如果想查看本地ipfs节点的数据,可以通过ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T进行查看。

  • ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T和命令ipfs cat file.txt的内容是一致的

  • 但是通过http://ipfs.io/ipfs/QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T进行数据访问时,无法访问,因为这个数据存储在我们本地,只有将其发布到IPFS的公有集群上才可以访问。

同步节点

  • 使用命令ipfs daemon

$ ipfs daemon
Initializing daemon...
Adjusting current ulimit to 2048...
Successfully raised file descriptor limit to 2048.
Swarm listening on /ip4/111.196.246.151/tcp/3637
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/169.254.170.167/tcp/4001
Swarm listening on /ip4/192.168.0.107/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready

创建目录存储文件

  • 使用命令ipfs cat QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T liyc1215查看添加到ipfs网络的file.txt文件的内容
  • 也可以通过ipfs的相关命令在ipfs的根目录下面创建文件夹,并且将file.txt文件移动或者拷贝到我们创建的文件夹中。

  • cp不会改变文件hash,mv会改变hash寻址

  • ipfs files mkdir /1122 新建的1122文件夹前面必须有 /


$ ipfs files mkdir /1122
$ ipfs files cp /ipfs/QmbrevseVQKf1vsYMsxCscRf6D7S2dftYpHwxkYf94pc7T /1122/file.txt
$ ipfs files ls /
1122
$ ipfs files ls /1122/
file.txt
$ ipfs files read /1122/file.txt
赫赫

新增一个目录

使用ipfs add -r可以上传一整个目录

$ ipfs add -r ipfs-tutorial/
added QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc ipfs-tutorial/contactme.txt
added QmfKdWsguobA3aDPvSxLB3Bq4HMKyqKSgFr2NFUuVH8n31 ipfs-tutorial/eth-fabric.png
added QmXe8jTxTh5MZP6BK5cnj19mXNTKVMzNyUJZUHuYyr5dk1 ipfs-tutorial/gongzhonghao.png
added QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek ipfs-tutorial
  • QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek是整体的哈希

  • 文件名前的哈希是他们自己各自的哈希值

通过路径访问contactme.txt文件数据

  • 如果我们上传的是目录,那么可以通过下面几种方式访问到contactme.txt文件的数据。
$ ipfs cat QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc

$ ipfs cat /ipfs/QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc

$ ipfs cat /ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek/contactme.txt

通过Hash查看数据IPFS网络数据

访问目录:https://ipfs.io/ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek

通过目录访问文件:https://ipfs.io/ipfs/QmSsjQDVw1fvmG5RsZMgp2GjihiXn2zDv64mfHZN3AREek/contactme.txt

通过文件hash直接访问:https://ipfs.io/ipfs/QmYx4BnhnLXeMWF5mKu16fJgUBiVP7ECXh7qcsUZnXiRxc

 

发布了76 篇原创文章 · 获赞 2 · 访问量 3781

猜你喜欢

转载自blog.csdn.net/CHYabc123456hh/article/details/105439360