Overview

Cloud Storage is scalable and maintenance-free. By integrating the Cloud Storage SDK, you can store high volumes of data such as images, audios, and videos generated by your users to the cloud or download these data from the cloud.

What You Will Create

In this codelab, you will build an app that integrates Cloud Storage capabilities. Your app will allow users to upload a file to the cloud and download it to a local path by calling APIs.

What You Will Learn

What You Will Need

Development Environment and Skill Requirements

Device Requirements

To integrate Cloud Storage of AppGallery Connect, you must complete the following preparations:

For details, please refer to Configuring App Information in AppGallery Connect.

Enabling Cloud Storage

Before integrating the Cloud Storage SDK, you need to enable Cloud Storage in AppGallery Connect.

  1. Sign in to AppGallery Connect and click My projects.
  2. Click the project for which you need to enable Cloud Storage.
  3. Go to Build > Cloud Storage. On the Cloud Storage page, click Enable now.
  4. On the page displayed, set Storage instance and Data storage location.
  5. Click Next. The Define security rules page is displayed by default.
  6. Click Finish. Cloud Storage is successfully enabled for the project.

Integrating the Cloud Storage SDK

If you are using Node.js, you need to integrate the Cloud Storage SDK into your Node.js project before development.

  1. Sign in to AppGallery Connect and click My projects.
  2. Find and click your project.
  3. Click Project settings, access the Server SDK page, and create or download an API client authentication credential.
  4. Change the authentication credential file name and copy the authentication credential to another directory as required.
  5. Open the package.json file in the NPM project and add the following dependency:
    "dependencies": { "@agconnect/cloudstorage-server": "1.0.0" }
  6. Run the following command in the project directory to install the dependency:
    npm install

Before calling the Cloud Storage SDK, import the authentication credential to the SDK.

  1. Import the AppGallery Connect module.
    const {AGCClient, CredentialParser} = require('@agconnect/common-server'); const {StorageManagement} = require('@agconnect/cloudstorage-server');
  2. Import the authentication credential at the code entry. Change the path and JSON file name to the actual values.
    const credential = CredentialParser.toCredential(' [PATH]/agc-apiclient-xxx-xxx.json'); AGCClient.initialize(credential);

You can call related APIs of the Cloud Storage SDK to develop functions.

Initializing the Storage Instance

Before using Cloud Storage APIs, you need to initialize the cloud storage instance, storage instance, and file instance as required. You can obtain the storage instance name from the storage instance box on the Project settings > Build > Cloud Storage page of AppGallery Connect. The testFile.txt file in the code is only an example. Replace it with the actual storage instance name and file name.

const storage = new StorageManagement(); const bucket = storage.bucket('test-bucket'); const file = bucket.file('testFile.txt');

File Upload

Call the uploadFile() method to upload the file to the cloud.

function uploadFile() { const storage = new StorageManagement(); const bucket = storage.bucket(bucketName); bucket.upload('./test.txt').then(res => console.log(res)).catch(err => console.log(err)); }

(The test.txt file to be uploaded is configured in the project code.)

File Download

Call the downloadFile() method to download the file just uploaded from the cloud server to a local path. The file is saved in the upload path specified by localFile.

const fs = require('fs'); function downloadFile() { const storage = new StorageManagement(); const bucket = storage.bucket(bucketName); const remoteFile = bucket.file('test.txt'); const localFile = './test.txt'; remoteFile.createReadStream() .on('error', err => { }) .on('end', () => { }) .pipe(fs.createWriteStream(localFile)) }

File Metadata Operations

  1. Call the getFileMetadata() method to obtain the file metadata.
    function getFileMetadata() { const storage = new StorageManagement(); const bucket = storage.bucket(bucketName); const file = bucket.file('test.txt'); file.getMetadata().then(res => { console.log(res); }).catch(err => { console.log(err); }) }
  2. Call the updateFileMetadata() method to modify the file metadata.
    function updateFileMetadata() { const storage = new StorageManagement(); const bucket = storage.bucket(bucketName); const file = bucket.file('test.txt'); const metadata = { contentLanguage: 'en-US', customMetadata: { test: 'test' } }; file.setMetadata(metadata).then(res => { console.log(res); }).catch(err => { console.log(err); }) }

File List Obtaining

Call the getFileList() method to obtain the file list from the cloud server.

function getFileList() { const storage = new StorageManagement(); const bucket = storage.bucket(bucketName); bucket.getFiles({delimiter: '/'}).then(res => { console.log(res); }).catch(err => { console.log(err); }) }

File Deletion

Call the deleteFile() method to delete the uploaded file from the cloud server.

function deleteFile() { const storage = new StorageManagement(); const bucket = storage.bucket(bucketName); const file = bucket.file('test.txt'); file.delete().then(res => { }).catch(err => { }) }

You can uncomment related methods in the demo to call them.

function deleteFile() { const storage = new StorageManagement(); const bucket = storage.bucket(bucketName); const file = bucket.file('test.txt'); file.delete().then(res => { }).catch(err => { }) }

Run the following command in the project directory to test the demo:

node cloudstorage.js

If the file metadata logs are displayed on the console, the file is successfully uploaded.

Cloud Storage of AppGallery Connect provides visualized file management and data analysis functions.

  1. Sign in to AppGallery Connect and click My projects.
  2. Find and click your project.
  3. Go to Build > Cloud Storage. On the Cloud Storage page that is displayed, you can manage files, including uploading, viewing, downloading, and deleting files.
  4. Click the Usage tab to view data analysis of Cloud Storage. The page displays the storage usage, number of storage objects, network traffic, and number of requests of the current storage instance. You can select a time segment (current day, yesterday, last 7 days, or last 30 days) in the upper right corner of the page.

Well done. You have successfully built an app that integrates Cloud Storage of AppGallery Connect and learned how to manage Cloud Storage usage in AppGallery Connect.

For details about APIs related to Cloud Storage of AppGallery Connect, please refer to API Reference.
To download the sample code, please click the button below:

Download

Code copied