IPFS入门-HelloWorld

本文由币乎(bihu.com)内容支持计划奖励

目录

  1. 新建一个HelloWorld.html文件
  2. 查看ipfs使用命令
  3. 将HelloWorld.htm添加到ipfs节点
  4. 同步节点文件
  5. 从IPFS网络查看文件

1 新建一个HelloWorld.html文件

新增一个文件夹,在文件夹新建一个HelloWorld.html文件:

image

文件内容是一段简单html:

<html>
<head>
<title>IPFS,Hello World!</title>
</head>
<body>
<p>IPFS,Hello World!</p>
</body>
</html>

2 查看ipfs使用命令

新建一个终端,执行命令ipfs.exe help,可以查到相关使用命令,输入命令如下:

D:\ipfs\go-ipfs>ipfs.exe 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

  Use 'ipfs <command> --help' to learn more about each command.

  ipfs uses a repository in the local file system. By default, the repo is
  located at ~/.ipfs. To change the repo location, set the $IPFS_PATH
  environment variable:

    export IPFS_PATH=/path/to/ipfsrepo

  EXIT STATUS

  The CLI will exit with one of the following values:

  0     Successful execution.
  1     Failed executions.

3 将HelloWorld.html添加到ipfs节点

首先我们将HelloWord.html添加到本地节点,在终端输入一下命令:

D:\ipfs\go-ipfs>ipfs.exe add D:\ipfs\lean\HelloWorld.html
109 B / 109 B [==================================================================================================================================
added QmZ52xEibwKm28x4942KsLAs4njhF3QtkbNcs4pC4LCyBp HelloWorld.html

当执行完ipfs.exe add HelloWorld.html 命令之后,就会将HelloWorld.html 添加到ipfs当前节点中,并且会对HelloWorld.html 文件生产一个唯一的hash:QmSzPqkCCBHmyymof83g8xPZjiZHyVz1UkNuGXUGutFPaK

如果想看本地ipfs节点数据,可以通过输入下面命令:

D:\ipfs\go-ipfs>ipfs.exe cat QmZ52xEibwKm28x4942KsLAs4njhF3QtkbNcs4pC4LCyBp
<html>
<head>
<title>IPFS,Hello World!</title>
</head>
<body>
<p>IPFS,Hello World!</p>
</body>
</html>

4 同步节点文件

虽然文件已经添加到当前自己的IPFS节点中,如果想同步到IPFS网络上,还需要进行同步操作,接下来执行同步命令,将节点文件同步到IPFS网络,输入命令:

D:\ipfs\go-ipfs>ipfs.exe daemon
Initializing daemon...
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/169.254.35.144/tcp/4001
Swarm listening on /ip4/169.254.36.228/tcp/4001
Swarm listening on /ip4/192.168.0.103/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /p2p-circuit/ipfs/QmNaTdLDsVymhfhNxbPVkSN9jAbrZHYWvg9b83j2PLWUcy
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/169.254.35.144/tcp/4001
Swarm announcing /ip4/169.254.36.228/tcp/4001
Swarm announcing /ip4/192.168.0.103/tcp/4001
Swarm announcing /ip4/192.168.1.2/tcp/16143
Swarm announcing /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

5 从IPFS网络查看文件

同步执行完成之后,可以通过公网地址直接访问第4步同步的HelloWorld.html
浏览器输入:

https://ipfs.io/ipfs/QmZ52xEibwKm28x4942KsLAs4njhF3QtkbNcs4pC4LCyBp

image.png

至此已经成功将Hello World发送到IPFS网络上!更多精彩章节后续更新!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录 1. 如何在IPFS新增一个文件1.1 新建file.txt文件1.2 查看ipfs相关命令1.3 将fil...
    rectinajh阅读 2,389评论 1 8
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,519评论 9 467
  • 现在,越来越喜欢独处,不喜欢与人产生牵绊。让我心生杂念。 我不是一个不合群的人,我只是不喜欢你把自己的观点强行灌输...
    至夜阅读 355评论 0 0
  • 查理认为未雨绸缪、富有耐心、律己严厉和不偏不倚是最基本的指导原则。 ”努力去培养和坚持这种方法值得吗?查理是这么想...
    莲花舒梓慧阅读 129评论 0 0
  • 感恩妈妈一早过来给光宝起床,照顾光宝的生活,感恩早上的学习,让自己真正有所成长,感恩早上的美好的天气和空气让我感到...
    日精进_a07d阅读 73评论 0 2