Go语言访问IPFS分布式文件客户端的API-未完待续

作为备份,存储一下,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

import (
"context"
"fmt"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/client/rpc"
ma "github.com/multiformats/go-multiaddr"
"net/http"
"os"
)

func main() {
// "Connect" to local node
address, err := ma.NewMultiaddr("/ip4/192.168.123.6/tcp/4001")
if err != nil {
fmt.Println(err)
}

node, err := rpc.NewApiWithClient(address, &http.Client{})
//node, err := rpc.NewLocalApi()
if err != nil {
fmt.Println(err)
return
}
// Pin a given file by its CID
ctx := context.Background()
cid := "/ipfs/QmepEzGBkDQ7a8xCTVYZvuZL2C7FUxaaC75FSY8pGb1v7b"
p, err := path.NewPath(cid)
if err != nil {
fmt.Println(err)
}
//p := path.New(cid)
data, err := node.Object().Get(ctx, p)

if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(data.RawData()))
return
}

func getFile() {

}

func putFile() {
// "Connect" to local node
node, err := rpc.NewLocalApi()
if err != nil {
fmt.Println(err)
return
}
// Pin a given file by its CID
ctx := context.Background()

f, _ := os.Open("/Users/qxdo/Projects/make_order/test.html")
fmt.Println(node.Unixfs().Add(ctx, files.NewReaderFile(f)))
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main

import (
"context"
"fmt"
"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/client/rpc"
//path "github.com/ipfs/boxo/coreiface/path"
)

func main() {
// "Connect" to local node
node, err := rpc.NewLocalApi()
if err != nil {
fmt.Println(err)
return
}
// Pin a given file by its CID
ctx := context.Background()
cid := "/ipfs/QmcmaL2fiXR6BLJiVixtCsX46xydTTjYdQr3DB2D7ecBQT"
p, err := path.NewPath(cid)
if err != nil {
fmt.Println(err)
}
//p := path.New(cid)
data, err := node.Object().Get(ctx, p)

if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(data.RawData()))
return
}

一个简易HTML 网页文件管理的前端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>文件列表页</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
width: 80%;
margin: 20px auto;
}
.file-list {
list-style-type: none;
padding: 0;
}
.file-list-item {
padding: 10px;
border-bottom: 1px solid #ddd;
}
.file-list-item:hover {
background-color: #f5f5f5;
}
.button-group {
margin-bottom: 20px;
}
.button {
padding: 10px 20px;
margin-right: 10px;
background-color: #5cb85c;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.button:hover {
background-color: #4cae4c;
}


body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
width: 80%;
margin: 20px auto;
}
.file-list {
list-style-type: none;
padding: 0;
}
.file-list-item {
padding: 10px;
border-bottom: 1px solid #ddd;
}
.file-list-item:hover {
background-color: #f5f5f5;
}
.button-group {
margin-bottom: 20px;
text-align: right;
}
.button {
padding: 10px 20px;
margin-left: 10px;
background-color: #5cb85c;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.button:hover {
background-color: #4cae4c;
}
/* 添加文件上传样式 */
.upload-area {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
border: 1px solid #ddd;
padding: 20px;
border-radius: 4px;
z-index: 1000;
}
.close-btn {
float: right;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<div class="button-group">
<button class="button" onclick="toggleUploadArea()">上传文件</button>
<button class="button">新建文件夹</button>
</div>
<ul class="file-list">
<!-- 文件列表项 -->
</ul>
</div>
<!-- 文件上传区域 -->
<div class="upload-area" id="uploadArea">
<span class="close-btn" onclick="toggleUploadArea()">&times;</span>
<h3>选择文件上传</h3>
<form id="uploadForm">
<input type="file" id="fileInput" multiple>
<button type="submit" class="button">开始上传</button>
</form>
</div>
<script>
// 切换上传区域的显示和隐藏
function toggleUploadArea() {
var uploadArea = document.getElementById('uploadArea');
uploadArea.style.display = uploadArea.style.display === 'none' ? 'block' : 'none';
}
// 处理文件上传
document.getElementById('uploadForm').addEventListener('submit', function(event) {
event.preventDefault();
var files = document.getElementById('fileInput').files;
// 这里可以添加处理文件上传的代码,例如通过XMLHttpRequest发送文件到服务器
alert('文件选择成功,可以开始上传了。');
// 关闭上传区域
toggleUploadArea();
});
// 关闭上传区域
document.getElementById('uploadArea').addEventListener('click', function(event) {
if (event.target === event.currentTarget) {
toggleUploadArea();
}
});
</script>
</body>
</html>