如何通过HTTP API将文件或文件夹写入IPFS?

huangapple go评论106阅读模式
英文:

How to Write Files or Folder to IPFS via the HTTP API

问题

我对IPFS的HTTP API文档感到困惑。以下是其中的一部分。

/api/v0/add
将文件或目录添加到IPFS。
//但是如何使用golang添加目录呢?看起来很简单,但没有一个示例来完成它

#cURL示例
curl -X POST -F file=@myfile "http://127.0.0.1:5001/api/v0/add?quiet=&quieter=&silent=&progress=&trickle=&only-hash=&wrap-with-directory=&chunker=size-262144&pin=true&raw-leaves=&nocopy=&fscache=&cid-version=&hash=sha2-256&inline=&inline-limit=32"

英文:

I'm confused about the HTTP API docs of IPFS。next is part of it。

/api/v0/add
Add a file or directory to IPFS.
//but how to add a directory by golang? it look like so simple but no a example to finish it

#cURL Example
curl -X POST -F file=@myfile "http://127.0.0.1:5001/api/v0/add?quiet=<value>&quieter=<value>&silent=<value>&progress=<value>&trickle=<value>&only-hash=<value>&wrap-with-directory=<value>&chunker=size-262144&pin=true&raw-leaves=<value>&nocopy=<value>&fscache=<value>&cid-version=<value>&hash=sha2-256&inline=<value>&inline-limit=32"

答案1

得分: 1

我在同一个问题上工作过,并找到了这个可行的Shell解决方案:

https://community.infura.io/t/ipfs-http-api-add-directory/189/8

你可以用Go重新构建这个解决方案

package main

import (
	"bytes"
	"github.com/stretchr/testify/assert"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
	"strings"
	"testing"
)

func TestUploadFolderRaw(t *testing.T) {
	ct, r, err := createForm(map[string]string{
		"/file1": "@/my/path/file1",
		"/dir":   "@/my/path/dir",
		"/dir/file": "@/my/path/dir/file",
	})
	assert.NoError(t, err)

	resp, err := http.Post("http://localhost:5001/api/v0/add?pin=true&recursive=true&wrap-with-directory=true", ct, r)
	assert.NoError(t, err)

	respAsBytes, err := ioutil.ReadAll(resp.Body)
	assert.NoError(t, err)

	t.Log(string(respAsBytes))
}

func createForm(form map[string]string) (string, io.Reader, error) {
	body := new(bytes.Buffer)
	mp := multipart.NewWriter(body)
	defer mp.Close()
	for key, val := range form {
		if strings.HasPrefix(val, "@") {
			val = val[1:]
			file, err := os.Open(val)
			if err != nil { return "", nil, err }
			defer file.Close()
			part, err := mp.CreateFormFile(key, val)
			if err != nil { return "", nil, err }
			io.Copy(part, file)
		} else {
			mp.WriteField(key, val)
		}
	}
	return mp.FormDataContentType(), body, nil
}

或者使用https://github.com/ipfs/go-ipfs-http-client,这似乎是一个更好的方法。我正在研究它,等我知道如何使用它时会告诉你。

问候

英文:

I worked on the same issue and found this working shell solution:

https://community.infura.io/t/ipfs-http-api-add-directory/189/8

you can rebuild this in go

package main

import (
	&quot;bytes&quot;
	&quot;github.com/stretchr/testify/assert&quot;
	&quot;io&quot;
	&quot;io/ioutil&quot;
	&quot;mime/multipart&quot;
	&quot;net/http&quot;
	&quot;os&quot;
	&quot;strings&quot;
	&quot;testing&quot;
)

func TestUploadFolderRaw(t *testing.T) {
	ct, r, err := createForm(map[string]string{
		&quot;/file1&quot;: &quot;@/my/path/file1&quot;,
		&quot;/dir&quot;: &quot;@/my/path/dir&quot;,
		&quot;/dir/file&quot;: &quot;@/my/path/dir/file&quot;,
	})
	assert.NoError(t, err)

	resp, err := http.Post(&quot;http://localhost:5001/api/v0/add?pin=true&amp;recursive=true&amp;wrap-with-directory=true&quot;, ct, r)
	assert.NoError(t, err)

	respAsBytes, err := ioutil.ReadAll(resp.Body)
	assert.NoError(t, err)

	t.Log(string(respAsBytes))
}

func createForm(form map[string]string) (string, io.Reader, error) {
	body := new(bytes.Buffer)
	mp := multipart.NewWriter(body)
	defer mp.Close()
	for key, val := range form {
		if strings.HasPrefix(val, &quot;@&quot;) {
			val = val[1:]
			file, err := os.Open(val)
			if err != nil { return &quot;&quot;, nil, err }
			defer file.Close()
			part, err := mp.CreateFormFile(key, val)
			if err != nil { return &quot;&quot;, nil, err }
			io.Copy(part, file)
		} else {
			mp.WriteField(key, val)
		}
	}
	return mp.FormDataContentType(), body, nil
}

or use https://github.com/ipfs/go-ipfs-http-client which seems to be a better way. I'm working on it and tell you when I know how to use it

Greetings

huangapple
  • 本文由 发表于 2021年8月10日 21:17:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/68727572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定