Use GUI, CLI or API to Connect an App deployed within VPC to Cloud Object Storage (COS) using a private endpoint.
This scenario illustrates how an application deployed within a VPC can use IBM Cloud Object Storage.
The example shown in this scenario includes a Node.js application (MyCOS) which saves records to an IBM Cloud Object Storage bucket.
After completing the scenario you will be able to:
- Create Virtual Private Clouds using the IBM Cloud console (UI), Command Line Interface (CLI), or Application Programming Interface (API).
- Create Linux Virtual Servers in a Virtual Private Cloud
- Connect to and execute commands in a linux Virtual Server
- Configure a Node.js application to use an existing IBM Cloud Object Storage service.
- Save data into a Cloud Object Storage bucket
This scenario includes a small Node.js application called MyCOS. This Node.js application saves data into IBM Cloud Object Storage.
The MyCOS application will perform the following actions:
- Connect to IBM Cloud Object Storage
- List the buckets that are in the COS
- Create a new record in COS
MyCOS uses the IBM Cloud Object Storage SDK to connect to and save data to IBM Cloud Object Storage.
- An IBM Cloud Account
- Authority to create VPC resources and IBM Cloud Object Storage services in the IBM Cloud Account
- A provisioned instance of the IBM Cloud Object Storage service
- IBM Cloud Object Storage service credentials. Cloud Object Storage Service Credentials
- ssh-keygen installed locally. SSH Keys
- The reader has a basic knowledge of Linux commands and VI editor.
The instructions to deploy the VPC infrastructure for this scenario are available in these flavors:
-
Issue the following command to access the Virtual Server from your workstation:
ssh -i root@*floating ip*- The floating ip is the IP address assigned to the VPC's VSI in the previous section.
If you need to specify the SSH Key file, use the following command:
ssh -i *ssh key file* root@*floating ip*- The ssh key file is the full path and filename of the SSH Key file created in Section #2 above.
-
Update the local package repository. Issue the command
apt update -
Install Node.js and NPM by issuing the following commands:
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -apt install nodejs
-
Verify the installation is complete by issuing the command
node --version. You should see a valid version of Node returned that is greater than or equal to 10.15.0 -
Create a new directory called 'myCOS' and change to that directory by issuing these commands:
mkdir myCOScd myCOS
-
Create the package.json file by issuing the command
vi package.json -
Copy the following code to your clipboard:
{
"name": "mycos",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node main.js"
},
"dependencies": {
"ibm-cos-sdk": "^1.4.1"
},
"repository": {},
"engines": {
"node": "6.x"
}
}
- Paste the code above into the VI editor Hint: In VI, press the 'a' key to start appending text and then paste the text
- Save and exit the file Hint: in VI, press the Colon key and then type wq! and press enter
- Create the main.js file by issuing the command
vi main.js - Copy and paste the entire main.js source file into the vi editor. Open the main.js file
- Using your COS service credentials, edit the following variables in the main.js source code:
- apiKeyId: Set this value to the api-key in your credentials
- serviceInstanceId: Set this value to the resource-instance-id in your credentials
- endPoint: Set the endpoint based on the COS direct endpoint documented here
- Set the bucketName variable to the name of a bucket in your COS that myCOS can insert data into
- Save and exit the file
- Issue the command
npm update - Start the application by issuing the command
npm start
