Wednesday, December 3, 2025

Working with OCI CLI

 The CLI provides the same core functionality as we have in the Console, with additional commands.
Such as the ability to run scripts, extend Console functionality.

We can use CLI from Cloud Shell or from our Local machine.

The CLI is pre-configured with our credentials and ready to use immediately from within Cloud Shell.

Some of the usecases will be :

Working on OCI functions:

We can use OCI and Fn Project  CLI  to work on OCI functions , create function,push the docker image to OCR, invoke function etc.

Working on OCI Object Storage Bulk Operation:

Many a times we need to perform bulk operation like bulk download, bulk delete , Bulk restore objects from object storage bucktes. In such cases OCI cli is really helpful.

There are many such usecases where OCI CLI works as a very powerful tool.

In this blog we will see:

  • How to install OCI CLI locally
  • How to Authenticate to OCI
  • How to use the profile which  gets created locally
  • Run few commands from Local machine for OCI Object storage

In the next post we will see more about OCI functions setup, working and testing.

 

How to install OCI CLI locally

We can follow below Oracle doc  to install CLI based on our OS:

https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/cliinstall.htm

Before installing oci-cli if we type oci and press enter in terminal it will give below message:


 

To install oci cli Run the below command:

brew update && brew install oci-cli
 It will download and install all the dependency.
 It will take few mins to complete.
 If the link has not happened then run:
 brew link oci-cli  
 
Once the cli is installed :
type the command oci and press enter:
we should see like below:
 
 
Now let us run the actual commands.
To do this we need to first authenticate to the OCI
so type the below command and run:
oci session authenticate 

It will ask for region to select:


Ours is us-ashburn-1 so we just need to type 50 and press enter


We may get popup like below, click on allow:
This will open to authenticate from browser.
Select your tenancy and click continue:
 
 
We can have multiple sign in options based on the identity provider setup we have in OCI.
we can select the option we want to login with:
We will take the Onelogin option, this is sso  federate one:
 
 
Once successfully logged in from browser we will get below message in browser:
 
 
Inter terminal it will be like below:
When we are doing for the first time then we it creates a DEFAULT profile in config
 which can be used to run OCI CLi commands:
 
 
Copy the below command (this is a sample command to test oci connectivity with the profile that 
got created):
oci iam region list --config-file /Users/sumit_kumar/.oci/config --profile DEFAULT --auth security_token 
We can see this in .oci folder
 
 
 
We can go see config contents to see the profile details :
type less config and press enter
 
 
 
 
When the session expires then we need to refresh the token with below command:
oci session refresh 

We will get message like below when token is successfully refreshed:
 
 
Now let us see how we can run commands to access OCI services.
Run the command we had copied after authentication:
oci iam region list --config-file /Users/sumit_kumar/.oci/config --profile DEFAULT --auth security_token 
 
This will give region list also ensures the oci connectivity from local machine.
 
 
 
Every time we run any command we need to give the profile for security token detail like above.
Since it is constant for every command run we can create alias for oci profile and use.
alias op='oci --profile DEFAULT --auth security_token' 
 
Then we just need to type the below command for region list :
op iam region list 
 
 
 
 Now when we authenticate next time with oci session authenticate
 , it will ask to create new profile:
We can give different profile name or same name and use accordingly.
 
 
 
 
Now let us see how we we can use oci cli to download object store objects in bulk:
 
 
 We will download the below files in bulk:
 
 
Since we are already authenticated we just need to run the command ,
with alias or with full command:
We will try with full command:
oci os object bulk-download --config-file /Users/sumit_kumar/.oci/config --profile DEFAULT 
--auth security_token --namespace-name tenancy_name  --bucket-name cusa-prod-xyz-integrations 
--prefix xyz/Alteryx/Aug23New --download-dir . 
 
 
 
 
 
We get response like below:
 
 
Now we will verify from directory if all 22 files were downloaded successfully:
 
 
All the files were downloaded successfully.
Thus we see that how useful OCI CLI was in this example. It helped us downlaod all the files from 
the given folder from bucket in a single click. Otherwise we would have had to download one by one 
from UI.
 
Like this there are many other usecases where OCI CLI helps.
One example is working on OCI Function.
In the next post we will see how we can install fn project cli, work on OCI fuctions, 
login to docker and push our OCI function docker image to OCR and invoke these functions.
 

No comments:

Post a Comment