diff --git a/storage/s3-sdk/README.rst b/storage/s3-sdk/README.rst new file mode 100644 index 00000000000..9eaddcc6a5f --- /dev/null +++ b/storage/s3-sdk/README.rst @@ -0,0 +1,84 @@ +.. This file is automatically generated. Do not edit this file directly. + +Google Cloud Storage Python Samples +=============================================================================== + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=storage/s3-sdk/README.rst + + +This directory contains samples for Google Cloud Storage. `Google Cloud Storage`_ provides support for S3 API users through the GCS XML API. +Learn more about migrating data from S3 to GCS at https://cloud.google.com/storage/docs/migrating. + + + + +.. _Google Cloud Storage: https://cloud.google.com/storage/docs + +Setup +------------------------------------------------------------------------------- + + +Install Dependencies +++++++++++++++++++++ + +#. Clone python-docs-samples and change directory to the sample directory you want to use. + + .. code-block:: bash + + $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git + +#. 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. + + .. _Python Development Environment Setup Guide: + https://cloud.google.com/python/setup + +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. + + .. code-block:: bash + + $ virtualenv env + $ source env/bin/activate + +#. Install the dependencies needed to run the samples. + + .. code-block:: bash + + $ pip install -r requirements.txt + +.. _pip: https://pip.pypa.io/ +.. _virtualenv: https://virtualenv.pypa.io/ + +Samples +------------------------------------------------------------------------------- + +List GCS Buckets using S3 SDK ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=storage/s3-sdk/list_gcs_buckets.py,storage/s3-sdk/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python list_gcs_buckets.py + + usage: list_gcs_buckets.py [-h] google_access_key_id google_access_key_secret + + positional arguments: + google_access_key_id Your Cloud Storage HMAC Access Key ID. + google_access_key_secret + Your Cloud Storage HMAC Access Key Secret. + + optional arguments: + -h, --help show this help message and exit + + + + + +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/storage/s3-sdk/README.rst.in b/storage/s3-sdk/README.rst.in new file mode 100644 index 00000000000..a5031c33e3f --- /dev/null +++ b/storage/s3-sdk/README.rst.in @@ -0,0 +1,23 @@ +# This file is used to generate README.rst + +product: + name: Google Cloud Storage + short_name: Cloud Storage + url: https://cloud.google.com/storage/docs + description: > + `Google Cloud Storage`_ provides support for S3 API users through the + GCS XML API. + + Learn more about migrating data from S3 to GCS at https://cloud.google.com/storage/docs/migrating. + +setup: +- install_deps + +samples: +- name: List GCS Buckets using S3 SDK + file: list_gcs_buckets.py + show_help: true + +cloud_client_library: false + +folder: storage/s3-sdk diff --git a/storage/s3-sdk/list_gcs_buckets.py b/storage/s3-sdk/list_gcs_buckets.py new file mode 100644 index 00000000000..8f3373db1dc --- /dev/null +++ b/storage/s3-sdk/list_gcs_buckets.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +# Copyright 2019 Google, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +# [START storage_s3_sdk_list_buckets] +import boto3 + + +def list_gcs_buckets(google_access_key_id, google_access_key_secret): + """Lists all GCS buckets using boto3 SDK""" + # Create a new client and do the following: + # 1. Change the endpoint URL to use the + # Google Cloud Storage XML API endpoint. + # 2. Use Cloud Storage HMAC Credentials. + client = boto3.client("s3", region_name="auto", + endpoint_url="https://storage.googleapis.com", + aws_access_key_id=google_access_key_id, + aws_secret_access_key=google_access_key_secret) + + # Call GCS to list current buckets + response = client.list_buckets() + + # Print bucket names + print("Buckets:") + for bucket in response["Buckets"]: + print(bucket["Name"]) +# [END storage_s3_sdk_list_buckets] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("google_access_key_id", + help="Your Cloud Storage HMAC Access Key ID.") + parser.add_argument("google_access_key_secret", + help="Your Cloud Storage HMAC Access Key Secret.") + + args = parser.parse_args() + + list_gcs_buckets(google_access_key_id=args.google_access_key_id, + google_access_key_secret=args.google_access_key_secret) diff --git a/storage/s3-sdk/list_gcs_buckets_test.py b/storage/s3-sdk/list_gcs_buckets_test.py new file mode 100644 index 00000000000..1d29f23a389 --- /dev/null +++ b/storage/s3-sdk/list_gcs_buckets_test.py @@ -0,0 +1,28 @@ +# Copyright 2019 Google, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import list_gcs_buckets + +BUCKET = os.environ["GOOGLE_CLOUD_PROJECT_S3_SDK"] +KEY_ID = os.environ["STORAGE_HMAC_ACCESS_KEY_ID"] +SECRET_KEY = os.environ["STORAGE_HMAC_ACCESS_SECRET_KEY"] + + +def test_list_blobs(capsys): + list_gcs_buckets.list_gcs_buckets(google_access_key_id=KEY_ID, + google_access_key_secret=SECRET_KEY) + out, _ = capsys.readouterr() + assert BUCKET in out diff --git a/storage/s3-sdk/requirements.txt b/storage/s3-sdk/requirements.txt new file mode 100644 index 00000000000..d65147f7ed3 --- /dev/null +++ b/storage/s3-sdk/requirements.txt @@ -0,0 +1 @@ +boto3==1.9.38 \ No newline at end of file diff --git a/testing/secrets.tar.enc b/testing/secrets.tar.enc index 0e4b3910904..59f9e624f90 100644 Binary files a/testing/secrets.tar.enc and b/testing/secrets.tar.enc differ