1. Introduction
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
- Integrate the Cloud Storage SDK.
- Manage files on the Cloud Storage page in AppGallery Connect.
What You Will Need
Development Environment and Skill Requirements
- A Node.js development environment
- Skills in Node.js and JavaScript
- A developer account registered by referring to Registration and Verification
Device Requirements
- A PC or server with Node.js 10.0.0 or later installed
2. Integration Preparations
To integrate Cloud Storage of AppGallery Connect, you must complete the following preparations:
- Create a project in AppGallery Connect.
- Download project-level credential.
- Initialize an NPM project.
For details, please refer to Configuring App Information in AppGallery Connect.
3. Configuring the Development Environment
Enabling Cloud Storage
Before integrating the Cloud Storage SDK, you need to enable Cloud Storage in AppGallery Connect.
- Sign in to AppGallery Connect and click My projects.
- Click the project for which you need to enable Cloud Storage.
- Go to Build > Cloud Storage. On the Cloud Storage page, click Enable now.
- On the page displayed, set Storage instance and Data storage location.
- Click Next. The Define security rules page is displayed by default.
- 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.
- Sign in to AppGallery Connect and click My projects.
- Find and click your project.
- Click Project settings, access the Server SDK page, and create or download an API client authentication credential.
- Change the authentication credential file name and copy the authentication credential to another directory as required.
- Open the package.json file in the NPM project and add the following dependency:
"dependencies": { "@agconnect/cloudstorage-server": "1.0.0" }
- Run the following command in the project directory to install the dependency:
npm install
4. Prerequisites
Before calling the Cloud Storage SDK, import the authentication credential to the SDK.
- Import the AppGallery Connect module.
const {AGCClient, CredentialParser} = require('@agconnect/common-server'); const {StorageManagement} = require('@agconnect/cloudstorage-server');
- 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);
5. Function Development
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
- 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); }) }
- 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 => {
})
}
6. Function Testing
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.
7. File Management
Cloud Storage of AppGallery Connect provides visualized file management and data analysis functions.
- File management: In addition to managing files by calling APIs of the Cloud Storage SDK on the app client, you can manage files in a visualized manner in AppGallery Connect, including uploading files, viewing file details, downloading files, creating folders, deleting files or folders, and creating tokens.
- Data analysis: You can view data analysis about Cloud Storage in AppGallery Connect.
- Sign in to AppGallery Connect and click My projects.
- Find and click your project.
- Go to Build > Cloud Storage. On the Cloud Storage page that is displayed, you can manage files, including uploading, viewing, downloading, and deleting files.
- 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.
8. Congratulations
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.
9. Reference
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: