当前位置:首页 > 文章列表 > Golang > Go问答 > 无法从超级账本结构中的链代码实例将数据上传到谷歌云存储

无法从超级账本结构中的链代码实例将数据上传到谷歌云存储

来源:stackoverflow 2024-03-30 09:21:33 0浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《无法从超级账本结构中的链代码实例将数据上传到谷歌云存储》,聊聊,我们一起来看看吧!

问题内容

我尝试编写一个链代码,以便当它在对等实例中执行时,它将数据上传到谷歌云存储桶。我要上传的文件实际上作为小文件块存储在文件夹中,以便不同的对等方将不同的块上传到 gcs 存储桶。我使用 fabcar 蓝图来开发此链代码,并使用测试网络脚本文件来执行链代码。我用来上传数据的函数在本地执行时运行良好,但是当我尝试在链码中使用时,它显示

error: endorsement failure during invoke. response: status:500 message:"error in simulation: failed to execute transaction 49a9b96088ff2f32906a6b6c9ba1f4ac0a530779bf8d506b176fcdfb8818afe2: error sending: chaincode stream terminated"

(我正在做的事情可能听起来很疯狂,但我是这个超级账本结构的新手)

下面是我正在执行的代码示例(我认为这是 uploadgcs 或 initledger 函数的问题)(仅供参考:链码执行仅运行 initledger 函数,当然它使用了 uploadgcs 函数)

package main

import (
    "fmt"
    "os"
    "io"
    "log"
    "strings"
    "encoding/json"
    "encoding/hex"
    "github.com/hyperledger/fabric-contract-api-go/contractapi"
    "path/filepath"
    "strconv"
    "crypto/sha256"
    "time"
    "context"
    "cloud.google.com/go/storage"
    "google.golang.org/api/option"
    "golang.org/x/oauth2/google"
)

type SmartContract struct {
    contractapi.Contract
}

type Data struct {
    Owner  string `json:"owner"`
    File string `json:"file"`
    FileChunkNumber string `json:"filechunknumber"`
    SHA256 string `json:"sha256"`
}

func uploadGCS(owner, filechunklocation, uploadlocation string) error {
    ct := context.Background()
    creds, err := google.FindDefaultCredentials(ct, storage.ScopeReadOnly)
    if err != nil {
            log.Fatal("GoT an err %s", err)
    }

    client, err := storage.NewClient(ct, option.WithCredentials(creds))
    if err != nil {
        return fmt.Errorf("storage.NewClient: %v", err)
    }
    defer client.Close()

    // Open local file.
    f, err := os.Open(filechunklocation)
    if err != nil {
        return fmt.Errorf("os.Open: %v", err)
    }
    defer f.Close()

    ct, cancel := context.WithTimeout(ct, time.Second*50)
    defer cancel()

    // Upload an object with storage.Writer.
    wc := client.Bucket("btp2016bcs0015-cloud-storage").Object(uploadlocation).NewWriter(ct)
    if _, err = io.Copy(wc, f); err != nil {
        return fmt.Errorf("io.Copy: %v", err)
    }
    if err := wc.Close(); err != nil {
        return fmt.Errorf("Writer.Close: %v", err)
    }
    return nil
}

func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error {
    filelocation := "/home/busyfriend/go/src/github.com/hyperledger/fabric-samples/test-network/samplefile---pdf"
    data := []Data{
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "1", SHA256: "eb73a20d61c1fb294b0eba4d35568d10c8ddbfe2544a3cacc959d640077673f5"},
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "2", SHA256: "92dd8ea8aa0da4a48a2cb45ae38f70f17526b6b50ef80c44367a56de6ec9abf9"},
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "3", SHA256: "b97027d261d01f86d1e514a52886add096ddc4e66d15d01e53516dd9d5cfb20b"},
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "4", SHA256: "377582f5e62dc3b34e40741f2d70d8f37a029856f75cbe68a6659328258e23a3"},
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "5", SHA256: "afb6c6d112d446ac07d78b13957bb440105038411095032de444bf08e3bbdba8"},
        Data{Owner: "ID126859", File: "samplefile.pdf", FileChunkNumber: "6", SHA256: "e43b885c2bfb47130c54fa70528fb2a91d9d1af1417a0f7c5a4c22d8f16efb01"},
    }

    for i := range data {
        _, dir := filepath.Split(filelocation)
        dir_1 := strings.Split(dir,"---")
        filechunk := dir_1[0]+"_"+ data[i].FileChunkNumber
        filechunklocation := filepath.Join(filelocation, filechunk)
        uploadlocation :=  data[i].Owner + "/" + dir + "/" + filechunk

        err := uploadGCS(data[i].Owner, filechunklocation, uploadlocation)

        if err != nil {
            return fmt.Errorf("Got an error %s", err.Error())
        }
    }

    for i, putdata := range data {
        dataAsBytes, _ := json.Marshal(putdata)
        err := ctx.GetStub().PutState("DATA"+strconv.Itoa(i), dataAsBytes)

        if err != nil {
            return fmt.Errorf("Failed to put to world state. %s", err.Error())
        }
    }

    return nil
}

// Uploads new data to the world state with given details
func (s *SmartContract) uploadData(ctx contractapi.TransactionContextInterface, dataID string, owner string, filelocation string, filechunknumber string) error {
    //Uploads the filechunk to the cloud storage
    _, dir := filepath.Split(filelocation)
    dir_1 := strings.Split(dir,"---")
    filechunk := dir_1[0]+"_"+ filechunknumber
    filechunklocation := filepath.Join(filelocation, filechunk)
    uploadlocation :=  owner + "/" + dir + "/" + filechunk
    err := uploadGCS(owner, filechunklocation, uploadlocation)
    if err != nil {
        fmt.Println(err.Error())
        return err
    }

    //Creates SHA256 hash of the file chunk
    f, err := os.Open(filechunklocation)
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()
    h := sha256.New()
    if _, err := io.Copy(h, f); err != nil {
        log.Fatal(err)
    }

    data := Data{
        Owner: owner,
        File: dir_1[0]+"."+dir_1[1],
        FileChunkNumber: filechunknumber,
        SHA256: hex.EncodeToString(h.Sum(nil)),
    }

    dataAsBytes, _ := json.Marshal(data)

    return ctx.GetStub().PutState(dataID, dataAsBytes)
}


func main() {

    chaincode, err := contractapi.NewChaincode(new(SmartContract))

    if err != nil {
        fmt.Printf("Error create cloud chaincode: %s", err.Error())
        return
    }

    if err := chaincode.Start(); err != nil {
        fmt.Printf("Error starting cloud chaincode: %s", err.Error())
    }
}

这是我执行此链代码后得到的结果 最终结果


解决方案


你能检查链码容器的日志吗?您可以找到创建的额外容器,其中包含您的链代码的名称和版本。如果您想查看链码生成的日志,则需要查看这些容器(使用 docker 日志 )。

在您的代码中,您正在记录一些致命错误。与其采用记录并继续的方法,不如返回错误并失败。

请注意,根据背书策略(即 AND(Org1.member, Org2.member)),一个调用请求可以在多个对等点上执行。所有这些执行都必须返回相同的结果,并将完全相同相同的数据放入账本或从账本获取完全相同的数据。这是为了确保运行相同链代码的不同组织之间的信任而设计的。 (即,如果您的不同对等方在放入分类帐之前将同一对象转换为具有不同属性顺序的字符串,则会失败。)

我的观点是,如果要将 I/O 操作分开,那么只需将哈希值放入分类帐即可。您的方法与链下数据概念有一些共同点。请在重新考虑之前先查看一下。

理论要掌握,实操不能落!以上关于《无法从超级账本结构中的链代码实例将数据上传到谷歌云存储》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
企业微信接口与PHP实现行政审批的实践步骤企业微信接口与PHP实现行政审批的实践步骤
上一篇
企业微信接口与PHP实现行政审批的实践步骤
Vue 中使用 provide 和 inject 实现祖先组件向后代组件传递数据的技巧
下一篇
Vue 中使用 provide 和 inject 实现祖先组件向后代组件传递数据的技巧
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    508次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    497次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • 茅茅虫AIGC检测:精准识别AI生成内容,保障学术诚信
    茅茅虫AIGC检测
    茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
    41次使用
  • 赛林匹克平台:科技赛事聚合,赋能AI、算力、量子计算创新
    赛林匹克平台(Challympics)
    探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
    61次使用
  • SEO  笔格AIPPT:AI智能PPT制作,免费生成,高效演示
    笔格AIPPT
    SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
    71次使用
  • 稿定PPT:在线AI演示设计,高效PPT制作工具
    稿定PPT
    告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
    66次使用
  • Suno苏诺中文版:AI音乐创作平台,人人都是音乐家
    Suno苏诺中文版
    探索Suno苏诺中文版,一款颠覆传统音乐创作的AI平台。无需专业技能,轻松创作个性化音乐。智能词曲生成、风格迁移、海量音效,释放您的音乐灵感!
    69次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码