Run C++ Examples in Cloud Shell

Learn how to compile and run the C++ client examples in Cloud Shell using micromamba and Conda

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .

  4. To initialize the gcloud CLI, run the following command:

    gcloud  
    init
  5. Create or select a Google Cloud project .

    Roles required to select or create a project

    • Select a project : Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project : To create a project, you need the Project Creator role ( roles/resourcemanager.projectCreator ), which contains the resourcemanager.projects.create permission. Learn how to grant roles .
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID 
      

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID 
      

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project .

  7. Enable the Cloud Storage API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin ), which contains the serviceusage.services.enable permission. Learn how to grant roles .

    gcloud  
    services  
     enable 
      
    storage.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .

  10. To initialize the gcloud CLI, run the following command:

    gcloud  
    init
  11. Create or select a Google Cloud project .

    Roles required to select or create a project

    • Select a project : Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project : To create a project, you need the Project Creator role ( roles/resourcemanager.projectCreator ), which contains the resourcemanager.projects.create permission. Learn how to grant roles .
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID 
      

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID 
      

      Replace PROJECT_ID with your Google Cloud project name.

  12. Verify that billing is enabled for your Google Cloud project .

  13. Enable the Cloud Storage API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin ), which contains the serviceusage.services.enable permission. Learn how to grant roles .

    gcloud  
    services  
     enable 
      
    storage.googleapis.com
  14. Create a Cloud Storage bucket run the examples:

    1. Ensure that you have the Storage Admin role ( roles/storage.admin ) on the project. Learn how to grant roles .
    2. Create a Cloud Storage bucket:
      gcloud  
      storage  
      buckets  
      create  
      gs:// BUCKET_NAME 
      
      Replace BUCKET_NAME with a bucket name that meets the bucket naming requirements .
  15. Ensure that the following tools are installed:

Install pre-compiled C++ Client Libraries in Cloud Shell

  1. In Cloud Shell, install micromamba :

     curl  
    -fsSL  
    https://micro.mamba.pm/api/micromamba/linux-64/latest  
     | 
      
    sudo  
    tar  
    -C  
    /usr  
    -xj  
    bin/micromamba 
    
  2. Add micromamba hooks to the current shell:

      eval 
      
     " 
     $( 
    /usr/bin/micromamba  
    shell  
    hook  
    -s  
    posix ) 
     " 
     
    
  3. Use micromamba to create a Conda environment with the necessary development tools and libraries:

     micromamba  
    create  
    -y  
    -n  
    base  
    -c  
    conda-forge  
    cmake  
    ninja  
    cxx-compiler  
    google-cloud-cpp  
    libgoogle-cloud 
    
  4. Activate the new conda environment:

     micromamba  
    activate 
    

Checkout and compile the "Storage Quickstart" example

  1. Checkout the C++ Client Library source code, including the quickstart examples for all services:

     git  
    clone  
    --depth  
     1 
      
    https://github.com/googleapis/google-cloud-cpp 
    
  2. Configure CMake to compile the "Storage Quickstart" example:

     cmake  
    -S  
    google-cloud-cpp/google/cloud/storage/quickstart  
    -B  
    .build 
    
  3. Compile the example:

     cmake  
    --build  
    .build 
    
  4. Run the example:

     .build/quickstart  
     BUCKET_NAME 
     
    
  5. After running this example, you'll see a message similar to Successfully created object ... followed by data specific to your object and ending with a line that shows Hello World! .

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Google Cloud project with the resources.

  1. Delete the object created by the example:

     gcloud  
    storage  
    rm  
    gs:// BUCKET_NAME 
    /quickstart.txt 
    
  2. Delete the bucket:

     gcloud  
    storage  
    buckets  
    delete  
    gs:// BUCKET_NAME 
     
    

What's next

Design a Mobile Site
View Site in Mobile | Classic
Share by: