[英]How Upload JPEG Image To IPFS - Golang
I am trying to upload an image from inside the code to IPFS.我正在尝试将代码内部的图像上传到 IPFS。 I am running daemon on my machine.我在我的机器上运行守护进程。 Steps: Open the file, Decode it, Convert to bytes, Upload to IPFS using localhost 5001.步骤:打开文件,解码,转换为字节,使用 localhost 5001 上传到 IPFS。
In fact I get the hash/CID QmUi25FVFwzW9bywDeoYbVkfqAqQEdYhz8Scicm1fqjusq
and inserting it on URL like that https://qmui25fvfwzw9bywdeoybvkfqaqqedyhz8scicm1fqjusq.ipfs.dweb.link receive the message: invalid ipfs path: invalid path "/ipfs/qmui25fvfwzw9bywdeoybvkfqaqqedyhz8scicm1fqjusq/": invalid CID: selected encoding not supported (possible lowercased CIDv0; consider converting to a case-agnostic CIDv1, such as base32)
. In fact I get the hash/CID QmUi25FVFwzW9bywDeoYbVkfqAqQEdYhz8Scicm1fqjusq
and inserting it on URL like that https://qmui25fvfwzw9bywdeoybvkfqaqqedyhz8scicm1fqjusq.ipfs.dweb.link receive the message: invalid ipfs path: invalid path "/ipfs/qmui25fvfwzw9bywdeoybvkfqaqqedyhz8scicm1fqjusq/": invalid CID: selected encoding not supported (possible lowercased CIDv0; consider converting to a case-agnostic CIDv1, such as base32)
。
Therefore I used the CID Inspector to generate CID in base32 https://cid.ipfs.io/#QmUi25FVFwzW9bywDeoYbVkfqAqQEdYhz8Scicm1fqjusq .因此,我使用 CID Inspector 在 base32 https://cid.ipfs.io/#QmUi25FVFwzW9bywDeoYbVkfqAqQEdYhz8Scicm1fqjusq中生成 CID。
New URL: https://bafybeic6t543xz7w23xovave7kyysqbnf6wy6cbrmrrygxh2sibd2ahjeq.ipfs.dweb.link .新 URL: https://bafybeic6t543xz7w23xovave7kyysqbnf6wy6cbrmrrygxh2sibd2ahjeq.ipfs.dweb.link 。 Nevertheless I still getting error message: 504 Gateway Time-out openresty
.尽管如此,我仍然收到错误消息: 504 Gateway Time-out openresty
。
Due this results I am considering that may I stored the image wrongly.由于这个结果,我正在考虑我可能错误地存储了图像。 Below my code for reading the image and saving into bytes and after calling it inside the function UploadIPFS
and returning the hash/CID.在我的代码下方,用于读取图像并保存为字节,并在 function UploadIPFS
中调用它并返回哈希/CID。 Imported shell "github.com/ipfs/go-ipfs-api"
was used.使用了进口shell "github.com/ipfs/go-ipfs-api"
。 Anyone could help me?任何人都可以帮助我吗?
func ReadImageBytes(path_image string) []byte {
inputFile, _ := os.Open(path_image)
inputFile.Close()
File, err := os.Open(path_image)
if err != nil {
log.Fatal(err)
}
defer File.Close()
img, err := jpeg.Decode(File)
if err != nil {
log.Fatal(err)
}
sz := img.Bounds()
raw := make([]uint8, (sz.Max.X-sz.Min.X)*(sz.Max.Y-sz.Min.Y)*4)
idx := 0
for y := sz.Min.Y; y < sz.Max.Y; y++ {
for x := sz.Min.X; x < sz.Max.X; x++ {
r, g, b, a := img.At(x, y).RGBA()
raw[idx], raw[idx+1], raw[idx+2], raw[idx+3] = uint8(r), uint8(g), uint8(b), uint8(a)
idx += 4
}
}
return raw
}
func UploadIPFS(raw []byte) (string, error) {
sh := shell.NewShell("localhost:5001")
reader := bytes.NewReader(raw)
fileHash, err := sh.Add(reader)
if err != nil {
return "", err
}
fmt.Println(fileHash)
return fileHash, nil
}
I was also facing this error on my side I was not passing the complete URL that's why I was facing an error maybe this is also on your side我也面临这个错误我没有通过完整的 URL 这就是为什么我遇到错误也许这也在你身边
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.