Saturday, August 2, 2025

Monitor Oracle Cloud Object Storage Space Usage via OCI CLI

 

๐Ÿ”น Introduction

Managing storage efficiently is essential, especially when dealing with large backups or logs in Oracle Cloud Infrastructure (OCI). While the OCI Console provides visibility, it’s not always ideal for scripting or automation. In this post, we’ll explore how to use the OCI Command Line Interface (CLI) to monitor the space usage of your Object Storage buckets.

๐Ÿ”น What You’ll Learn

  • How to install and configure OCI CLI

  • List all buckets in a compartment

  • Retrieve storage usage details per bucket

  • Generate a summary report

  • Optional: Automate and send email alerts

๐Ÿ”น Step 1: Install & Configure OCI CLI

If you haven’t already installed the CLI:

bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"

After installation, run:

oci setup config

You'll be prompted to enter:

  • Tenancy OCID

  • User OCID

  • Compartment OCID

  • Region

  • API key (which it generates)

๐Ÿ”น Step 2: List Buckets in Your Compartment

oci os bucket list --compartment-id <your_compartment_ocid> --all

Output will show all buckets, their names, and creation timestamps.

๐Ÿ”น Step 3: Check Storage Usage per Bucket

To get the total size of a bucket (in bytes):

oci os object list --bucket-name <bucket_name> --query "sum([*].size)" --raw-output

๐Ÿ”ธ Sample Shell Script to Loop All Buckets:

#!/bin/bash COMPARTMENT_ID="<your_compartment_ocid>" BUCKETS=$(oci os bucket list --compartment-id $COMPARTMENT_ID --query "data[*].name" --raw-output) echo "Storage Report for Buckets in OCI:" echo "----------------------------------" for bucket in $BUCKETS; do SIZE=$(oci os object list --bucket-name "$bucket" --query "sum([*].size)" --raw-output) SIZE_MB=$(echo "scale=2; $SIZE / 1048576" | bc) echo "Bucket: $bucket - Size: $SIZE_MB MB" done

๐Ÿ”น Step 4: Automate Storage Monitoring (Optional)

You can run the script daily and email the output:

crontab -e

Add this line to run at 8 AM daily:

0 8 * * * /home/opc/check_oci_storage.sh | mail -s "OCI Bucket Usage Report" your@email.com

๐Ÿ”น Tips for DBAs

  • Use Storage Class filters (Standard vs Archive) to reduce costs.

  • Enable Lifecycle Policies to auto-delete old files.

  • Avoid keeping RMAN backups longer than needed.

๐Ÿ”น Vignesh’s Tip

For temporary one-time uploads (like logs or exported tables), use Pre-Authenticated Requests (PARs). They give you a URL without needing to expose keys or public access.

๐Ÿ”น Conclusion

Monitoring storage usage with OCI CLI not only helps you manage cost but also keeps your backup strategy efficient. For DBAs and DevOps engineers working in hybrid environments, this approach enables easy scripting and reporting.

No comments:

Post a Comment

Auto Shutdown and Restart of Oracle DB Systems in OCI Using Functions

  ๐Ÿ”น Introduction Oracle Cloud Infrastructure (OCI) Database Systems incur compute costs even when idle. If you're running non-producti...