Quickly Integrate Cloud Storage of AppGallery Connect Into a nodeJS
HUAWEI AppGallery Connect Cloud Storage provides maintenance-free cloud storage functions. See the release of Cloud Storage Node.js SDK for server integration.
Join the DZone community and get the full member experience.Join For Free
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.
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
- SDK version: agconnect/cloudstorage-server”: “1.0.0”
- SDK integration command: npm install — save @agconnect/cloudstorage-server
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 here.
- 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.
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:
- You can download the Node.js package for Windows from this website.
- When you install the environment, the related npm will be automatically installed. You can check its version number by running the following command.
- I like to use Visual Studio Code (VS Code) for developing Node.js code. If you need to install one; here’s the website.
Integrating the SDK
- 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.
- Run the following command to add the Server SDK dependency of Cloud Storage to the file
- 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.
Perform the Initialization
You need to initialize both the Server SDK and the storage instance.
Configure the Storage Instance.
Copy the bucket name of the project in AppGallery Connect and run the following code to initialize the bucket:
Upload a File
In this example, I upload the test123.txt file in the project path. You can use a file as required.
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.
Delete a File
Here, I delete the test123.txt file in the root directory from the cloud. This is also an example.
List all Files
Here, I list all files in the root directory from the cloud.
Verify the Upload Function
Delete the comment tag before uploadFile(), and run the node CloudStorage.js command to upload a file.
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.
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.
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.
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.
Opinions expressed by DZone contributors are their own.