Cocos|快速集成華爲AGC雲存儲服務

華爲AppGallery Connect提供了一個雲存儲(CloudStorage)的服務,號稱提供了一個便捷的雲端存儲服務,應用開發者使用的時候,能夠不用關注服務器的部署,直接使用就行。html

目前這個功能還在beta階段,我先搶先體驗了一下。前端

一、環境與應用信息

在這裏插入圖片描述

AGC地址:https://developer.huawei.com/consumer/cn/service/josp/agc/index.html java

SDK集成方式:在Cocos Creator中開通集成android

二、在AGC上開通雲存儲:

PS: 雲存儲服務目前還處於beta狀態,使用前應該發郵件去申請開通:web

https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-cloudstorage-applyjson

個人項目 下選擇你的開發項目,在構建下面,找到雲存儲服務,點擊開通:安全

沒有Android項目的話,能夠先本身建立一個。服務器

在這裏插入圖片描述

開通服務的時候,須要先配置存儲實例,這裏按需配置就能夠,我就隨便配置一個。app

在這裏插入圖片描述

下一步,還須要配置安全策略,這裏使用默認的安全策略就好:運維

PS:默認的狀況是,只有通過身份認證的用戶才能進行讀寫。

在這裏插入圖片描述

三、在Cocos Creator項目集成SDK

3.1 集成SDK

官方文檔:https://docs.cocos.com/creator/manual/zh/cocos-service/agc-applinking.html

(1)在 Cocos 服務面板接入所需的雲存儲服務。目前 SDK 僅支持 Android 平臺

在這裏插入圖片描述

(2)接入相關的服務前,須要先關聯APP,在服務界面點擊啓用後,再點擊 關聯 按鈕,點擊建立後,會跳轉到Cocos的管理臺,

在這裏插入圖片描述

(3)能夠根據我的須要建立一個新遊戲

(4)建立完成後,就能夠回到Cocos Creator界面新建,刷新和選擇進行應用的關聯,
在這裏插入圖片描述

(5)而後就能夠回到雲存儲界面,能夠正式進行服務的開通了
在這裏插入圖片描述

(6)服務開通之後,須要配置默認的存儲實例,此處須要填寫AGC界面上的已經開通雲存儲的項目的默認桶。

在這裏插入圖片描述

3.2 下載json文件

(1) 服務開通之後,回到項目設置界面,下載最新的json文件。
在這裏插入圖片描述

(2)將剛下載好json文件,放到Cocos項目的settings目錄下

在這裏插入圖片描述

四、前置步驟

4.1 修改安全規則

默認的安全規則下,須要通過華爲認證的用戶,才能進行雲存儲文件操做。

爲了集成方便,特意修改雲存儲的安全規則,將安全規則修改成所有容許,此時無需通過華爲認證,就能夠對雲存儲中的文件進行操做。

// 所有容許 
agc.cloud.storage[
   match: /{bucket}/{path=**} {
      allow read, write:  if true;
   }
]

在這裏插入圖片描述

4.2 界面佈局

在Cocos Creator上設置幾個按鈕,經過點擊按鈕來實現功能:包括例舉文件,上傳,下載文件,和刪除文件的按鈕。

在這裏插入圖片描述

五、 功能開發:

5.1 初始化,而且在Start中建立監聽

start () {      
        this._storage = huawei.agc.storage;
                 this._storage.storageService.on("error", data => console.log("Cloud Storage", `error : ${data.errCode}:${data.errMsg}`), this);
                 this._storage.storageService.on("get-file-metadata", data => console.log("Cloud Storage", JSON.stringify(data)), this);
                 this._storage.storageService.on("update-file-metadata", data => console.log("Cloud Storage", JSON.stringify(data)), this);
                 this._storage.storageService.on("delete-file", data => console.log("Cloud Storage", JSON.stringify(data)), this);
                 this._storage.storageService.on("list-file", data => console.log("Cloud Storage", JSON.stringify(data)), this);
                 this._storage.storageService.on("get-download-url", data => console.log("Cloud Storage", JSON.stringify(data)), this);
                 this._storage.storageService.on("task", data => {
                         console.log("Cloud Storage", JSON.stringify(data));
                         if (data.task instanceof this._storage.AGCDownloadTask && data.status === 'successful') {
                                  jsb.fileUtils.renameFile(jsb.fileUtils.getWritablePath() + "/output.json", jsb.fileUtils.getWritablePath() + "/output1.json");
                         }
                 }, this);
                 // 建立根目錄的引用
                 this.rootReference = huawei.agc.storage.storageService.getInstance().getStorageReference();
    },

5.2 例舉文件

使用ListALL方法,例舉全部文件:

listAll:function () {
    this.rootReference.listAll();
    console.log('Cloud Storage', 'ListAll file');
}

5.3 下載文件

下載一個雲端的,名爲test.jpg的文件,而且從新命名爲test1.jpg。

download:function () {
// 先 delete 文件,避免文件已存在致使下載失敗
jsb.fileUtils.removeFile(jsb.fileUtils.getWritablePath() + "/test.jpg");
this.rootReference.child("test.jpg").getFile(jsb.fileUtils.getWritablePath() + "/test1.jpg");
console.log('Cloud Storage', 'download test.jpg, and reNamed test1.jpg');
},

5.4 上傳文件

上傳剛剛下載的test1.jpg文件

upload:function () {
                 if (!jsb.fileUtils.isFileExist(jsb.fileUtils.getWritablePath() + "/test1.jpg")) {
                         return console.log('Cloud Storage', 'local file test1.jpg not exist, please click Download!')
                 }
        this.rootReference.child("test1.jpg").putFile(jsb.fileUtils.getWritablePath() + "/test1.jpg");
        console.log('Cloud Storage', 'upload test1.jpg');
    },

5.5 刪除文件

刪除本地的,以及雲端的test1.jpg文件。

delete:function () {
        this.rootReference.child("test1.jpg").delete();
        console.log('Cloud Storage', 'delete test1.jpg success!')
                 jsb.fileUtils.removeFile(jsb.fileUtils.getWritablePath() + "/test1.jpg");
        },

六、打包測試:

使用Cocos creator的build,打包一個Android文件,而且安裝到設備上,驗證功能點是否正確。

android:allowBackup="false"

注意打包的時候,若是API選擇的是android-30,則須要把AndroidManifest文件中的android:allowBackup屬性,修改成false。

6.1 例舉文件

點擊ListALL按鈕,在Logcat中查看全部列舉的文件:在日誌中篩選查看Cloud Storage,查看結果
在這裏插入圖片描述

6.2 下載文件

點擊download按鈕,在日誌中查看下載結果:

在這裏插入圖片描述

6.3 上傳文件

點擊Upload按鈕,在日誌中查看下載結果:

在這裏插入圖片描述

此時在AGC界面上,也能夠看到剛剛上傳的test1.jpg文件
在這裏插入圖片描述

6.4 刪除文件

點擊delete按鈕,在日誌中查看下載結果:
在這裏插入圖片描述

此時在AGC界面上,也能夠剛剛上傳的test1.jpg文件已經被刪除

在這裏插入圖片描述

七、總結

僅關注前端應用的開發,就能夠在Cocos上開發一個帶雲端存儲服務功能的遊戲。不再用爲了服務器的搭建和運維擔憂,省時省力。並且還提供了相似於管理員模式的web控制檯,能夠簡單直觀的對服務器上的文件進行管理。

這個雲存儲服務,除了最普通的上傳下載和刪除功能,還包括有設置元數據等功能,具體能夠看官方文檔:

  欲瞭解更多詳情,請參見:

  雲存儲服務開發指南:

https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-cloudstorage-introduction

  Cocos關於雲存儲的文檔:
https://docs.cocos.com/creator/manual/zh/cocos-service/agc-cloudstorage.html


原文連接:
https://developer.huawei.com/consumer/cn/forum/topic/0201419625098590546?fid=0101271690375130218做者:Jessyyyyy

相關文章
相關標籤/搜索