Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
README.rst
README.rst.in
acl.py
acl_test.py
bucket_lock.py
bucket_lock_test.py
encryption.py
encryption_test.py
hmac_samples.py
hmac_samples_test.py
iam.py
iam_test.py
notification_polling.py
notification_polling_test.py
quickstart.py
quickstart_test.py
requester_pays.py
requester_pays_test.py
requirements.txt
snippets.py
snippets_test.py
uniform_bucket_level_access.py
uniform_bucket_level_access_test.py

README.rst

Google Cloud Storage Python Samples

https://gstatic.com/cloudssh/images/open-btn.png

This directory contains samples for Google Cloud Storage. Google Cloud Storage allows world-wide storage and retrieval of any amount of data at any time.

Setup

Authentication

This sample requires you to have authentication setup. Refer to the Authentication Getting Started Guide for instructions on setting up credentials for applications.

Install Dependencies

  1. Clone python-docs-samples and change directory to the sample directory you want to use.

    $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
  2. Install pip and virtualenv if you do not already have them. You may want to refer to the Python Development Environment Setup Guide for Google Cloud Platform for instructions.

  3. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.

    $ virtualenv env
    $ source env/bin/activate
  4. Install the dependencies needed to run the samples.

    $ pip install -r requirements.txt

Samples

Quickstart

https://gstatic.com/cloudssh/images/open-btn.png

To run this sample:

$ python quickstart.py

Snippets

https://gstatic.com/cloudssh/images/open-btn.png

To run this sample:

$ python snippets.py

usage: snippets.py [-h]
                   {list-buckets,create-bucket,delete-bucket,get-bucket-labels,add-bucket-label,remove-bucket-label,list,bucket-metadata,list-with-prefix,upload,enable-default-kms-key,upload-with-kms-key,download,delete,metadata,make-public,signed-url,signed-url-download-v4,signed-url-upload-v4,rename,copy}
                   ...

This application demonstrates how to perform basic operations on blobs
(objects) in a Google Cloud Storage bucket.

For more information, see the README.md under /storage  and the documentation
at https://cloud.google.com/storage/docs.

positional arguments:
  {list-buckets,create-bucket,delete-bucket,get-bucket-labels,add-bucket-label,remove-bucket-label,list,bucket-metadata,list-with-prefix,upload,enable-default-kms-key,upload-with-kms-key,download,delete,metadata,make-public,signed-url,signed-url-download-v4,signed-url-upload-v4,rename,copy}
    list-buckets        Lists all buckets.
    create-bucket       Creates a new bucket.
    delete-bucket       Deletes a bucket. The bucket must be empty.
    get-bucket-labels   Prints out a bucket's labels.
    add-bucket-label    Add a label to a bucket.
    remove-bucket-label
                        Remove a label from a bucket.
    list                Lists all the blobs in the bucket.
    bucket-metadata     Prints out a bucket's metadata.
    list-with-prefix    Lists all the blobs in the bucket that begin with the
                        prefix. This can be used to list all blobs in a
                        "folder", e.g. "public/". The delimiter argument can
                        be used to restrict the results to only the "files" in
                        the given "folder". Without the delimiter, the entire
                        tree under the prefix is returned. For example, given
                        these blobs: /a/1.txt /a/b/2.txt If you just specify
                        prefix = '/a', you'll get back: /a/1.txt /a/b/2.txt
                        However, if you specify prefix='/a' and delimiter='/',
                        you'll get back: /a/1.txt
    upload              Uploads a file to the bucket.
    enable-default-kms-key
                        Sets a bucket's default KMS key.
    upload-with-kms-key
                        Uploads a file to the bucket, encrypting it with the
                        given KMS key.
    download            Downloads a blob from the bucket.
    delete              Deletes a blob from the bucket.
    metadata            Prints out a blob's metadata.
    make-public         Makes a blob publicly accessible.
    signed-url          Generates a v2 signed URL for downloading a blob. Note
                        that this method requires a service account key file.
                        You can not use this if you are using Application
                        Default Credentials from Google Compute Engine or from
                        the Google Cloud SDK.
    signed-url-download-v4
                        Generates a v4 signed URL for downloading a blob. Note
                        that this method requires a service account key file.
                        You can not use this if you are using Application
                        Default Credentials from Google Compute Engine or from
                        the Google Cloud SDK.
    signed-url-upload-v4
                        Generates a v4 signed URL for uploading a blob using
                        HTTP PUT. Note that this method requires a service
                        account key file. You can not use this if you are
                        using Application Default Credentials from Google
                        Compute Engine or from the Google Cloud SDK.
    rename              Renames a blob.
    copy                Renames a blob.

optional arguments:
  -h, --help            show this help message and exit

Access Control Lists

https://gstatic.com/cloudssh/images/open-btn.png

To run this sample:

$ python acl.py

usage: acl.py [-h]
              {print-bucket-acl,print-bucket-acl-for-user,add-bucket-owner,remove-bucket-owner,add-bucket-default-owner,remove-bucket-default-owner,print-blob-acl,print-blob-acl-for-user,add-blob-owner,remove-blob-owner}
              ...

This application demonstrates how to manage access control lists (acls) in
Google Cloud Storage.

For more information, see the README.md under /storage and the documentation
at https://cloud.google.com/storage/docs/encryption.

positional arguments:
  {print-bucket-acl,print-bucket-acl-for-user,add-bucket-owner,remove-bucket-owner,add-bucket-default-owner,remove-bucket-default-owner,print-blob-acl,print-blob-acl-for-user,add-blob-owner,remove-blob-owner}
    print-bucket-acl    Prints out a bucket's access control list.
    print-bucket-acl-for-user
                        Prints out a bucket's access control list.
    add-bucket-owner    Adds a user as an owner on the given bucket.
    remove-bucket-owner
                        Removes a user from the access control list of the
                        given bucket.
    add-bucket-default-owner
                        Adds a user as an owner in the given bucket's default
                        object access control list.
    remove-bucket-default-owner
                        Removes a user from the access control list of the
                        given bucket's default object access control list.
    print-blob-acl      Prints out a blob's access control list.
    print-blob-acl-for-user
                        Prints out a blob's access control list for a given
                        user.
    add-blob-owner      Adds a user as an owner on the given blob.
    remove-blob-owner   Removes a user from the access control list of the
                        given blob in the given bucket.

optional arguments:
  -h, --help            show this help message and exit

Customer-Supplied Encryption

https://gstatic.com/cloudssh/images/open-btn.png

To run this sample:

$ python encryption.py

usage: encryption.py [-h] {generate-encryption-key,upload,download,rotate} ...

This application demonstrates how to upload and download encrypted blobs
(objects) in Google Cloud Storage.

Use `generate-encryption-key` to generate an example key:

    python encryption.py generate-encryption-key

Then use the key to upload and download files encrypted with a custom key.

For more information, see the README.md under /storage and the documentation
at https://cloud.google.com/storage/docs/encryption.

positional arguments:
  {generate-encryption-key,upload,download,rotate}
    generate-encryption-key
                        Generates a 256 bit (32 byte) AES encryption key and
                        prints the base64 representation. This is included for
                        demonstration purposes. You should generate your own
                        key. Please remember that encryption keys should be
                        handled with a comprehensive security policy.
    upload              Uploads a file to a Google Cloud Storage bucket using
                        a custom encryption key. The file will be encrypted by
                        Google Cloud Storage and only retrievable using the
                        provided encryption key.
    download            Downloads a previously-encrypted blob from Google
                        Cloud Storage. The encryption key provided must be the
                        same key provided when uploading the blob.
    rotate              Performs a key rotation by re-writing an encrypted
                        blob with a new encryption key.

optional arguments:
  -h, --help            show this help message and exit

Bucket Lock

https://gstatic.com/cloudssh/images/open-btn.png

To run this sample:

$ python bucket_lock.py

usage: bucket_lock.py [-h]
                      {set-retention-policy,remove-retention-policy,lock-retention-policy,get-retention-policy,set-temporary-hold,release-temporary-hold,set-event-based-hold,release-event-based-hold,enable-default-event-based-hold,disable-default-event-based-hold,get-default-event-based-hold}
                      ...

positional arguments:
  {set-retention-policy,remove-retention-policy,lock-retention-policy,get-retention-policy,set-temporary-hold,release-temporary-hold,set-event-based-hold,release-event-based-hold,enable-default-event-based-hold,disable-default-event-based-hold,get-default-event-based-hold}
    set-retention-policy
                        Defines a retention policy on a given bucket
    remove-retention-policy
                        Removes the retention policy on a given bucket
    lock-retention-policy
                        Locks the retention policy on a given bucket
    get-retention-policy
                        Gets the retention policy on a given bucket
    set-temporary-hold  Sets a temporary hold on a given blob
    release-temporary-hold
                        Releases the temporary hold on a given blob
    set-event-based-hold
                        Sets a event based hold on a given blob
    release-event-based-hold
                        Releases the event based hold on a given blob
    enable-default-event-based-hold
                        Enables the default event based hold on a given bucket
    disable-default-event-based-hold
                        Disables the default event based hold on a given
                        bucket
    get-default-event-based-hold
                        Gets the default event based hold on a given bucket

optional arguments:
  -h, --help            show this help message and exit

Uniform Bucket Level Access

https://gstatic.com/cloudssh/images/open-btn.png

To run this sample:

$ python uniform_bucket_level_access.py

usage: uniform_bucket_level_access.py [-h]
                             {enable-uniform-bucket-level-access,disable-uniform-bucket-level-access,get-uniform-bucket-level-access}
                             ...

positional arguments:
  {enable-uniform-bucket-level-access,disable-uniform-bucket-level-access,get-uniform-bucket-level-access}
    enable-uniform-bucket-level-access
                        Enable uniform bucket-level access  for a bucket
    disable-uniform-bucket-level-access
                        Disable uniform bucket-level access for a bucket
    get-uniform-bucket-level-access
                        Get uniform bucket-level access for a bucket

optional arguments:
  -h, --help            show this help message and exit

Notification Polling

https://gstatic.com/cloudssh/images/open-btn.png

To run this sample:

$ python notification_polling.py

usage: notification_polling.py [-h] project subscription

This application demonstrates how to poll for GCS notifications from a
Cloud Pub/Sub subscription, parse the incoming message, and acknowledge the
successful processing of the message.

This application will work with any subscription configured for pull rather
than push notifications. If you do not already have notifications configured,
you may consult the docs at
https://cloud.google.com/storage/docs/reporting-changes or follow the steps
below:

1. First, follow the common setup steps for these snippets, specically
   configuring auth and installing dependencies. See the README's "Setup"
   section.

2. Activate the Google Cloud Pub/Sub API, if you have not already done so.
   https://console.cloud.google.com/flows/enableapi?apiid=pubsub

3. Create a Google Cloud Storage bucket:
   $ gsutil mb gs://testbucket

4. Create a Cloud Pub/Sub topic and publish bucket notifications there:
   $ gsutil notification create -f json -t testtopic gs://testbucket

5. Create a subscription for your new topic:
   $ gcloud beta pubsub subscriptions create testsubscription --topic=testtopic

6. Run this program:
   $ python notification_polling.py my-project-id testsubscription

7. While the program is running, upload and delete some files in the testbucket
   bucket (you could use the console or gsutil) and watch as changes scroll by
   in the app.

positional arguments:
  project       The ID of the project that owns the subscription
  subscription  The ID of the Pub/Sub subscription

optional arguments:
  -h, --help    show this help message and exit

Service Account HMAC Keys

https://gstatic.com/cloudssh/images/open-btn.png

To run this sample:

$ python hmac_samples.py

The client library

This sample uses the Google Cloud Client Library for Python. You can read the documentation for more details on API usage and use GitHub to browse the source and report issues.

You can’t perform that action at this time.