Compose部署IPFS星际文件系统以及sdk的使用

Compose部署IPFS星际文件系统

注意

  1. docker和docker-compose安装就不用多说了。
  2. 虚拟机的话注意防火墙开放端口。云服务器需要在安全组添加端口。

compose-ipfs.yaml

version: "2"

services:
  ipfs:
    image: ipfs/go-ipfs:latest
    container_name: ipfs
    restart: always
    volumes:
    # Pay attention to synchronization time
    # echo 'Asia/Shanghai' > /etc/timezone/timezone
    - /etc/timezone/timezone:/etc/timezone
    - /etc/localtime:/etc/localtime
    - /home/ipfs/export/:/export
    - /home/ipfs/data/:/data/ipfs
    ports:
    - 4001:4001
    - 0.0.0.0:8818:8080
    # http://ip:5001/webui
    - 0.0.0.0:5001:5001

启动IPFS容器

docker-compose -f compose-ipfs.yaml up -d

Web UI

http://192.168.1.111:5001/webui

解决跨域问题

docker exec ipfs ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]'
docker exec ipfs ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]' 

sdk使用

这里使用的是java版本的sdk,提供了最简单的使用,更多请参考https://github.com/ipfs-shipyard/java-ipfs-http-client/blob/master/src/test/java/io/ipfs/api/APITest.java


    <dependency>
        <groupId>com.github.ipfs</groupId>
        <artifactId>java-ipfs-http-client</artifactId>
        <version>v1.3.3</version>
    </dependency>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

代码示例:

import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import io.ipfs.multihash.Multihash;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author ming
 * @version 1.0.0
 * @date 2020/12/3 18:08
 **/
public class IpfsTest {
    private IPFS ipfs;

    @Before
    public void setUp() {
        ipfs = new IPFS("/ip4/192.168.1.110/tcp/5001");
    }

    @Test
    public void myTest() throws IOException {
        //保存上传文件
        NamedStreamable.FileWrapper saveFile = new NamedStreamable.FileWrapper(new File("E:\\guaranty\\233ade8f1076f5b3f8a4a02def39d1ef.png"));
        MerkleNode result = ipfs.add(saveFile).get(0);
        System.out.println(result.hash);

    }

    @Test
    public void download() throws IOException {
        //下载文件参数为文件 hash
        Multihash filePointer = Multihash.fromBase58("QmcbJuTRYyxWerJrMUJsnEX68CCEoGYbEv5gXNQqavptew");
        byte[] fileContents = ipfs.cat(filePointer);

        //保存文件
        File downloadFile = new File("E:\\guaranty\\test\\233ade8f1076f5b3f8a4a02def39d1ef-test1.png");

        if (!downloadFile.exists()) {
            if (downloadFile.createNewFile()) {
                System.out.println(String.format("文件保存成功: %s", downloadFile.getAbsolutePath()));
            }
        }

        try (FileOutputStream fop = new FileOutputStream(downloadFile)) {
            fop.write(fileContents);
            fop.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
posted @ 2020-12-03 16:35  itwetouch  阅读(679)  评论(0编辑  收藏  举报