簡體   English   中英

如何將 JPEG 圖像上傳到 IPFS - Golang

[英]How Upload JPEG Image To IPFS - Golang

我正在嘗試將代碼內部的圖像上傳到 IPFS。 我在我的機器上運行守護進程。 步驟:打開文件,解碼,轉換為字節,使用 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)

因此,我使用 CID Inspector 在 base32 https://cid.ipfs.io/#QmUi25FVFwzW9bywDeoYbVkfqAqQEdYhz8Scicm1fqjusq中生成 CID。

新 URL: https://bafybeic6t543xz7w23xovave7kyysqbnf6wy6cbrmrrygxh2sibd2ahjeq.ipfs.dweb.link 盡管如此,我仍然收到錯誤消息: 504 Gateway Time-out openresty

由於這個結果,我正在考慮我可能錯誤地存儲了圖像。 在我的代碼下方,用於讀取圖像並保存為字節,並在 function UploadIPFS中調用它並返回哈希/CID。 使用了進口shell "github.com/ipfs/go-ipfs-api" 任何人都可以幫助我嗎?

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
}

我也面臨這個錯誤我沒有通過完整的 URL 這就是為什么我遇到錯誤也許這也在你身邊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM