👨🏼‍💻What is Huawei Cloud Storage and How to use it ? Android | Kotlin

Sezer Ă–zaltun
Huawei Developers
Published in
4 min readDec 5, 2022
Huawei Cloud Storage

Introduction

Hello everyone, we are together with a new article. I’ll explain some topics such as What is Cloud Storage and How Can we use it. Let’s start.

What is Cloud Storage ?

AppGallery Connect Cloud Storage allows you to store high volumes of data such as images, audio, videos, and other user-generated content securely and economically.

Advantages of Cloud Storage

  • Strong security: Data is transmitted using HTTPS, and files are encrypted and stored on the cloud using secure encryption protocols.
  • Resumable transfer: You can resume uploads or downloads from the breakpoint if there is a network failure or misoperation while the upload or download is underway.
  • High scalability: Cloud Storage is built for exabyte scale when your app has much data to store.
  • Easy O&M: You can easily locate the cause of an error according to the result code.

How To Use Cloud Storage

Now let’s see how to use Cloud Storage step by step.

Enabling the Service

  1. Sign in to AppGallery Connect and click My projects.
  2. Find and click your project.
  3. Go to Build > Cloud Storage. On the Cloud Storage page, click Enable now.
Cloud Storage Enable now

4. On the page displayed, set Storage instance and Default data processing location.

Cloud Storage Reference

Note: If you select Germany as location, you need to send mail to get permisson. Recommended Singapore

5. Click Next and view the default security rules.

Security Rules

Note: The default security rules allow authorized users to read and write all files. You can modify rules only after the service is enabled.

6. Click Finish.

AppGallery Connect Configuration

For its configuration for AppGallery Connect, you can read the related article by clicking here.

Integrating Cloud Storage SDK

  1. Obtain the agconnect-services.json file.
  2. Verify the default_storage setting under service > cloudstorage in the agconnect-services.json file

3. Configure a dependency on Cloud Storage to the dependencies block in build gradle(app)

Modifying the AndroidManifest File

To read and write files and access the Internet, configure the following permissions in the AndroidManifest.xml file:

Initializing Permissions

To enable the Cloud Storage SDK to obtain the device storage permission, call the permission request methods in the onCreate method of MainActivity. The sample code is as follows:

Initializing a Storage Instance

You can initialize an instance by adding the following codes to your project.

Creating a Reference

Files are stored in storage instances on the cloud. The files in a storage instance are presented in a hierarchical structure, just like the file system on your local hard disk. After creating a reference to a file, you can use this reference to upload, download, or delete the file, or update its metadata. In addition, you can also create a reference to a directory and use this reference to list all files in this directory. We will do this during the file upload.

Uploading a File

You can upload files using the code here. I uploaded it with a .png extension because I wanted upload image.

path: Your file path on Cloud Storage.

agcSdkDirPath: It allows you to get the path of your file in the phone’s memory.

File: storageReference expects a file path of type File from us. So we define agcSdkDirPath in File.

Listing Files

You can use the code below to view all the files you have uploaded to Cloud Storage.

path: It is the address of the files you upload on Cloud Storage.

listResultTask returns a list of type Listresult. I converted it to ArrayList and showed the whole list in RecyclerView.

Downloading a File

You can safely download files on Cloud Storage with the code below.

I used the Environment.DIRECTORY_DOWNLOADS parameter because I prefer that the pictures in Cloud Storage are downloaded to the downloads folder of the phone.

Deleting a File

You can delete your file on Cloud Storage with the code below.

Cloud Storage File Path
Cloud Storage Uploaded Files

Conclusion

In this article, we learned what Cloud Storage is and how to use it. With Cloud Storage, you can securely and easily store your files on the cloud and access them from anywhere. I hope it has been a useful resource for you. See you in the next articles….

--

--