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

1. Configuring the Environment and Version Information

2. Enabling and Configuring Cloud Storage in AppGallery Connect

3. Installing the Node.js Environment

4. Integrating the SDK

npm install --save @agconnect/cloudstorage-server

5. Developing Functions

a) Perform the initialization.

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.

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

c) Upload a file.

// 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.

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.

// 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.

//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.

2. Verify the download function.

3. Verify the function of listing files.

4. Verify the deletion function.

7. Summary

--

--

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