[Huawei Developers] Mayism:
HUAWEI AppGallery Connect Cloud Storage provides maintenance-free cloud storage functions.Currently, this service is still under beta testing.If you want to quickly experience this service, see the demo on GitHub.

1. Environment

SDK Version：agconnect-storage:1.3.0.300

Platform：Cocos Creator

Device：HUAWEI P40 Pro

AppGallery Connect:

https://developer.huawei.com/consumer/en/service/josp/agc/index.html

2. Enabling and Configuring Cloud Storage in AppGallery Connect

Note: Currently, Cloud Storage is still under beta state. To use the service, you need to send an email for application. For details, check:

https://developer.huawei.com/consum...Gallery-connect-Guides/agc-cloudstorage-apply

Create an app first and add it to a project, or select an app from the project list on the My projects page in AppGallery Connect.

Under the project, go to Build > Cloud Storage, and click Enable now.

Configure a storage instance as required.

Define security rules. In this example, the default rules are used.

Note: By default, only authenticated users can read and write data.

3. Integrating the Cloud Storage SDK in Cocos Creator

a. Integrate the SDK.

Official documentation:

https://docs.cocos.com/creator/manual/en/cocos-service/agc-applinking.html

1. On the Service panel of Cocos Creator, find Cloud Storage. Currently, the Cloud Storage SDK supports only the Android platform.

2. Before integrating a service, you need to associate the service with an app. Click Association. In the dialog box that is displayed, click Create. The Cocos console is displayed.

3. On the Cocos console, create a game.

4. Go back to Cocos Creator and create, refresh, or select an association.

5. Return to the Cloud Storage page, and enable the service.

6. Configure the Default Instance by entering the one set in AppGallery Connect.

b. Download the JSON file.

1、 Go to Project settings in AppGallery Connect and download the latest agconnect-services.json file.

2. Save the downloaded agconnect-services.json file to the settings directory in your Cocos project.

4. Preparations for Development

1. Modify the security rules.

Default security rules allow only users authenticated by Huawei to perform operations on cloud storage files.

You can simplify the integration by allowing all users to perform operations without Huawei authentication.

// Allow all users.
agc.cloud.storage[
match: /{bucket}/{path=**} {
allow read, write: if true;
}
]

2. Configure the UI layout.

Configure buttons for listing, uploading, downloading, and deleting files separately.

5. Function Development

1. Initialize Cloud Storage and create a listener in the start block.

start () {
this._storage = huawei.agc.storage;
this._storage.storageService.on("error", data => console.log("Cloud Storage", `error : ${data.errCode}:${data.errMsg}`), this);
this._storage.storageService.on("get-file-metadata", data => console.log("Cloud Storage", JSON.stringify(data)), this);
this._storage.storageService.on("update-file-metadata", data => console.log("Cloud Storage", JSON.stringify(data)), this);
this._storage.storageService.on("delete-file", data => console.log("Cloud Storage", JSON.stringify(data)), this);
this._storage.storageService.on("list-file", data => console.log("Cloud Storage", JSON.stringify(data)), this);
this._storage.storageService.on("get-download-url", data => console.log("Cloud Storage", JSON.stringify(data)), this);
this._storage.storageService.on("task", data => {
console.log("Cloud Storage", JSON.stringify(data));
if (data.task instanceof this._storage.AGCDownloadTask && data.status === 'successful') {
jsb.fileUtils.renameFile(jsb.fileUtils.getWritablePath() + "/output.json", jsb.fileUtils.getWritablePath() + "/output1.json");
}
}, this);

// Create a reference to the root directory.
this.rootReference = huawei.agc.storage.storageService.getInstance().getStorageReference();
},

2. List files.

Use the ListAll method to list all files.

listAll:function () {
this.rootReference.listAll();
console.log('Cloud Storage', 'ListAll file');
}

3. Download a file.

Download a test.jpg file from the cloud and rename it test1.jpg.

download:function () {
// Delete the original file before downloading a new one.
jsb.fileUtils.removeFile(jsb.fileUtils.getWritablePath() + "/test.jpg");
this.rootReference.child("test.jpg").getFile(jsb.fileUtils.getWritablePath() + "/test1.jpg");
console.log('Cloud Storage', 'download test.jpg, and reNamed test1.jpg');
},

4. Upload a file.

Upload the downloaded test1.jpg file.

upload:function () {
if (!jsb.fileUtils.isFileExist(jsb.fileUtils.getWritablePath() + "/test1.jpg")) {
return console.log('Cloud Storage', 'local file test1.jpg not exist, please click Download!')
}
this.rootReference.child("test1.jpg").putFile(jsb.fileUtils.getWritablePath() + "/test1.jpg");
console.log('Cloud Storage', 'upload test1.jpg');
},

5. Delete a file.

Delete the test1.jpg files from both the local path and cloud.

delete:function () {
this.rootReference.child("test1.jpg").delete();
console.log('Cloud Storage', 'delete test1.jpg success!')
jsb.fileUtils.removeFile(jsb.fileUtils.getWritablePath() + "/test1.jpg");
},

6. Packaging and Testing

In Cocos Creator, go to Project > Build…, package an Android app, install it to your device, and verify the functions.

android:allowBackup=”false”

Note: If you choose the android-30 API, you need to change the value of android:allowBackup to false in the AndroidManifest.xml file.

1. List files.

Click ListAll to view all listed files in the Logcat area. Filter log information by entering Cloud Storage.

2. Download a file.

Click Download and view the result in log information.

3. Upload a file.

Click Upload and view the result in log information.

You can also view the uploaded test1.jpg file in AppGallery Connect.

4. Delete a file.

Click Delete and view the result in log information.

The uploaded test1.jpg file is also deleted in AppGallery Connect.

7. Summary

Cloud Storage allows you to store your Cocos-based game data on the cloud without the hassle of building a server and O&M. Also, with AppGallery Connect, a web console, you can easily manage your files on the cloud side.

In addition to file upload, download, and deletion, Cloud Storage also provides metadata settings.

For more details, please check:

Cloud Storage development guide:

https://developer.huawei.com/consum...-connect-Guides/agc-cloudstorage-introduction

Cocos documentation:

https://docs.cocos.com/creator/manual/en/cocos-service/agc-cloudstorage.html