[Huawei Developers] John Wo:
Overview

Cloud Storage Service allows you to store high volumes of data such as images, audios, and videos generated by your users securely and economically with direct device access. The service is stable, secure, efficient, and easy-to-use, and can free you from development, deployment, O&M, and capacity expansion of storage servers.

In this article, let us learn how to do the following:

Create an app and configure its information in AppGallery Connect.

Use B4A IDE to integrate the HUAWEI HMS Cloud Storage SDK into your app and develop storage-related functions.

Build and demonstrate a demo app.

Manage files on the Cloud Storage page in AppGallery Connect.

For details on installation and configuration of B4A IDE, please refer to https://www.b4x.com/b4a.html

Creating Wrapper Class

1. Download the following AAR package inside Addition folder in your project directory

HMSSDK AGConnect:

https://developer.huawei.com/repo/com/huawei/agconnect/agconnect-auth/1.4.1.300/agconnect-auth-1.4.1.300.aar

https://developer.huawei.com/repo/com/huawei/agconnect/agconnect-core/1.4.0.300/agconnect-core-1.4.0.300.aar

https://developer.huawei.com/repo/com/huawei/agconnect/agconnect-storage/1.3.0.300/agconnect-storage-1.3.0.300.aar

https://developer.huawei.com/repo/com/huawei/agconnect/datastore-core/1.4.1.300/datastore-core-1.4.1.300.aar

https://developer.huawei.com/repo/com/huawei/agconnect/datastore-annotation/1.4.1.300/datastore-annotation-1.4.1.300.jar

https://developer.huawei.com/repo/com/huawei/agconnect/agconnect-https/1.4.1.300/agconnect-https-1.4.1.300.aar

https://developer.huawei.com/repo/com/huawei/agconnect/agconnect-credential/1.4.1.300/agconnect-credential-1.4.1.300.aar

HMSSDK Base:

https://developer.huawei.com/repo/com/huawei/hms/base/5.0.0.301/base-5.0.0.301.aar

HMSSDK Update:

https://developer.huawei.com/repo/com/huawei/hms/availableupdate/5.0.0.301/availableupdate-5.0.0.301.aar

https://developer.huawei.com/repo/com/huawei/hms/update/2.0.6.302/update-2.0.6.302.aar

HMSSDK Device:

https://developer.huawei.com/repo/com/huawei/hms/device/5.0.0.301/device-5.0.0.301.aar

HMSSDK DynamicAPI:

https://developer.huawei.com/repo/com/huawei/hms/dynamic-api/1.0.13.303/dynamic-api-1.0.13.303.aar

HMSSDK Log:

https://developer.huawei.com/repo/com/huawei/hms/log/5.0.0.301/log-5.0.0.301.aar

HMSSDK Network:

https://developer.huawei.com/repo/com/huawei/hms/network-common/4.0.2.300/network-common-4.0.2.300.aar

https://developer.huawei.com/repo/com/huawei/hms/network-grs/4.0.2.300/network-grs-4.0.2.300.aar

HMSSDK Stats:

https://developer.huawei.com/repo/com/huawei/hms/stats/5.0.0.301/stats-5.0.0.301.aar

HMSSDK Tasks:

https://developer.huawei.com/repo/com/huawei/hmf/tasks/1.4.1.300/tasks-1.4.1.300.aar

HMSSDK UI:

https://developer.huawei.com/repo/com/huawei/hms/ui/5.0.0.301/ui-5.0.0.301.aar

Squareup Libraries:

https://repo1.maven.org/maven2/com/squareup/okhttp3/okhttp/3.11.0/okhttp-3.11.0.jar

https://repo1.maven.org/maven2/com/squareup/okio/okio/1.14.0/okio-1.14.0.jar

Note: After download of the above AAR files, create Addition folder inside Project and keep all AAR packages in it.

2. Open each of the AAR package with RAR tool and rename the class.jar and AndroidManifest.xml files. And save those jar files inside libs folder (It is recommended that the two files be renamed consistently with the AAR package names.)

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104105544.86054782652144717812424570604068:50511125015537:2800:BDEB92FE9C7C301523E6F3AEBCB14B3DD90ADCA14297FB6E863A6D7BADB8BD3D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104105544.86054782652144717812424570604068:50511125015537:2800:BDEB92FE9C7C301523E6F3AEBCB14B3DD90ADCA14297FB6E863A6D7BADB8BD3D.png[/IMG]

3. Copy required permissions in the <manifest> section in B4A IDE. Remember to aggregate all the permissions as shown below.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104105619.09081511046564735036911775982129:50511125015537:2800:45E1FB324BCAD7C819D5BA157B575FD859A6AF640E8FA451F5C6D4A55AF60082.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104105619.09081511046564735036911775982129:50511125015537:2800:45E1FB324BCAD7C819D5BA157B575FD859A6AF640E8FA451F5C6D4A55AF60082.png[/IMG]

4. Copy all configurations, referring to the AndroidManifest.xml file in all AAR files, to the <application> section in the B4A IDE Manifest Editor.

NOTE: Remember to aggregate all the configurations in relevant sections as shown below.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104105953.64016922077442203983791042341403:50511125015537:2800:B582F2D50D677754DD6D60DA828411569BCA59FDB5CAEAE7706084D16EC85BE9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104105953.64016922077442203983791042341403:50511125015537:2800:B582F2D50D677754DD6D60DA828411569BCA59FDB5CAEAE7706084D16EC85BE9.png[/IMG]

5. Change all occurrences of ${applicationId} to $PACKAGE$.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104110102.12005493163191328511342318515342:50511125015537:2800:BF59F2D906D35172AC91BF9A57AA754CAA65208BDF67CFB2EE101BEF41B9840C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104110102.12005493163191328511342318515342:50511125015537:2800:BF59F2D906D35172AC91BF9A57AA754CAA65208BDF67CFB2EE101BEF41B9840C.png[/IMG]

6. Download the configuration file (agconnect-services.json) from App Gallery Connect and add the JSON File to the assets Folder.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104110659.63644370091617378805590727289641:50511125015537:2800:5690DA848DB566DDB9A9DE73E1B1FD4D4A00F56F47B5BA8F880784D3CBA78896.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104110659.63644370091617378805590727289641:50511125015537:2800:5690DA848DB566DDB9A9DE73E1B1FD4D4A00F56F47B5BA8F880784D3CBA78896.png[/IMG]

B4A will automatically incorporate files in the assets folder of an AAR package to the assets folder of your main project.

Encapsulating Java Files Using SLC

Create Library as parent and then create bin, libs and src as subfolder in the project directory

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104111333.60104053213906793576796640853636:50511125015537:2800:B82BCAE8ACDB04482B988D44B18F54F6433999E08761D59D09A12C2736D227E9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201104111333.60104053213906793576796640853636:50511125015537:2800:B82BCAE8ACDB04482B988D44B18F54F6433999E08761D59D09A12C2736D227E9.png[/IMG]

2. Develop java project inside the following path,

Library Folder > src > b4x > hms > cloudstorage

a. Import the following two lines of code to each Java file

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;

b. Add necessary annotations to the Java files

[CODE [MENTION=702169]version[/MENTION](1.0f)

@ShortName("CloudStorageFuncs")

@DependsOn(values={

"agconnect-auth-1.4.1.300.aar",

"agconnect-core-1.4.0.300.aar",

"agconnect-storage-1.3.0.300.aar",

"availableupdate-5.0.0.301.aar",

"base-5.0.0.301.aar",

"device-5.0.0.301.aar",

"dynamic-api-1.0.13.303.aar",

"log-5.0.0.301.aar",

"network-common-4.0.2.300.aar",

"network-grs-4.0.2.300.aar",

"stats-5.0.0.301.aar",

"tasks-1.4.1.300.aar",

"ui-5.0.0.301.aar",

"update-2.0.6.302.aar",

"okhttp-3.11.0.jar",

"datastore-core-1.4.1.300.aar",

"datastore-annotation-1.4.1.300.jar",

"agconnect-https-1.4.1.300.aar",

"agconnect-credential-1.4.1.300.aar",

"okio-1.14.0.jar"

})

[/CODE]

c. Modify the context.

B4A does not recognize the Context class. Therefore, when parsing a class that contains the [MENTION=702169]version[/MENTION](1.0f) annotation, it will report an error if a method of the class has referenced Context. In this case, you need to change Context to the B4A object BA.

public void init(BA ba) {
Log.d(LOGTAG, "init entered. before AGConnectInstance initialize() ");

Log.d(LOGTAG, "init before AGCStorageManagement.getInstance()");
mAGCStorageManagement = AGCStorageManagement.getInstance();
if(mAGCStorageManagement == null){
Log.d(LOGTAG, "init entered. before AGConnectInstance initialize() ");
showToast(ba, "mAGCStorageManagement == null");
AGConnectInstance.initialize(ba.context);

Generating library

1. Download SimpleLibraryCompiler from the below link,

www.b4x.com/android/files/SimpleLibraryCompiler.zip

2. Go to Project > Build Configurations and change the B4A project package name to the project package name configured in App Gallery Connect

3. Select the Addition folder and add AAR packages to it.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115536.87231072892682401277470247148201:50511125060810:2800:49DB248C28FCCE7A8849853D7BF71C1B6783D820A63302AD4DD66EE80D3F2233.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115536.87231072892682401277470247148201:50511125060810:2800:49DB248C28FCCE7A8849853D7BF71C1B6783D820A63302AD4DD66EE80D3F2233.png[/IMG]

4. Select the project folder, define the library name, and click Compile

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115559.95288088336430204945211886683020:50511125060810:2800:A300FF27160DA5BDCD2477607F27AEE364AFABC2A04B7221CF38D67078E30751.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115559.95288088336430204945211886683020:50511125060810:2800:A300FF27160DA5BDCD2477607F27AEE364AFABC2A04B7221CF38D67078E30751.png[/IMG]

Integrating the HMS Cloud Storage Kit Libraries for B4A

Enable library in B4A IDE

Add the #AdditionalJar field to Project Attributes to reference the AAR packages

#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: HMSCloudStorageCodelabs
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#AdditionalJar: agconnect-auth-1.4.1.300.aar
#AdditionalJar: agconnect-core-1.4.0.300.aar
#AdditionalJar: agconnect-storage-1.3.0.300.aar
#AdditionalJar: availableupdate-5.0.0.301.aar
#AdditionalJar: base-5.0.0.301.aar
#AdditionalJar: device-5.0.0.301.aar
#AdditionalJar: dynamic-api-1.0.13.303.aar
#AdditionalJar: log-5.0.0.301.aar
#AdditionalJar: network-common-4.0.2.300.aar
#AdditionalJar: network-grs-4.0.2.300.aar
#AdditionalJar: stats-5.0.0.301.aar
#AdditionalJar: tasks-1.4.1.300.aar
#AdditionalJar: ui-5.0.0.301.aar
#AdditionalJar: update-2.0.6.302.aar
#AdditionalJar: okhttp-3.11.0.jar
#AdditionalJar: datastore-core-1.4.1.300.aar
#AdditionalJar: datastore-annotation-1.4.1.300.jar
#AdditionalJar: agconnect-https-1.4.1.300.aar
#AdditionalJar: agconnect-credential-1.4.1.300.aar
#AdditionalJar: okio-1.14.0.jar
#End Region

Add below code in B4A project to call the methods written for cloud storage kit.

Sub Process_Globals
Private rp As RuntimePermissions
End Sub

Sub Globals
Dim locationPermissionGranted As Int
Dim CSFuncs As CloudStorageFuncs
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("AccountAuthCS")

Log("Entered Activity_Create")
'Following Runtime Dangerous Permissions needed for accessing Cloud Storage SDK
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)

Log("Activity_PermissionResult before init")
CSFuncs.init

Log("Activity_PermissionResult before showToast")
CSFuncs.showToast("CSFuncs init completed")

Log("Activity_Create exit ")
End Sub

' Callback on permission granted / denied by the user.
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
Log("Activity_PermissionResult entered")

If Permission = rp.PERMISSION_WRITE_EXTERNAL_STORAGE Then
If True = Result Then
locationPermissionGranted = locationPermissionGranted + 1
Log("Activity_PermissionResult rp.PERMISSION_WRITE_EXTERNAL_STORAGE Result = " & Result)
End If
End If

If Permission = rp.PERMISSION_READ_EXTERNAL_STORAGE Then
If True = Result Then
locationPermissionGranted = locationPermissionGranted + 1
Log("Activity_PermissionResult rp.PERMISSION_READ_EXTERNAL_STORAGE Result = " & Result)
End If
End If

If 2 = locationPermissionGranted Then
Log("Activity_PermissionResult, permission granted")
End If
End Sub

Sub Activity_Start
Log("Activity_Start entered")
End Sub

Sub Activity_Resume
Log("Activity_Resume entered")
End Sub

Sub Activity_Pause (UserClosed As Boolean)
Log("Activity_Pause entered")
End Sub

Sub Activity_Stop
Log("Activity_Stop entered")
End Sub

Sub Button3_Click
Log("Button3_Click entered")

Log("Button3_Click before uploadFile")
CSFuncs.uploadFile("sample_file.txt")
End Sub

Sub Button2_Click
Log("Button2_Click entered")

Log("Button2_Click before Auth login")
CSFuncs.login
End Sub

Sub Button7_Click
Log("Button7_Click entered")

Log("Button7_Click before deleteFile")
CSFuncs.deleteFile("sample_file.txt")
End Sub

Sub Button6_Click
Log("Button6_Click entered")

Log("Button6_Click before updateFileMetadata")
CSFuncs.updateFileMetadata("sample_file.txt")
End Sub

Sub Button5_Click
Log("Button5_Click entered")

Log("Button5_Click before getFileMetadata")
CSFuncs.getFileMetadata("sample_file.txt")
End Sub

Sub Button4_Click
Log("Button4_Click entered")

Log("Button4_Click before getFileList")
CSFuncs.getFileList("/")
End Sub

Sub Button1_Click
Log("Button1_Click entered")

Log("Button1_Click before downloadFile")
CSFuncs.downloadFile("sample_file.txt")
End Sub

Integrating the HMS Core SDK

Before you start developing an app, integrate the HMS Core SDK into your project.

a. Sign in to AppGallery Connect and select My Projects.

b. Find your app from the list and click the app name.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115804.29269100453803094007115467602922:50511125060810:2800:7797D36897EC7C64AF413AD20DE7B6EC81293FB75C2F676CFEB478730632AD1C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115804.29269100453803094007115467602922:50511125060810:2800:7797D36897EC7C64AF413AD20DE7B6EC81293FB75C2F676CFEB478730632AD1C.png[/IMG]

c. Enable Cloud Storage Kit in App gallery connect

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115818.40163843660281814605884492142051:50511125060810:2800:E93ED7624994EDE0A52FA356376CA0DCDADA9E790839931DD11CCA53B5E11849.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115818.40163843660281814605884492142051:50511125060810:2800:E93ED7624994EDE0A52FA356376CA0DCDADA9E790839931DD11CCA53B5E11849.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115829.06460188255783378498336282899052:50511125060810:2800:49EA3DA5DA498970781390DEC9DD34EAEADECB5122424081DB0BC5D9A5FA50B0.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115829.06460188255783378498336282899052:50511125060810:2800:49EA3DA5DA498970781390DEC9DD34EAEADECB5122424081DB0BC5D9A5FA50B0.png[/IMG]

d. Go to Develop > Overview > App information.

e. Click agconnect-services.json to download the configuration file.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115849.74015482434182418857307009364817:50511125060810:2800:EA5F4DC5EB50D0E9F29333980131B72C81CC57548C7EBFE3129A1A0536F55A45.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115849.74015482434182418857307009364817:50511125060810:2800:EA5F4DC5EB50D0E9F29333980131B72C81CC57548C7EBFE3129A1A0536F55A45.png[/IMG]

Configuring App Information

In order to enable the communication between your application and the AppGallery, there are some configurations that need to be made.

Configuring the Signing Certificate

1. Sign in to AppGallery Connect and select My Projects.

2. Find your app from the list and click the app name.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115914.59076423806882649297649356529641:50511125060810:2800:0F867D49E1E987062210A8FD9851CF10BFC0EEF7F2DA663026CC1E896701C535.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115914.59076423806882649297649356529641:50511125060810:2800:0F867D49E1E987062210A8FD9851CF10BFC0EEF7F2DA663026CC1E896701C535.png[/IMG]

3. In the App information section of the page, set SHA-256 certificate fingerprint to the SHA-256 fingerprint that was generated in the preceding Obtaining the SHA-256 Fingerprint From Signature File section.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115939.02032418595817667552596847355372:50511125060810:2800:E9E1C662B102975025DFD6D69299D11E8D9C13CD0B4D1982168CAB51F63F8724.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113115939.02032418595817667552596847355372:50511125060810:2800:E9E1C662B102975025DFD6D69299D11E8D9C13CD0B4D1982168CAB51F63F8724.png[/IMG]

Setting Package Signing Options

To create a signed application package perform the following steps:

1. Go to Tools > Private Sign Key and select the SHA256 signature file specified in AppGallery Connect as the B4A project signature file.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113120001.12148445751395455915573006291708:50511125060810:2800:191B6CA36C841AF77246C581CE4892FC78C754CD2EC20AB541E4269295FA4DAF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113120001.12148445751395455915573006291708:50511125060810:2800:191B6CA36C841AF77246C581CE4892FC78C754CD2EC20AB541E4269295FA4DAF.png[/IMG]

2. Declare MainApplication in the Manifest file.

The path of MainApplication must also be the path to the src folder where MainApplication is located.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113120023.40062840479422523990957874943374:50511125060810:2800:8DD6CC39465DE23464BC56F52FC375A58F046EB70CA443BE359F4A66A7BD9F65.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113120023.40062840479422523990957874943374:50511125060810:2800:8DD6CC39465DE23464BC56F52FC375A58F046EB70CA443BE359F4A66A7BD9F65.png[/IMG]

Testing the App

Open the developed app and check whether it can execute all the cloud storage methods properly.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113120230.43244963926157899939663917939319:50511125060810:2800:89F3C3DF8949E7345F27258B4FD8A7EB41336EB55687118C1A0A40EF6EFA50E3.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113120230.43244963926157899939663917939319:50511125060810:2800:89F3C3DF8949E7345F27258B4FD8A7EB41336EB55687118C1A0A40EF6EFA50E3.jpg[/IMG]

Verify on the AGC console that the cloud storage methods have executed properly.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113120349.09144119294416613861943264951229:50511125060810:2800:222C5300CA81478D981669B54C3777F5EE74ED25E45B62BEE8C62C7BCD2B626F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/872/635/2640091000001872635.20201113120349.09144119294416613861943264951229:50511125060810:2800:222C5300CA81478D981669B54C3777F5EE74ED25E45B62BEE8C62C7BCD2B626F.png[/IMG]

Congratulations

Well done. You have successfully learned how to:

Create an app and configure its information in AppGallery Connect.

Use B4A IDE to integrate the HUAWEI HMS Cloud Storage SDK into your app and develop storage-related functions.

Build and demonstrate a demo app.

Manage files on the Cloud Storage page in AppGallery Connect