華為AppGallery Connect提供了一個云存盤(CloudStorage)的服務,號稱提供了一個便捷的云端存盤服務,應用開發者使用的時候,可以不用關注服務器的部署,直接使用就行,
目前這個功能還在bate階段,我先搶先體驗了一下,如果你想快速體驗云存盤服務的功能,請參考demo,
1、環境與應用資訊
| 版本名稱 | 集成環境 | 測驗設備 |
|---|---|---|
| agconnect-storage:1.3.1.100 | Android Studio | 榮耀Magic 2 |
AGC地址:https://developer.huawei.com/consumer/cn/service/josp/agc/index.html
SDK集成方式:Maven倉集成,對接華為Maven倉:
implementation ‘com.huawei.agconnect:agconnect-storage:1.3.1.100’
2、在AGC上開通云存盤:
PS: 云存盤服務目前還處于beta狀態,我是發了郵件申請開通才可以使用的:
https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-cloudstorage-apply
在我的專案 下選擇你的開發專案,在構建下面,找到云存盤服務,點擊開通:
沒有Android專案的話,可以先自己創建一個,

開通服務的時候,需要先配置存盤實體,這里按需配置就可以,我就隨便配置一個,

下一步,還需要配置安全策略,這里使用默認的安全策略就好:
PS:默認的情況是,只有經過身份認證的用戶才能進行讀寫,

3、在Android專案里集成SDK
a) 集成SDK
1、在專案級的gradle檔案中添加華為Maven,配置如下內容
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、打開應用級的build.gradle檔案,配置好云存盤的SDK和華為認證服務的SDK,配置下面標紅的內容即可,注意別落了上面的agcp插件,
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) 下載json檔案,并且配置默認存盤實體
1、在AGC界面上,選擇 我的專案 -> 專案設定–> 常規 下面,下載agconnect-services.json檔案到你的Android專案的app路徑下,
2、記得查看你的json檔案,注意是否有default_storage,如果沒有的話,需要自行添加,

4、前置步驟
1、申請權限
需要先申請檔案的讀寫權限和網路訪問權限,在Mainfest.xml檔案里面application的外層,配置下面這些代碼來申請權限:
注意android:allowBackup的引數必須為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、界面布局
設定幾個按鈕,通過點擊按鈕來實作功能:包括上傳,下載檔案,和洗掉檔案的按鈕,
<?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、功能開發:
1、先初始化引數
在MainActivity,先初始化引數:包括,云存盤實體,展示訊息框,以及相關權限,
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、相關方法:匿名登錄&獲取路徑
匿名認證方法:對于云存盤的資料操作,需要經過華為認證服務,這里為了簡化,就僅使用華為的匿名認證:
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
}
});
}
獲取檔案路徑的方法:對于云存盤的資料操作,上傳時候本地檔案的獲取,以及云端檔案的下載存放,都是在這個路徑下, 即 /AGCSdk路徑下
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、初始化云存盤實體
云存盤的每一步操作之前,都需要確保已經進行初始化了云存盤實體
private void initAGCStorageManagement() {
mAGCStorageManagement = AGCStorageManagement.getInstance();
}
4、上傳檔案:
需要先獲取到本地檔案 ,以及這個檔案的路徑,然后創建檔案參考,對檔案的進行上傳的uploadTask操作,
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、下載檔案:
需要先在本地設備中創建該檔案,包括這個檔案的路徑和檔案名,然后創建一個云端檔案名稱的參考,對這個檔案的參考的進行下載的downloadTask操作,
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、洗掉檔案:
首先,先指定一個檔案名為test.jpg的檔案,對這個檔案名創建一個參考,然后對該參考執行deleteTask操作,就可以將云端的test.jpg洗掉了,
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、打包測驗:
Android Studio連接手機,然后將該安卓專案運行到手機上,
1、準備好初始檔案
打開手機的檔案管理器,找到 內部存盤/AGCSdk/ 路徑下,添加并且準備一個test.jpg檔案,如下圖所示:

2、上傳檔案 & 上傳結果
打開剛剛的應用,點擊Upload File 按鈕,查看上傳結果


此時AGC界面上,也可以看到剛剛上傳的檔案:

2、下載檔案 & 下載結果
在應用內點擊下載按鈕,可以看到界面顯示 下載成功,
此時回到檔案管理器,可以看到剛剛下載的檔案,


2、洗掉檔案 & 洗掉結果
點擊應用里面的洗掉按鈕,可以看到應用界面上顯示洗掉成功,

此時到AGC界面上確認下載結果,發現剛剛的test.jpg檔案已經被洗掉了

7、總結
僅關注前端應用的開發,就可以開發一個帶云端存盤服務器的應用,再也不用為了服務器的搭建和運維擔心,省時省力,而且還提供了類似于管理員模式的web控制臺,可以簡單直觀的對服務器上的檔案進行管理,
這個云存盤服務,除了最普通的上傳下載和洗掉功能,還包括有列舉檔案,設定元資料等功能,具體可以看官方檔案:
云存盤服務開發指南:
https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-cloudstorage-introduction
云存盤codelab:
https://developer.huawei.com/consumer/cn/codelab/CloudStorage/index.html#1
原文鏈接:https://developer.huawei.com/consumer/cn/forum/topic/0201411971207960391?fid=0101271690375130218
原作者:Mayism
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/262948.html
標籤:其他
