Connecting to Cloud Storage buckets

Google Cloud Storage is a flexible, scalable, and durable storage option for your virtual machine instances. You can write files to Cloud Storage buckets from almost anywhere, so you can use buckets as common storage between your instances, Google App Engine, your on-premises systems, and other cloud services.

If Cloud Storage buckets do not meet your performance and latency requirements, you can use Cloud Storage in combination with other instance storage options.

Before you begin

Writing data to Google Cloud Storage buckets

Write files to Google Cloud Storage buckets through the gsutil tool or the Cloud Storage API.

gsutil


By default, the gsutil tool command is installed on all virtual machine instances that use supported operating systems.

  1. Connect to your instance through SSH. For this example, go to the VM instances page and click the SSH button next to the instance.

  2. If you have never used gsutil on this instance before, use the gcloud tool to set up credentials.

    gcloud init

    Alternatively, if your instance is configured to use a service account with a Google Cloud Storage scope, you can skip this step.

  3. Use the gsutil tool to create buckets and write data to those buckets. Optionally, you can also stream data to Cloud Storage.

API


If you configured your instance to use a service account with a Google Cloud Storage scope, you can use the Cloud Storage APIs to write data to Cloud Storage buckets.

  1. Connect to your instance through SSH. For this example, go to the VM instances page and click the SSH button next the instance.

  2. Install and configure a client library for your preferred language.

  3. Follow one of the insert examples and include code in your application that writes a file to a Cloud Storage bucket.

Mounting a bucket as a file system

You can use the Google Cloud Storage FUSE tool to mount a Cloud Storage bucket to your Compute Engine instance. The mounted bucket behaves similarly to a persistent disk even though Cloud Storage buckets are object storage.

Before you can mount a bucket to your instance must have access to Google Cloud Storage through one of the following authentication methods:

  • A service account with a read or write access to Google Cloud Storage.
  • Credentials that you created on the instance through the gcloud auth login command.

To create and mount a bucket:

  1. Connect to your instance through SSH. For this example, go to the VM instances page and click the SSH button next the instance where you want to mount the Cloud Storage bucket.

  2. If you have never used gsutil on this instance before, use the gcloud tool to set up credentials.

    gcloud init

    Alternatively, if your instance is configured to use a service account with read and write access from a Google Cloud Storage scope, you can skip this step.

  3. After you connect, install the gcsfuse tool on the instance.

  4. Create a bucket using the gsutil tool, which is installed by default on your instance. Specify a bucket location and your own unique bucket name.

    gsutil mb -c STANDARD -l US gs://[BUCKET_NAME]

  5. Create a directory where you can mount the bucket.

    sudo mkdir /mnt/gcs-bucket

  6. Give all users write access to the directory.

    sudo chmod a+w /mnt/gcs-bucket

  7. Mount the bucket to your instance.

    gcsfuse [BUCKET_NAME] /mnt/gcs-bucket
    
    Using mount point: /mnt/gcs-bucket
    Opening connection...
    Opening bucket...
    Mounting file system...
    File system has been successfully mounted.

  8. Verify that the mounted bucket functions as intended. Create a hello.txt file in the bucket and ensure that it is actually in Cloud Storage with the gsutil ls command:

    echo 'Hello, World!' > /mnt/gcs-bucket/hello.txt

    gsutil ls -r gs://[BUCKET_NAME]
    gs://[BUCKET_NAME]/hello.txt

To unmount the bucket, use the same umount command that you use to unmount persistent disks.

$ sudo umount /mnt/gcs-bucket

Send feedback about...

Compute Engine Documentation