Huawei AppGallery Connect provides a cloud storage service, which claims to provide a convenient cloud storage service. When application developers use it, they can use it directly without paying attention to the deployment of servers.
At present, this function is still in the bate stage. I experienced it first. If you want to quickly experience the functions of cloud storage services, please refer to demo.
1. Environment and application information
Version name | Integrated environment | Test equipment |
---|---|---|
agconnect-storage:1.3.1.100 | Android Studio | Glory Magic 2 |
AGC address: https://developer.huawei.com/consumer/cn/service/josp/agc/index.html
SDK integration mode: Maven warehouse integration, docking with Huawei Maven warehouse:
implementation 'com.huawei.agconnect:agconnect-storage:1.3.1.100'
2. Open Cloud Storage on AGC:
PS: the cloud storage service is still in beta status. I can only use it after I send an email to apply for opening:
Select your development project under my project, find the cloud storage service under build, and click Open:
If you don't have an Android project, you can create one yourself first.
When opening the service, you need to configure the storage instance first. Here, you can configure it as needed. I can configure one at will.
Next, you need to configure the security policy. Just use the default security policy here:
PS: by default, only authenticated users can read and write.
3. Integrate SDK in Android project
a) Integrated SDK
1. Add Huawei Maven to the gradle file at the project level. The configuration is as follows:
buildscript { repositories { //... maven {url 'https://developer.huawei.com/repo/'} } dependencies { //... classpath 'com.huawei.agconnect:agcp:1.4.1.300' } } allprojects { repositories { //... maven {url 'https://developer.huawei.com/repo/'} } }
2. Open the application level build Gradle file, configure the SDK of cloud storage and the SDK of Huawei authentication service, and configure the contents marked in red below. Be careful not to leave the agcp plug-in above.
apply plugin: 'com.android.application' apply plugin: 'com.huawei.agconnect' android {.....} dependencies { //... implementation 'com.huawei.agconnect:agconnect-auth:1.4.1.300' implementation 'com.huawei.agconnect:agconnect-storage:1.3.1.100' }
b) Download the json file and configure the default storage instance
1. On the AGC interface, select my project - > project settings – > general, and then download agconnect services JSON file to the app path of your Android project.
2. Remember to check your json file and pay attention to whether there is default_ If there is no storage, you need to add it yourself.
4. Pre step
1. Apply for permission
You need to apply for the read-write permission and network access permission of the file first. In mainfest In the outer layer of application in the XML file, configure the following codes to apply for permission:
Note that the parameter of android:allowBackup must be false.
<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" /> <application android:allowBackup="false"/>
2. Interface layout
Set several buttons and click them to realize the functions: including the buttons to upload, download and delete files.
<?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="uploadFile" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="Upload File" /> <Button android:onClick="downloadFile" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAllCaps="false" android:text="Download File" /> <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>
5. Function development:
1. Initialize parameters first
In MainActivity, initialize parameters first, including cloud storage instance, display message box, and related permissions.
private AGCStorageManagement mAGCStorageManagement; private TextView mShowResultTv; private String[] permissions = { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, }; @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); }
2. Related methods: anonymous login & get path
Anonymous authentication method: for cloud storage data operations, Huawei authentication service is required. Here, in order to simplify, only Huawei anonymous authentication is used:
private void login() { if (AGConnectAuth.getInstance().getCurrentUser() != null) { System.out.println("already sign a user"); return; } AGConnectAuth.getInstance().signInAnonymously().addOnSuccessListener(new OnSuccessListener<SignInResult>() { @Override public void onSuccess(SignInResult signInResult) { System.out.println("AGConnect OnSuccess"); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { // onFail } }); }
Method of obtaining file path: the data operation of cloud storage, the acquisition of local files during uploading, and the download and storage of cloud files are all under this path, i.e. / AGCSdk path
private String getAGCSdkDirPath() { String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/AGCSdk/"; System.out.println("path=" + path); File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } return path; }
3. Initialize cloud storage instance
Before each operation of cloud storage, you need to ensure that the cloud storage instance has been initialized
private void initAGCStorageManagement() { mAGCStorageManagement = AGCStorageManagement.getInstance(); }
4. Upload file:
You need to get the local file and the path of the file first, and then create a file reference to upload the file.
public void uploadFile(View view) { if (mAGCStorageManagement == null) { initAGCStorageManagement(); } final String path = "test.jpg"; String fileName = "test.jpg"; String agcSdkDirPath = getAGCSdkDirPath(); final File file = new File(agcSdkDirPath, fileName); if (!file.exists()) { mShowResultTv.setText("file is not exist!"); return; } StorageReference storageReference = mAGCStorageManagement.getStorageReference(path); UploadTask uploadTask = storageReference.putFile(file); try { uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.UploadResult>() { @Override public void onSuccess(UploadTask.UploadResult uploadResult) { mShowResultTv.setText("upload success!"); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { mShowResultTv.setText("upload failure!" + e.getMessage()); } }); } catch (Exception e) { e.printStackTrace(); } }
5. Download File:
You need to create the file in the local device first, including the path and file name of the file. Then create a reference to the cloud file name and download the downloadTask operation of the reference of this file.
public void downloadFile(View view) { if (mAGCStorageManagement == null) { initAGCStorageManagement(); } String fileName = "download_" + System.currentTimeMillis() + ".jpg"; final String path = "test.jpg"; String agcSdkDirPath = getAGCSdkDirPath(); final File file = new File(agcSdkDirPath, fileName); StorageReference storageReference = mAGCStorageManagement.getStorageReference(path); DownloadTask downloadTask = storageReference.getFile(file); try { downloadTask.addOnSuccessListener(new OnSuccessListener<DownloadTask.DownloadResult>() { @Override public void onSuccess(DownloadTask.DownloadResult downloadResult) { mShowResultTv.setText("download success!"); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { mShowResultTv.setText("download failure!" + e.getMessage()); } }); } catch (Exception e) { e.printStackTrace(); } }
6. Delete file:
First, specify a file named test JPG file, create a reference to the file name, and then perform the deleteTask operation on the reference to delete the cloud test Jpg deleted.
public void deleteFile(View view) { if (mAGCStorageManagement == null) { initAGCStorageManagement(); } final String path = "test.jpg"; System.out.println(String.format("path=%s", path)); 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!" + e.getMessage()); } }); } catch (Exception e) { e.printStackTrace(); } }
6. Packaging test:
Connect Android Studio to your phone and run the Android project on your phone.
1. Prepare initial documents
Open the file manager of the mobile phone, find the internal storage / AGCSdk / path, add and prepare a test JPG file. As shown in the figure below:
2. Upload files & upload results
Open the application just now and click the Upload File button to view the upload results
At this time, the file just uploaded can also be seen on the AGC interface:
2. Download files & Download Results
Click the download button in the application to see the interface showing that the download was successful.
Back to the file manager, you can see the file you just downloaded.
2. Delete file & delete result
Click the delete button in the application to see the successful deletion displayed on the application interface.
At this time, go to the AGC interface to confirm the download results and find the test The JPG file has been deleted
7. Summary
If you only focus on the development of front-end applications, you can develop an application with cloud storage server. No longer need to worry about the construction and operation and maintenance of the server, saving time and effort. It also provides a web console similar to the administrator mode, which can manage the files on the server simply and intuitively.
In addition to the most common upload, download and delete functions, this cloud storage service also includes functions such as listing files and setting metadata. For details, see the official documents:
Cloud Storage Service Development Guide:
Cloud storage codelab:
https://developer.huawei.com/consumer/cn/codelab/CloudStorage/index.html#1
Original link: https://developer.huawei.com/consumer/cn/forum/topic/0201411971207960391?fid=0101271690375130218
Author: Mayi