简体   繁体   中英

How Upload JPEG Image To IPFS - Golang

I am trying to upload an image from inside the code to IPFS. I am running daemon on my machine. Steps: Open the file, Decode it, Convert to bytes, Upload to IPFS using localhost 5001.

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 .

New URL: https://bafybeic6t543xz7w23xovave7kyysqbnf6wy6cbrmrrygxh2sibd2ahjeq.ipfs.dweb.link . Nevertheless I still getting error message: 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. Imported shell "github.com/ipfs/go-ipfs-api" was used. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM