[Huawei Developers] Mayism:
HUAWEI AppGallery Connect Cloud Storage provides maintenance-free cloud storage functions.​

Recently, the Cloud Storage Node.js SDK for server integration has been released. I’ve been using it for a while. You can also access the sample code on GitHub.

1. Configuring the Environment and Version Information​

Version：cloudstorage-server”: “1.0.0”

Environment：Window-Node -v14.15.0，npm v6.14.8，Visual Studio Code

Test Device：PC

cloudstorage-server”: “1.0.0”

AppGallery Connect: https://developer.huawei.com/consumer/en/service/josp/agc/index.html

SDK version: agconnect/cloudstorage-server”: “1.0.0”

SDK integration command: npm install — save @agconnect/cloudstorage-server

2. Enabling and Configuring Cloud Storage in AppGallery Connect​

Currently, Cloud Storage is still in beta testing. To use the service, you’ll first need to send an application by email. For details, visit the following address:

https://developer.huawei.com/consum...Gallery-connect-Guides/agc-cloudstorage-apply

Create an app first and add it to a project, or select an app from the project list on the My projects page in AppGallery Connect.

Under the project, go to Build > Cloud Storage, and click Enable now.

Configure a storage instance as required.

Define the security rules. In this example, the default rules are used.

Note: By default, only authorized users have read and write permissions.

3. Installing the Node.js Environment​

I won’t be describing the detailed procedure for installing the Node.js environment, but here are a few tips:

1. You can download the Node.js package for Windows from this website: https://nodejs.org/en/download/.

2. When you install the environment, the related npm will be automatically installed. You can check its version number by running the following command.

3. I like to use Visual Studio Code (VS Code) for developing Node.js code. If you need to install one, here’s the website: http://code.visualstudio.com/

4. Once you’ve finished installing VS Code, open the testNodeJSDemo project, create a JavaScript file, and write some code.

5. Run the JavaScript file in VS Code. Go to RUN > Run and Debug. Select the Node.js file, and you can see the output at the bottom.

4. Integrating the SDK​

1. Open the CLI, go to your project directory, and run the npm init command to create the package.json file. Keep pressing Enter to apply the default parameters.

2. Run the following command to add the Server SDK dependency of Cloud Storage to the file:

npm install --save @agconnect/cloudstorage-server

3. Download the authentication credential of the Server SDK from AppGallery Connect and save the credential to the path of the created project. If no credential is available, just create one.

5. Developing Functions​

a) Perform the initialization.​

You need to initialize both the Server SDK and the storage instance.

var { AGCClient, CredentialParser } = require("@agconnect/common-server");
var credential = CredentialParser.toCredential("./agc-apiclient-testnodejs.json");
AGCClient.initialize(credential);

const {StorageManagement} = require('@agconnect/cloudstorage-server');

b) Configure the storage instance.​

Copy the bucket name of the project in AppGallery Connect and run the following code to initialize the bucket:

let bucketName = '9105385871708601205-ffeon'; // Configure the storage instance in AppGallery Connect.

c) Upload a file.​

In this example, I upload the test123.txt file in the project path. You can use a file as required.

// uploadFile();
function uploadFile() {
const storage = new StorageManagement();
const bucket = storage.bucket(bucketName);

bucket.upload('./test123.txt')
.then(res => console.log(res))
.catch(err => console.log(err));
}

d) Download a file.​

Here, I download the test123.txt file from the root directory on the cloud and rename the downloaded file test_download.txt. This is just an example.

const fs = require('fs');
// downloadFile();
function downloadFile() {
const storage = new StorageManagement();
const bucket = storage.bucket(bucketName);
const remoteFile = bucket.file('test123.txt');
const localFile = './test_download.txt';

remoteFile.createReadStream()
.on('error', err => {
})
.on('end', () => {
})
.pipe(fs.createWriteStream(localFile))
}

e) Delete a file.​

Here, I delete the test123.txt file in the root directory from the cloud. This is also an example.

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

f) List all files.​

Here, I list all files in the root directory from the cloud.

//getFileList();
function getFileList() {
const storage = new StorageManagement();
const bucket = storage.bucket(bucketName);

bucket.getFiles({delimiter: '/'}).then(res => {
console.log(res)
}).catch(err => {
console.log(err);
})
}

6. Verifying Functions​

1. Verify the upload function.​

Delete the comment tag before uploadFile(); and run the node CloudStorage.js command to upload a file.

We can now see the uploaded test123.txt file in AppGallery Connect.

2. Verify the download function.​

Delete the comment tag before downloadFile(); and run the node CloudStorage.js command to download a file. If you see no information, don’t worry, it’s normal.

You’ll then see the downloaded test_download.txt file in the following path.

3. Verify the function of listing files.​

Delete the comment tag before getFileList(); and run the node CloudStorage.js command to view all files in the root directory. The following information is displayed.

You can see the same files displayed in AppGallery Connect.

4. Verify the deletion function.​

Delete the comment tag before deleteFile(); and run the node CloudStorage.js command to delete a file. This function will also give back no output.

You can see that the test123.txt file in AppGallery Connect has been deleted as well.

7. Summary​

Cloud Storage allows you to manage your project files using Node.js code without the hassle of building a server and O&M. Also, with AppGallery Connect, a web console, you can easily manage your files on the cloud.

In addition to file upload, download, and deletion, Cloud Storage also offers functions such as listing files and setting metadata.

Cloud Storage development guide:

https://developer.huawei.com/consum...-connect-Guides/agc-cloudstorage-introduction

Cloud Storage sample code:

https://github.com/AppGalleryConnec...ster/agc-cloudstorage-demo-nodejs/nodejs-demo