# BaaS Storage API Introduction

This document describes how to quickly call the public beacon BaaS storage API, which is suitable for API developers and requires a certain programming foundation. Due to the limitation of the size of the storage space in the GXChain, our solution is to store the file body on the side chain (IPFS) and return the file C-ID generated by the side chain to the main chain for verification. The C-ID and timestamp stored in the GXChain can guarantee the basic principle that the uplink data cannot be falsified.

# 1. Create an account

First, you need to have a public letter wallet account that is used to call the BaaS storage service, pay for storage and bill the blockchain.

If you already have a Wallet account, you can skip this step.

If you don't have a wallet account yet, you can create an account with your mobile wallet or web wallet.

PC wallet / web wallet tutorial:

  1. Registration and backup tutorial:http://mp.weixin.qq.com/s/eNQyqY5dyaP299J5qra0Bg (opens new window)
  2. Recovery and import tutorial:http://mp.weixin.qq.com/s/27v540tvhfDHF6Bv5_ObKQ (opens new window)

Mobile Wallet Tutorial:https://forum.gxb.io/topic/130/gxs-移动端钱包发布-说明文档-ios审核已通过 (opens new window)

# 2. Export private key

Export the active permission private key of the account from the mobile wallet or web wallet for later use when calling the SDK.

# 3. BaaS storage service address

  • Online official address: https://baas.gxchain.cn/api/storage
  • Online developer test address: https://baas-developer.gxchain.cn/api/storage

# 4. How to call the SDK

The SDK provides a method wrapper for interacting with the BaaS storage service. Currently, the Java version of the SDK is currently available, and subsequent languages will support multiple languages.

# Java - maven

  • maven address(HTML View): https://repo.gxchain.cn/service/rest/repository/browse/maven-public/
  • maven import address: https://repo.gxchain.cn/repository/maven-public/

Note

If you can't import the package, please change the https to http and try it out.

# maven setting.xml

<mirror>
    <id>gxchain</id>
    <mirrorOf>*</mirrorOf>
    <url>http://repo.gxchain.cn/repository/maven-public/</url>
</mirror>

# pom dependency

<dependency>
    <groupId>com.gxb.block.baas</groupId>
    <artifactId>baas-sdk-client</artifactId>
    <version>1.0.2-RELEASE</version>
</dependency>

# Node - npm

npm install baas-sdk-node

# 5. BaaS storage service API interface

interface description
provider Get service provider information
store Data storage interface
data Get the data interface and get the data according to cid

# provider-interface

Get the information provided by the servant by specifying the path

  • Request address

GET /provider

  • Request parameter none

  • Example (taking curl as an example)

curl https://baas.gxchain.cn/api/storage/provider
  • Response
{
    "code":200,
    "msg":"ok",
    "data":{
        "account_id":"1.2.639287", // Provider formal environment baas account id
        "name":"GXChain Official BaaS Storage",
        "description":"BaaS storage + deposit service",
        "fees":[ // Supported payment asset types and rates
            {"fee_per_kbytes": 20, "asset_id":"1.3.1"}
        ]
    }
}
  • SDK-Example-JAVA
return new BaasDefaultClient(URL_HEADER + "storage/provider").execute(new ProviderReq());

Refer tocom.gxb.block.baas.sdk.client.api.BaasConstantsClass

# store-interface

The caller can charge their own data through the baas platform service by this interface, .

  • Request address

POST /store

Content-Type= multipart/form-data

  • Request parameter
parameter type required max_size description example
data byte/File Y No more than 10MB Raw data to store 12345678asdfg()_:<>!@#$%^&*=-';" '

Description:

  • Store fixed consumption GXS, the minimum unit is 0.0002/kb.

  • Data size limit will be released later

  • Response parameter

parameter type required max_size description example
txid String Y 64 TXID d4763fd0d802473579ae2dcaa2c6707adf4f2e7e
cid String Y 64 CID value stored by IPFS QmaZrwThXyZm8Rxs93Tih3L6p4Q8NqYEXp32iN4PeAqDgv

Example:

{
    "code":200,
    "msg":"ok",
    "data":{
        "cid":"QmaZrwThXyZm8Rxs93Tih3L6p4Q8NqYEXp32iN4PeAqDgv",
        "txid":"d4763fd0d802473579ae2dcaa2c6707adf4f2e7e"
    }
}
  • SDK-Example-JAVA
// build store client
// EXAMPLE_ACCOUNT is your account id
// EXAMPLE_PRIVATE_KEY is your account private key
// EXAMPLE_PUBLIC_KEY is your account public key
// * Attention: Your EXAMPLE_PRIVATE_KEY and EXAMPLE_PUBLIC_KEY can not be uploaded.
StoreClient client = new StoreClient(EXAMPLE_ACCOUNT, EXAMPLE_PRIVATE_KEY, EXAMPLE_PUBLIC_KEY);
// response
StoreDataResp resp = client.store("Hello World!".getBytes());
Refer to com.gxb.block.baas.sdk.client.api.client.StoreClient

The online account id, account active permission public key can be obtained on the block explorer according to the account name:

block explorer: https://block.gxb.io/#/ (opens new window)

Can also obtain the BaaS account id corresponding to the online official environment and the developer test environment through the provider-interface interface.

# Take the account name gxs-dev as an example, params incoming account name
curl --data '{"jsonrpc": "2.0", "method": "get_account_by_name", "params": ["gxs-dev"], "id": 1}' https://node1.gxb.io

# response
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": {
        "id": "1.2.639290",  // account id
        "membership_expiration_date": "1970-01-01T00:00:00",
...
...
        "lifetime_referrer_fee_percentage": 3000,
        "referrer_rewards_percentage": 0,
        "name": "gxs-dev",  // account name
        "owner": {
            "weight_threshold": 1,
            "account_auths": [],
            "key_auths": [
                ["GXC85WbsFPSRjRto4n4gbopwGBEf41iroDesrNxN1WXJLTb9Mv2sc", 1]
            ],
            "address_auths": []
        },
        "active": { // active permission
            "weight_threshold": 1,
            "account_auths": [],
            "key_auths": [
                ["GXC7xQNvkevq5fkCZPfi7rLTXZb1WKfE41sDTxqf7xUg36BLbZLvh", 1] // Active permission public key
            ],
            "address_auths": []
        },
...
...
    }
}
  • Error condition
code msg Description
401 DATA_SIGN_FAILURE Verification failed
402 BALANCE_NO_ENOUGH Insufficient account balance
404 REQ_EXPIRATION Request expired
405 DATA_MD5_INVALID Data MD5 does not pass
406 ACCOUNT_NO_EXIT Account does not exist
407 DATA_OVER_SIZE Data length is too long
408 AMOUNT_INVALID Unsatisfactory amount

# data-interface

Obtain the corresponding data through the Cid value

  • Request address

GET /data/{cid}

  • Request parameter
parameter type required max_size description example
cid String Y 64 Store the Cid value of the data QmaZrwThXyZm8Rxs93Tih3L6p4Q8NqYEXp32iN4PeAqDgv

例子:

GET /api/data/QmaZrwThXyZm8Rxs93Tih3L6p4Q8NqYEXp32iN4PeAqDgv
  • response

Return a file of QmaZrwThXyZm8Rxs93Tih3L6p4Q8NqYEXp32iN4PeAqDgv.baas

  • SDK-Example-JAVA
// build store client
// EXAMPLE_ACCOUNT is your account id
// EXAMPLE_PRIVATE_KEY is your account private key
// EXAMPLE_PUBLIC_KEY is your account public key
// * Attention: Your EXAMPLE_PRIVATE_KEY and EXAMPLE_PUBLIC_KEY can not be uploaded.
StoreClient client = new StoreClient(EXAMPLE_ACCOUNT, EXAMPLE_PRIVATE_KEY, EXAMPLE_PUBLIC_KEY, false);
// byte[]
byte[] result = client.getRawBytes(CID);
// String
String str = client.getRawString(CID);
// File
String file = client.downloadFile(CID,TARGET_FILE); // TARGET_FILE is java.io.File.
  • Error
code msg description
401 NO_EXIT does not exist