1. Introduction
Overview
Cloud Storage is scalable and maintenance-free. By integrating the Cloud Storage SDK, you can store high volumes of data such as image, audio, and video files generated by your users to the cloud or download these data from the cloud.
What You Will Create
In this codelab, you will create an app that integrates Cloud Storage capabilities. Your app will allow users to query and delete files by calling APIs.
What You Will Learn
- Integrate the Cloud Storage SDK.
- Manage files on the Cloud Storage page in AppGallery Connect.
2. What You Will Need
Development Environment and Skill Requirements
- A computer with Android Studio installed for app development
- Skills in Java and Android Studio
- A HUAWEI ID, whose identity has been verified
Device Requirements
A device running Android 4.2 or a later version
3. Integration Preparations
To integrate Cloud Storage of AppGallery Connect, you must complete the following preparations:
- Create an app in AppGallery Connect.
- Create an Android Studio project.
- Add the app package name.
- Add the AppGallery Connect plugin and Maven repository address.
-
Synchronize the project.
For details, please refer to Integration Preparations.
4. Configuring the Development Environment
Enabling Cloud Storage
Before integrating the Cloud Storage SDK, you need to enable Cloud Storage in AppGallery Connect.
- Sign in to AppGallery Connect and click My projects.
- Click the project for which you need to enable Cloud Storage.
- Go to Build > Cloud Storage. On the Cloud Storage page, click Enable now.
-
Enter the storage instance name and
select the default data processing location.
-
Click Next and view the default security rules.
- Click Finish.
Integrating the Cloud Storage SDK
If you are using Android Studio, you need to integrate the Cloud Storage SDK into your Android Studio project before development.
- Sign in to AppGallery Connect and click My projects.
- Find and click your project.
- Go to Project settings > General information. In the App information area, download the agconnect-services.json file.
-
Copy the agconnect-services.json file to your
app's module directory.
-
Open the app-level build.gradle file and add the
following code to integrate the Cloud Storage SDK:
// Configure an address. apply plugin: 'com.huawei.agconnect' dependencies { // Add the dependency related to the AppGallery Connect framework before adding a storage dependency. implementation 'com.huawei.agconnect:agconnect-storage:1.5.0.100' }
-
Click Sync Now to synchronize the configuration.
5. Designing the UI
-
Create a layout page in the Android Studio project and add two
buttons and a TextView to this page.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" tools:context=".MainActivity"> <Button android:onClick="getFileList" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="Get FileList" /> <Button android:onClick="deleteFile" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="Delete File" /> <TextView android:id="@+id/showResult" android:enabled="false" android:hint="This will display the result of the operation" android:layout_width="match_parent" android:layout_marginTop="10dp" android:gravity="center" android:layout_height="wrap_content" /> </LinearLayout>
-
Add the following buttons and TextView:
- Get FileList: obtains an object list.
- Delete File: deletes a file.
- TextView: displays the result of triggering the previous buttons.
-
Design the UI as follows.
6. Prerequisites
The sample code in this codelab uses the anonymous sign-in mode. Therefore, you need to enable anonymous authentication of Auth Service in AppGallery Connect. Otherwise, sign-in fails.
- Sign in to AppGallery Connect and click My projects.
- Find and click your project.
- Go to Build > Auth Service. If it is the first time that you use Auth Service, click Enable now in the upper right corner.
-
Click the Authentication mode tab and click
Enable in the Operation column for
Anonymous account.
-
Add the following build dependencies to the app-level
build.gradle file (usually in the
app directory) to integrate the Auth Service SDK:
implementation 'com.huawei.agconnect:agconnect-core:1.6.5.300' implementation 'com.huawei.agconnect:agconnect-auth:1.6.5.300'
-
Add permissions to the AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
-
Call the method for requesting permissions in
onCreate of MainActivity. The
sample code is as follows:
private String[] permissions = { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, }; ActivityCompat.requestPermissions(this, permissions, 1);
-
Call methods related to Auth Service in onCreate of
MainActivity. The sample code for anonymous
authentication is as follows (before verifying Cloud Storage
functions, ensure that anonymous sign-in is successful):
private void login() { if (AGConnectAuth.getInstance().getCurrentUser() != null) { System.out.println("already sign a user"); return; } AGConnectAuth.getInstance().signInAnonymously().addOnSuccessListener(signInResult -> System.out.println("AGConnect OnSuccess")) .addOnFailureListener(e -> System.out.println("AGConnect OnFail: " + e.getMessage())); }
7. Developing Functions
You can call related APIs of the Cloud Storage SDK to develop functions.
Uploading Files to the Cloud
Sign in to
AppGallery Connect, go to My projects > Build >
Cloud Storage, click Upload file on
the Files tab page, and upload the
test.jpg file.
Initializing the Service
Call the AGCStorageManagement.getInstance method to initialize a storage instance before your app uses any Cloud Storage functions.
private void initAGCStorageManagement() {
mAGCStorageManagement = AGCStorageManagement.getInstance();
}
Obtaining the File List
Tap Get FileList. The getFileList method is called to obtain the list of certain files from the cloud.
public void getFileList(View view) {
if (mAGCStorageManagement == null) {
initAGCStorageManagement();
}
getFileList();
}
private void getFileList() {
final String path = "test.jpg";
new Thread(new Runnable() {
@Override
public void run() {
StorageReference storageReference = mAGCStorageManagement.getStorageReference(path);
Task<ListResult> listResultTask = null;
listResultTask = storageReference.list(100);
try {
listResultTask.addOnSuccessListener(new OnSuccessListener<ListResult>() {
@Override
public void onSuccess(ListResult listResult) {
mShowResultTv.setText("getfilelist success!");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
mShowResultTv.setText("getfilelist failure!");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
Deleting a File
Tap Delete File. The deleteFile method is called to delete the uploaded file from the cloud.
public void deleteFile(View view) {
if (mAGCStorageManagement == null) {
initAGCStorageManagement();
}
deleteFile();
}
private void deleteFile() {
final String path = "test.jpg";
System.out.println(String.format("path=%s", path));
new Thread(new Runnable() {
@Override
public void run() {
StorageReference storageReference = mAGCStorageManagement.getStorageReference(path);
Task<Void> deleteTask = storageReference.delete();
try {
deleteTask.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mShowResultTv.setText("delete success!");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
mShowResultTv.setText("delete failure!");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
8. Related Methods
View related methods in onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowResultTv = findViewById(R.id.showResult);
AGConnectInstance.initialize(getApplicationContext());
login();
ActivityCompat.requestPermissions(this, permissions, 1);
}
9. Packing and Testing
- Run your Android Studio project and generate an APK. Then, install the APK on your mobile phone for testing.
- Run the installed APK.
-
Tap Get FileList to obtain the file list.
-
Tap Delete File to delete the uploaded
test.jpg file. You can view the deletion result
under Cloud Storage >
Files.
10. Congratulations
Well done. You have successfully built an app that integrates Cloud Storage of AppGallery Connect and learned how to manage Cloud Storage in AppGallery Connect.