How Can I Quickly Integrate Cloud Storage of AppGallery Connect into a nodeJS

1. Configuring the Environment and Version Information

Version:cloudstorage-server”: “1.0.0”

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:

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:

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.

npm install --save @agconnect/cloudstorage-server

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.

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.

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.

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.

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store