Downloading a Cost or Usage Report

Download a cost or usage report.

  • To download a cost or usage report:
    1. Open the navigation menu and click Billing & Cost Management. Under Cost Management, click Cost and Usage Reports.
    2. Click the report you want to download from the list, and follow the browser's instructions for downloading.
  • Use the following example Object Storage list and get commands to list and download cost or usage reports. These are the OCI CLI command equivalents to the Python SDK code example shown in the API task.

    List all cost and usage reports from the Oracle managed namespace "bling" and bucket (customer tenancy OCID):

    oci os object list --namespace-name bling --bucket-name {customer_tenancy_ocid} --all

    List two cost and usage reports from the Oracle managed namespace "bling" and bucket (customer tenancy OCID):

    oci os object list --namespace-name bling --bucket-name {customer_tenancy_ocid} --limit 2

    All cost reports from the Oracle managed namespace "bling" and bucket (customer tenancy OCID) with the prefix "reports/cost-csv":

    oci os object list --namespace-name bling --bucket-name {customer_tenancy_ocid} --prefix reports/cost-csv --all

    All usage reports from the Oracle managed namespace "bling" and bucket (customer tenancy OCID) with the prefix "reports/usage-csv":

    oci os object list --namespace-name bling --bucket-name {customer_tenancy_ocid} --prefix reports/usage-csv --all

    Download a cost or usage report:

    oci os object get --namespace-name bling --bucket-name {customer_tenancy_ocid}  --name reports/usage-csv/{report_name}.csv.gz --file {local_report_name}.csv.gz

    For a complete list of parameters and variable options for CLI commands, see the Command Line Reference.

  • For information about using the API and signing requests, see REST API documentation and Security Credentials. For information about SDKs, see SDKs and the CLI.

    To download a cost or usage report, use the Object Storage APIs. The reports are stored in the tenancy's home region. The Object Storage namespace used for the reports is bling. The bucket name is the tenancy OCID.

    The following example shows how to download a cost report, usage report (or both) using a Python script:

    Note

    This example has a specific tenancy OCID, because the reports are stored in an Oracle-owned Object Storage bucket hosted by Oracle Cloud Infrastructure, and not a customer's tenancy.
    import oci
    import os
                                   
    # This script downloads all of the cost, usage, (or both) reports for a tenancy (specified in the config file).
    #
    # Pre-requisites: Create an IAM policy to endorse users in your tenancy to read cost reports from the OCI tenancy.
    #
    # Example policy:
    # define tenancy reporting as ocid1.tenancy.oc1..aaaaaaaaned4fkpkisbwjlr56u7cj63lf3wffbilvqknstgtvzub7vhqkggq
    # endorse group <group_name> to read objects in tenancy reporting
    #
    # Note - The only value you need to change is the <group_name> with your own value. Do not change the OCID in the first statement.
                                   
    reporting_namespace = 'bling'
                                   
    # Download all usage and cost files. You can comment out based on the specific need:
    prefix_file = ""                     #  For cost and usage files
    # prefix_file = "reports/cost-csv"   #  For cost
    # prefix_file = "reports/usage-csv"  #  For usage
                                   
    # Update these values
    destintation_path = 'downloaded_reports'
                                   
    # Make a directory to receive reports
    if not os.path.exists(destintation_path):
        os.mkdir(destintation_path)
                                   
    # Get the list of reports
    config = oci.config.from_file(oci.config.DEFAULT_LOCATION, oci.config.DEFAULT_PROFILE)
    reporting_bucket = config['tenancy']
    object_storage = oci.object_storage.ObjectStorageClient(config)
    report_bucket_objects = oci.pagination.list_call_get_all_results(object_storage.list_objects, reporting_namespace, reporting_bucket, prefix=prefix_file)                          
    for o in report_bucket_objects.data.objects:
        print('Found file ' + o.name)
        object_details = object_storage.get_object(reporting_namespace, reporting_bucket, o.name)
        filename = o.name.rsplit('/', 1)[-1]
                                   
        with open(destintation_path + '/' + filename, 'wb') as f:
            for chunk in object_details.data.raw.stream(1024 * 1024, decode_content=False):
                f.write(chunk)
                                   
        print('----> File ' + o.name + ' Downloaded')