All Products
Document Center

Example of Integration with OSS

Last Updated: May 19, 2020

To guarantee immutability of data or files, a sample application is provided which can store file on Alibaba Cloud OSS, and record the hash of file contents on Hyperledger Fabric.

This sample application is based on Hyperledger Fabric Go SDK. Please make sure Go development environment is correctly set up. You can refer to Use Fabric Go SDK for guidance.

Create OSS bucket

The sample application requires an OSS bucket to upload file. Please refer to Create a bucket for guidance. Let’s name the bucket new-bucket, which will be used later.

Download sample application

Download sample application from go-oss-demo-1.4.5 to local path: $GOPATH/src, and decompress it. Copy BaaS SDK configuration file connection-profile.yaml to path $GOPATH/src/go-oss-demo. You can refer to Use Fabric Go SDK for how to obtain connection-profile.yaml.

Deploy Chaincode

Upload the chaincode: chaincode/sacc.out to BaaS and instantiate it. Please refer to Deploy chaincode for guidance.

Run sample application

First, run the following commands to compile source codes into two binaries push and fetch.

  1. ./
  • push: Reads a local file, records the hash of the file into ledger by invoking a Fabric chaincode, then uploads the file to OSS.
  • fetch: Reads a hash string, validates the hash exists on ledger by querying a Fabric chaincode, then downloads the file from OSS.

Before running push and fetch, we need to set up some environment variables:

  1. export baas_user="user" # The user create on BaaS.
  2. export baas_secret="password" # The password of the user.
  3. export baas_channel="first-channel" # The name of channel.
  4. export oss_ak="ak" # The access key of OSS.
  5. export oss_ak_secret="secret" # The secret key of OSS.
  6. export oss_endpoint="" # The endpoint of OSS.
  7. export oss_bucket="new-bucket" # The name of bucket which we created before.

To record the file hash on ledger and upload the file to OSS:

  1. $ ./push
  2. Going to enroll user
  3. [fabsdk/fab] 2019/02/19 07:39:26 UTC - lib.(*Client).initHTTPClient -> INFO TLS Enabled
  4. [fabsdk/fab] 2019/02/19 07:39:26 UTC - util.BCCSPKeyRequestGenerate -> INFO generating key: &{A:ecdsa S:256}
  5. [fabsdk/fab] 2019/02/19 07:39:26 UTC - log.print -> INFO encoded CSR
  6. Success enroll user: appuser1
  7. ======================================================
  8. [-] Uploading to Alibaba Cloud OSS, MD5: 9218e8b37fabdc50bb6eca8597ffce22
  9. [-] Upload MD5 to chaincode...
  10. [-] Done.

The output shows the hash of the file is 9218e8b37fabdc50bb6eca8597ffce22, then the application will use the hash to download the file from OSS.

  1. $ ./fetch 9218e8b37fabdc50bb6eca8597ffce22
  2. User appuser1 already enrolled, skip enrollment.
  3. ======================================================
  4. [-] Fetch file info from chaincode...
  5. Chaincode status: 200
  6. Payload: baas-sdk-test1.9218e8b37fabdc50bb6eca8597ffce22
  7. [-] Downloading to 9218e8b37fabdc50bb6eca8597ffce22
  8. [-] Done.

The downloaded file is 9218e8b37fabdc50bb6eca8597ffce22, and we can verify the content is exactly the same as

To get more information about Hyperledger Fabric Go SDK and more sample codes, please see Use Fabric Go SDK.