本次实验,在centos虚拟机上搭建三个IPFS私有节点
名称 | IP |
---|---|
Peer0 | 192.168.184.150 |
Peer1 | 192.168.184.151 |
Peer2 | 192.168.184.152 |
# 单节点IPFS
go get -u github.com/ipfs/ipfs-update
ipfs-update install latest
ipfs init # 创建一个文件夹 cd /root/.ipfs
ipfs daemon # 启动IPFS
ipfs daemon & #启动IPFS,并且使他后台运行
ipfs shutdown #关闭IPFS
# 获取节点之间的私有密钥
go get -u github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen
ipfs-swarm-key-gen > ~/.ipfs/swarm.key
# 发送到其他节点
scp ./swarm.key root@192.168.184.151:/root/.ipfs
scp ./swarm.key root@192.168.184.152:/root/.ipfs
# 每个节点操作
#1. 移除连接到全球IPFS网络的bootstrap
ipfs bootstrap rm --all
#2. 修改配置文件config
vim /root/.ipfs/config
#修改其中的API和Gateway,修改成本机节点
#3. 向每个节点中配置其他网络的bootstrap
ipfs id #查找本机的bootstrap,注意必须是在节点启动的情况下
ipfs bootstrap add "/ip4/192.168.184.150/tcp/4001/ipfs/12D3KooWRT6cW26hyd89yNXnt85quyycpxHoVV5RLNfkjFjfNaQV"
ipfs bootstrap add "/ip4/192.168.184.151/tcp/4001/p2p/12D3KooWKMrBQ96SzdEpbbVA8drgaLSR8LDrvUw3B4AYLRpsTtyG"
ipfs bootstrap add "/ip4/192.168.184.152/tcp/4001/p2p/12D3KooWEtBTjr4UgDzy2ahEiZaxH45qciiHCzFa4QoJ1Fimzfsv"
ipfs add <文件路径>
ipfs get <哈希>
ipfs cat <哈希>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.ipfs</groupId>
<artifactId>java-ipfs-http-client</artifactId>
<version>$LATEST_VERSION</version>
</dependency>
</dependencies>
import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import io.ipfs.multihash.Multihash;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class IPFSOpe {
IPFS ipfs;
public void IPFSOpe(String ip) {
this.ipfs = new IPFS(ip);
}
public void upload(String upaddr) throws IOException {
//保存上传文件
NamedStreamable.FileWrapper savefile = new NamedStreamable.FileWrapper(new File(upaddr));
MerkleNode result = ipfs.add(savefile).get(0);
System.out.println(result.toString());
}
public void cat(String hash) throws IOException {
Multihash filePointer = Multihash.fromBase58(hash);//参数为文件 hash
byte[] fileContents = ipfs.cat(filePointer);
System.out.println(new String(fileContents));
}
public void download(String hash, String downaddr) throws IOException {
Multihash filePointer = Multihash.fromBase58(hash);//参数为文件 hash
byte[] fileContents = ipfs.cat(filePointer);
File downloadfile = new File(downaddr);
if(!downloadfile.exists()) {
downloadfile.createNewFile();
}
FileOutputStream fop = new FileOutputStream(downloadfile);
fop.write(fileContents);
fop.flush();
fop.close();
}
public static void main(String[] args) throws IOException {
String ip = "/ip4/192.168.184.150/tcp/5001";
String upaddr = "C:/Users/Administrator/Desktop/java.txt";
String downaddr = "C:/Users/Administrator/Desktop/毕设实验/IPFS-File/test.txt";
String hash = "QmZ9qHDKxtepTGRi8HstwG1e3TBwZVQdrBQPj628V5hK7X"; //test.txt
//String hash = "QmZcyqZJN2aio4Zc1W5rcRV6gB9xZbsD97UKonta1LXwcL"; //java.txt
IPFSOpe test = new IPFSOpe();
test.IPFSOpe(ip);
//test.upload(upaddr);
//test.cat(hash);
test.download(hash, downaddr);
}
}
原文:https://www.cnblogs.com/lyon-liu/p/14848958.html