简体   繁体   English

如何将 JPEG 图像上传到 IPFS - Golang

[英]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.

相关问题 如何在当前工作中保存图像并使用 docker 在 golang 中加载图像 - how to save the image in current working and load the image in golang using docker 使用 swift 将图像保存为 image/jpeg 到 firebase - Save image to firebase as image/jpeg using swift Amazon SageMaker 不支持的媒体类型:图像/jpeg - Amazon SageMaker Unsupported Media Type: image/jpeg 使用 golang 中的自定义字段分段上传到 S3 - Multipart upload to S3 with custom fields in golang Golang - 创建 docker 图像的问题 - Golang - problem with creating docker image 如何构建一个最小的“无发行版”镜像来运行一个运行 java 子服务的 golang 服务? - How to construct a minimal "distroless" image to run a golang service, which runs a java subservice? 如何使用 swift 在 firebase 上上传多张图片? - How to upload multiple image on firebase using swift? 如何通过 react native(expo) 将图像上传到 azure blob 存储? (获取实时图像并上传该图像) - How can i upload an image through react native(expo) to azure blob storage? (get real time image and upload that image) 如何将图像文件上传到Firebase存储并在Flutter web app中显示 - How to upload an image file to Firebase Storage and show it in the Flutter web app 如何使用 crud 方法将图像文件上传到 FIRESTORE? - How to upload image files to FIRESTORE using crud method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM