AppGallery Connect(简称AGC)云存储服务是为开发者打造的可伸缩、免维护的云端存储服务,通过集成云存储SDK,您可以将图片、音频、视频或其他由用户生成的内容存储到云端和从云端下载回本地。
在本次Codelab中,您将建立一个集成云存储能力的应用,该应用具备如下功能:
准备一台Android设备,必须为Android 4.2及以上版本。
集成AGC云存储服务,需要完成以下准备工作:
集成Cloud Storage SDK前需要开通云存储服务,具体步骤如下:
针对Android Studio开发环境,开发前需集成Cloud Storage SDK到您的Android Studio项目中。
// 配置如下地址
apply plugin: 'com.huawei.agconnect'
dependencies {
// 添加存储依赖时,先添加依赖AGC框架的相关依赖
implementation 'com.huawei.agconnect:agconnect-storage:1.5.0.100'
}
<?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="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>
本次Codelab中示例代码采用的匿名登录模式,因此在工程配置前需要在AGC管理台启用AGC认证服务的匿名帐号功能,否则登录失败。
implementation 'com.huawei.agconnect:agconnect-core:1.6.5.300'
implementation 'com.huawei.agconnect:agconnect-auth:1.6.5.300'
<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” />
private String[] permissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
};
ActivityCompat.requestPermissions(this, permissions, 1);
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()));
}
本次Codelab中您可以调用Cloud Storage SDK的相关API进行相应功能点开发,详见如下:
在应用客户端使用云存储功能前,需调用AGCStorageManagement.getInstance初始化存储实例。
private void initAGCStorageManagement() {
mAGCStorageManagement = AGCStorageManagement.getInstance();
}
点击"Upload File"按钮触发调用uploadFile方法可以将文件上传到云端。
public void uploadFile(View view) {
if (mAGCStorageManagement == null) {
initAGCStorageManagement();
}
uploadFile();
}
private void uploadFile() {
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 does not exist!");
return;
}
new Thread(new Runnable() {
@Override
public void run() {
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!");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
(在工程代码中配置了指定的上传的文件:在手机的内部存储AGCSdk目录下的test.jpg文件)
点击"Delete File"按钮触发调用deleteFile方法可以将刚才上传的文件从云端删除。
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();
}
@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);
}
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;
}
祝贺您,您已经成功地构建了您的第一个集成AGC Cloud Storage的应用程序,并学到了如何在AGC管理台对Cloud Storage进行管理。