├── .gitignore ├── LICENSE ├── README.md ├── gcloudcomputeengine ├── README.md ├── computeenginehelper.py └── computeengineinstances.py ├── gcloudfunctiongcscopy ├── README.md ├── main.py └── requirements.txt ├── gcloudfunctiongcsevent ├── README.md ├── main.py └── requirements.txt ├── gcloudfunctiongcsmove ├── README.md ├── main.py └── requirements.txt ├── gcloudfunctionhttprequest ├── README.md ├── main.py └── requirements.txt ├── gcloudfunctionpubsubevent ├── README.md ├── main.py └── requirements.txt ├── gcloudstoragecopy ├── README.md └── cloudstoragecopy.py ├── gcloudstoragecreate ├── README.md └── cloudstoragecreate.py ├── gcloudstoragedelete ├── README.md └── cloudstoragedelete.py ├── gcloudstoragedeleteobject ├── README.md └── cloudstoragedeleteobject.py ├── gcloudstoragedownload ├── README.md └── cloudstoragedownload.py ├── gcloudstoragelist ├── README.md └── cloudstoragelist.py ├── gcloudstoragelistall ├── README.md └── cloudstoragelistall.py ├── gcloudstoragelistallparam ├── README.md └── cloudstoragelistallparam.py ├── gcloudstoragemove ├── README.md └── cloudstoragemove.py └── gcloudstorageupload ├── README.md └── cloudstorageupload.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | 106 | # IntelliJ, PyCharm 107 | .idea/ 108 | *.iml 109 | *.iws 110 | target/ 111 | out/ 112 | 113 | # Visual Studio Code 114 | .vscode/ 115 | 116 | # Package Files 117 | *.zip 118 | *.tar 119 | *.tar.gz 120 | *.rar 121 | 122 | # Mac 123 | .DS_Store 124 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Alfonso Fernandez-Barandiaran 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python examples on Google Cloud Platform (GCP) 2 | 3 | This repo contains Python code examples on Google Cloud Platform (GCP). 4 | 5 | These examples show how to use Python 3 and Google Python Client Libraries in order to manage services on Google Cloud Platform. These use Google Cloud Python Client Library or Google API Python Client Library. 6 | 7 | Google Python Client Libraries allow Python developers to write software that makes use of Google Cloud services like Compute Engine and Cloud Storage. 8 | 9 | Google Python Client Libraries offers two different styles of API: 10 | 11 | * Google Cloud Client Library for Python: It is the recommended option for accessing Cloud APIs programmatically, where available. It is an idiomatic, intuitive, and natural way for Python developers to integrate with Google Cloud Platform services, like Cloud Datastore and Cloud Storage. 12 | * Google API Client Library for Python: A number of Google Cloud APIs do not yet have Google Cloud Client Libraries available in Python. If you want to use one of these APIs and there is no Cloud Client Library for Python, you can still use an older version of the client libraries called Google API Client Libraries. 13 | 14 | ## Quick start 15 | 16 | You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 17 | 18 | The code for the samples is contained in individual folders on this repository. 19 | 20 | For instructions on running the code, please consult the README in each folder. 21 | 22 | This is the list of examples: 23 | 24 | **Compute - Compute Engine:** 25 | 26 | * [gcloudcomputeengine](/gcloudcomputeengine) - Compute Engine VM instances: Example of how to handle Compute Engine VM instances. It uses the Google API Client Library. 27 | 28 | **Compute - Cloud Functions:** 29 | 30 | * [gcloudfunctionpubsubevent](/gcloudfunctionpubsubevent) - Cloud Function Pub/Sub Event: Example of how to handle a Google Cloud Function that sends information about a Cloud Pub/Sub event that depends on the input to the function log. 31 | * [gcloudfunctionhttprequest](/gcloudfunctionhttprequest) - Google Cloud Function HTTP Request: Example of how to handle a Google Cloud Function that responds to an HTTP request. 32 | * [gcloudfunctiongcsevent](/gcloudfunctiongcsevent) - Google Cloud Function Cloud Storage Event: Example of how to handle a Google Cloud Function that sends information to the function log about an object when it appears in a Cloud Storage bucket. 33 | * [gcloudfunctiongcscopy](/gcloudfunctiongcscopy) - Google Cloud Function Cloud Storage Copy: Example of how to handle an Google Cloud Function and copy an object when it appears in a Cloud Storage bucket to another Cloud Storage bucket. 34 | * [gcloudfunctiongcsmove](/gcloudfunctiongcsmove) - Google Cloud Function Cloud Storage Move: Example of how to handle an Google Cloud Function function and move an object when it appears in a Cloud Storage bucket to another Cloud Storage bucket. 35 | 36 | **Storage - Cloud Storage:** 37 | 38 | * [gcloudstoragecreate](/gcloudstoragecreate) - Google Cloud Storage Create: Example of how to handle Cloud Storage buckets and create a new Google Storage bucket in a Google Cloud Project. It uses the Google Cloud Client Library. 39 | * [gcloudstoragedelete](/gcloudstoragedelete) - Google Cloud Storage Delete: Example of how to handle Cloud Storage buckets and delete a Google Storage bucket in a Google Cloud Project. It uses the Google Cloud Client Library. 40 | * [gcloudstoragelist](/gcloudstoragelist) - Google Cloud Storage List: Example of how to handle Cloud Storage buckets and list information about the objects in a Cloud Storage bucket in a Google Cloud Project. It uses the Google Cloud Client Library. 41 | * [gcloudstoragelistall](/gcloudstoragelistall) - Google Cloud Storage List All: Example of how to handle Cloud Storage buckets and list information about all Cloud Storage buckets and the objects that they contain in a Google Cloud Project. It uses the Google Cloud Client Library. The credentials are taken from GOOGLE_APPLICATION_CREDENTIALS environment variable. 42 | * [gcloudstoragelistallparam](/gcloudstoragelistallparam) - Google Cloud Storage List All Parameters: Example of how to handle Cloud Storage buckets and list all Cloud Storage buckets and the files they contain in a Google Cloud Project. The user must provide the credentials using the application parameters. 43 | * [gcloudstorageupload](/gcloudstorageupload) - Google Cloud Storage Upload: Example of how to handle Cloud Storage buckets and upload a local file to a Cloud Storage bucket in a Google Cloud Project. It uses the Google Cloud Client Library. 44 | * [gcloudstoragedownload](/gcloudstoragedownload) - Google Cloud Storage Download: Example of how to handle Cloud Storage buckets and download an object in a Cloud Storage bucket in a Google Cloud Project to a local file. It uses the Google Cloud Client Library. 45 | * [gcloudstoragedeleteobject](/gcloudstoragedeleteobject) - Google Cloud Storage Delete Object: Example of how to handle Cloud Storage buckets and delete an object in a Google Storage bucket in a Google Cloud Project. It uses the Google Cloud Client Library. 46 | * [gcloudstoragecopy](/gcloudstoragecopy) - Google Cloud Storage Copy: Example of how to handle Cloud Storage buckets and copy an object from a Google Storage bucket to another Google Storage bucket in a Google Cloud Project. It uses the Google Cloud Client Library. 47 | * [gcloudstoragemove](/gcloudstoragemove) - Google Cloud Storage Move: Example of how to handle Cloud Storage buckets and move an object from a Google Storage bucket to another Google Storage bucket in a Google Cloud Project. It uses the Google Cloud Client Library. 48 | 49 | ## License 50 | 51 | This code is released under the MIT License. See LICENSE file. 52 | -------------------------------------------------------------------------------- /gcloudcomputeengine/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Compute Engine VM instances Python example 2 | 3 | This folder contains a Python application example that handles Compute Engine VM instances on Google Cloud Platform (GCP). 4 | 5 | ## Requirements 6 | 7 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 8 | 9 | * You can install and use the Google Cloud SDK. 10 | 11 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 12 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 13 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 14 | 15 | * The code was written for: 16 | 17 | * Python 3 18 | * Google API Python Client Library 19 | 20 | * Install the Google API Python Client Library. 21 | 22 | Install the latest Google API Python Client Library release via pip: 23 | 24 | ```bash 25 | pip install google-api-python-client 26 | ``` 27 | 28 | ## Using the code 29 | 30 | * Configure your Google Cloud access keys. 31 | 32 | The Google Cloud client library for Java allows you to use several authentication schemes. 33 | 34 | The application uses Application Default Credentials through a JSON service account key for authenticating. 35 | 36 | The credentials are taken from `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 37 | 38 | For example: 39 | 40 | ```bash 41 | GOOGLE_APPLICATION_CREDENTIALS = /path/to/my/key.json 42 | ``` 43 | 44 | Use the [Google Cloud Platform console](http://cloud.google.com/): 45 | 46 | * Go to the Google Cloud Project. 47 | 48 | * Prepare the credentials: 49 | * Create a Service account. 50 | 51 | For example: 52 | 53 | ```bash 54 | Name: gcloud-java-examples 55 | Role: Owner 56 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 57 | ``` 58 | 59 | * Create a key as a JSON file and download it. 60 | 61 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 62 | 63 | * Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your Operating System with the path of your JSON service account key file. 64 | 65 | * Run the code. 66 | 67 | Run application: 68 | 69 | ```bash 70 | python computeengineinstances.py 71 | ``` 72 | 73 | You can select an option in the menu in order to run every command: 74 | 75 | * 1 = List all VM instances 76 | * 2 = Create VM instance 77 | * 3 = List VM instance 78 | * 4 = Start VM instance 79 | * 5 = Stop VM instance 80 | * 6 = Reset instance 81 | * 7 = Delete instance 82 | 83 | * Test the application. 84 | 85 | You should see the new VM instance and modification of states with the Google Cloud console. 86 | -------------------------------------------------------------------------------- /gcloudcomputeengine/computeenginehelper.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # computeenginehelper.py 4 | # It has methods for managing Google Cloud Compute Engine VM instances. 5 | 6 | import sys 7 | import googleapiclient.discovery 8 | 9 | ZONE_NAME = 'us-east1-b' # Zone name 10 | PROJECT_NAME = 'gcloud-java-examples' # Project name 11 | IMAGE_NAME = "ubuntu-1604-lts" # Image name 12 | IMAGE_PROJECT_NAME = "ubuntu-os-cloud" # Image project name 13 | INSTANCE_TYPE = "n1-standard-1" # Instance type 14 | INSTANCE_NAME = "my-instance" # Name of the instance 15 | 16 | 17 | def list_instances(): 18 | """ 19 | List all Compute Engine VM instances associated with an Google Cloud account 20 | """ 21 | # Build and initialize the API 22 | compute = googleapiclient.discovery.build('compute', 'v1') 23 | 24 | print('Listing VM instances ...') 25 | 26 | # Describe instances 27 | response = compute.instances().list(project=PROJECT_NAME, zone=ZONE_NAME).execute() 28 | print('Instances in project "%s" and zone "%s":' % (PROJECT_NAME, ZONE_NAME)) 29 | if (response.get('items')): 30 | for instance in response['items']: 31 | print(' - Id: ' + instance['id']) 32 | print(' Name: ' + instance['name']) 33 | print(' Status: ' + instance['status']) 34 | print(' Machine type: ' + instance['machineType']) 35 | else: 36 | print('NO instances') 37 | 38 | return 39 | 40 | 41 | def create_instance(): 42 | """ 43 | Create a Compute Engine VM instance 44 | """ 45 | # Build and initialize the API 46 | compute = googleapiclient.discovery.build('compute', 'v1') 47 | 48 | print('Creating VM instance ...') 49 | 50 | # Get the latest image 51 | image_response = compute.images().getFromFamily( 52 | project=IMAGE_PROJECT_NAME, family=IMAGE_NAME).execute() 53 | source_disk_image = image_response['selfLink'] 54 | 55 | # Configure the machine 56 | #machine_type = 'zones/' + ZONE_NAME + '/machineTypes/' + INSTANCE_TYPE 57 | machine_type = 'zones/%s/machineTypes/%s' % (ZONE_NAME, INSTANCE_TYPE) 58 | 59 | config = { 60 | 'name': INSTANCE_NAME, 61 | 'machineType': machine_type, 62 | 63 | # Specify the boot disk and the image to use as a source. 64 | 'disks': [ 65 | { 66 | 'boot': True, 67 | 'autoDelete': True, 68 | 'initializeParams': { 69 | 'sourceImage': source_disk_image, 70 | } 71 | } 72 | ], 73 | 74 | # Specify a network interface with NAT to access the public 75 | # internet. 76 | 'networkInterfaces': [{ 77 | 'network': 'global/networks/default', 78 | 'accessConfigs': [ 79 | {'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'} 80 | ] 81 | }], 82 | 83 | # Allow the instance to access cloud storage and logging. 84 | 'serviceAccounts': [{ 85 | 'email': 'default', 86 | 'scopes': [ 87 | 'https://www.googleapis.com/auth/devstorage.read_write', 88 | 'https://www.googleapis.com/auth/logging.write' 89 | ] 90 | }] 91 | } 92 | 93 | response = compute.instances().insert(project=PROJECT_NAME, 94 | zone=ZONE_NAME, 95 | body=config).execute() 96 | 97 | print('Instance Id: ' + response['targetId']) 98 | 99 | return response['targetId'] 100 | 101 | 102 | def list_instance(instance_id): 103 | """ 104 | List a Compute Engine VM instance 105 | """ 106 | # Build and initialize the API 107 | compute = googleapiclient.discovery.build('compute', 'v1') 108 | 109 | print('Listing VM instance ...') 110 | print('Instance Id: ' + instance_id) 111 | 112 | # List the VM instance 113 | response = compute.instances().get(project=PROJECT_NAME, zone=ZONE_NAME, instance=INSTANCE_NAME).execute() 114 | 115 | print(' - Id: ' + response['id']) 116 | print(' Name: ' + response['name']) 117 | print(' Status: ' + response['status']) 118 | print(' Machine type: ' + response['machineType']) 119 | 120 | return 121 | 122 | 123 | def start_instance(instance_id): 124 | """ 125 | Start a Compute Engine VM instance 126 | """ 127 | # Build and initialize the API 128 | compute = googleapiclient.discovery.build('compute', 'v1') 129 | 130 | print('Starting VM instance ...') 131 | print('Instance Id: ' + instance_id) 132 | 133 | # Start VM instance 134 | compute.instances().start( 135 | project=PROJECT_NAME, 136 | zone=ZONE_NAME, 137 | instance=INSTANCE_NAME).execute() 138 | 139 | return 140 | 141 | 142 | def stop_instance(instance_id): 143 | """ 144 | Stop a Compute Engine VM instance 145 | """ 146 | # Build and initialize the API 147 | compute = googleapiclient.discovery.build('compute', 'v1') 148 | 149 | print('Stopping VM instance ...') 150 | print('Instance Id: ' + instance_id) 151 | 152 | # Stop VM instance 153 | compute.instances().stop( 154 | project=PROJECT_NAME, 155 | zone=ZONE_NAME, 156 | instance=INSTANCE_NAME).execute() 157 | 158 | return 159 | 160 | 161 | def reset_instance(instance_id): 162 | """ 163 | Reset a Compute Engine VM instance 164 | """ 165 | # Build and initialize the API 166 | compute = googleapiclient.discovery.build('compute', 'v1') 167 | 168 | print('Resetting VM instance ...') 169 | print('Instance Id: ' + instance_id) 170 | 171 | # Reset VM instance 172 | compute.instances().reset( 173 | project=PROJECT_NAME, 174 | zone=ZONE_NAME, 175 | instance=INSTANCE_NAME).execute() 176 | 177 | return 178 | 179 | 180 | def delete_instance(instance_id): 181 | """ 182 | Delete a Compute Engine VM instance 183 | """ 184 | # Build and initialize the API 185 | compute = googleapiclient.discovery.build('compute', 'v1') 186 | 187 | print('Deleting VM instance ...') 188 | print('Instance Id: ' + instance_id) 189 | 190 | # Delete VM instance 191 | compute.instances().delete( 192 | project=PROJECT_NAME, 193 | zone=ZONE_NAME, 194 | instance=INSTANCE_NAME).execute() 195 | 196 | return 197 | -------------------------------------------------------------------------------- /gcloudcomputeengine/computeengineinstances.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # computeengineinstances.py 4 | # It is an example of how to handle Google Cloud Compute Engine VM instances. 5 | 6 | import sys 7 | import computeenginehelper 8 | 9 | def print_menu(): 10 | print('\nMENU') 11 | print('0 = Quit') 12 | print('1 = List all VM instances') 13 | print('2 = Create VM instance') 14 | print('3 = List VM instance') 15 | print('4 = Start instance') 16 | print('5 = Stop instance') 17 | print('6 = Reset instance') 18 | print('7 = Delete instance') 19 | return 20 | 21 | 22 | def main(): 23 | instance_id = '' 24 | option = -1 25 | 26 | while option != 0: 27 | print_menu() 28 | try: 29 | option = int(input('\nEnter an option? ')) 30 | if option == 0: 31 | print('Bye') 32 | elif option == 1: 33 | computeenginehelper.list_instances() 34 | elif option == 2: 35 | instance_id = computeenginehelper.create_instance() 36 | elif option == 3: 37 | computeenginehelper.list_instance(instance_id) 38 | elif option == 4: 39 | computeenginehelper.start_instance(instance_id) 40 | elif option == 5: 41 | computeenginehelper.stop_instance(instance_id) 42 | elif option == 6: 43 | computeenginehelper.reset_instance(instance_id) 44 | elif option == 7: 45 | computeenginehelper.delete_instance(instance_id) 46 | else: 47 | print('\nERROR: Enter a valid option!!') 48 | except ValueError: 49 | print('\nERROR: Enter a valid option!!') 50 | 51 | return 52 | 53 | 54 | # This is the standard boilerplate that calls the main() function. 55 | if __name__ == '__main__': 56 | main() 57 | -------------------------------------------------------------------------------- /gcloudfunctiongcscopy/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Function Cloud Storage Copy Python example 2 | 3 | This folder contains a Google Cloud Function example in Python on Google Cloud Platform (GCP). 4 | 5 | It handles a Google Cloud Function that copies an object when it appears in a Cloud Storage bucket to another Cloud Storage bucket. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * Google API Python Client Library 15 | 16 | * Dependencies in Python are managed with pip and expressed in a metadata file called `requirements.txt` shipped alongside your function. 17 | 18 | This `requirements.txt` file must be in the same directory as the `main.py` file that contains your function code. 19 | 20 | The `requirements.txt` file contains one line per library. Each line contains the package name, and optionally, the requested version. 21 | 22 | In order to use the Google Cloud Python Client Library, the file needs to include this dependency: 23 | 24 | ```bash 25 | google-cloud-storage==1.12.0 26 | ``` 27 | 28 | ## Using the code 29 | 30 | * Access the Google Cloud console. 31 | 32 | * Create a Cloud Storage bucket for the source and another Cloud Storage bucket for the target. 33 | 34 | * Create a Google Cloud Function: 35 | * Name: `` 36 | * Memory allocated: `256 MB` 37 | * Trigger: `Cloud Storage` 38 | * Event Type: `Finalise/Create` 39 | * Bucket: `` 40 | * Source code. You can use 2 options: 41 | * Inline editor: 42 | Edit the code of the `main.py` and `requirements.txt` files in the browser. 43 | * ZIP upload: 44 | Upload a ZIP file containing the `main.py` and `requirements.txt` files. 45 | * ZIP file: `` 46 | * Stage bucket: `` 47 | * Runtime: `Python 3.7 (Beta)` 48 | * Function to execute: `gcs_copy` 49 | * Region: `` 50 | * Timeout: `60 seconds` 51 | 52 | * You can select the destination bucket name changing the value of `DESTINATION_BUCKET` variable in the code. 53 | 54 | * Save the Google Cloud Function. 55 | 56 | The function is deployed and run. 57 | 58 | * Test the function. 59 | 60 | Copy a file in the source Cloud Storage bucket. 61 | 62 | The object from the source Cloud Storage bucket should be copied to the target Cloud Storage bucket. 63 | -------------------------------------------------------------------------------- /gcloudfunctiongcscopy/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # main.py 4 | # It handles a Google Cloud Function that copies an object when it appears in a Cloud Storage bucket to another Cloud Storage bucket. 5 | 6 | from google.cloud import storage 7 | from google.cloud.exceptions import NotFound 8 | from google.cloud.exceptions import Forbidden 9 | 10 | def gcs_copy(event, context): 11 | """Triggered by a change to a Cloud Storage bucket. 12 | Args: 13 | event (dict): Event payload. 14 | context (google.cloud.functions.Context): Metadata for the event. 15 | """ 16 | 17 | DESTINATION_BUCKET = 'destinationbucket' # Destination Bucket name 18 | 19 | file = event 20 | 21 | source_bucket_name = file['bucket'] 22 | source_blob_name = file['name'] 23 | destination_bucket_name = DESTINATION_BUCKET 24 | destination_blob_name = source_blob_name 25 | 26 | print('From - bucket:', source_bucket_name) 27 | print('From - object:', source_blob_name) 28 | print('To - bucket: ', destination_bucket_name) 29 | print('To - object: ', destination_blob_name) 30 | 31 | print('Copying object ...') 32 | 33 | # Instantiate the client. 34 | client = storage.Client() 35 | 36 | try: 37 | # Get the source bucket. 38 | source_bucket = client.get_bucket(source_bucket_name) 39 | # Instantiate the source object. 40 | source_blob = source_bucket.blob(source_blob_name) 41 | # Get the destination bucket. 42 | destination_bucket = client.get_bucket(destination_bucket_name) 43 | # Copies a blob from one bucket to another one. 44 | source_bucket.copy_blob(source_blob, destination_bucket, destination_blob_name) 45 | print('\nCopied') 46 | except NotFound: 47 | print('Error: Bucket/Blob does NOT exists!!') 48 | pass 49 | except Forbidden: 50 | print('Error: Forbidden, you do not have access to it!!') 51 | pass 52 | -------------------------------------------------------------------------------- /gcloudfunctiongcscopy/requirements.txt: -------------------------------------------------------------------------------- 1 | # Function dependencies, for example: 2 | # package>=version 3 | google-cloud-storage==1.12.0 4 | -------------------------------------------------------------------------------- /gcloudfunctiongcsevent/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Function Cloud Storage Event Python example 2 | 3 | This folder contains a Google Cloud Function example in Python on Google Cloud Platform (GCP). 4 | 5 | It handles a Google Cloud Function that sends information to the function log about an object when it appears in a Cloud Storage bucket. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * The code was written for Python 3. 12 | 13 | ## Using the code 14 | 15 | * Access the Google Cloud console. 16 | 17 | * Create a Cloud Storage bucket. 18 | 19 | * Create a Google Cloud Function: 20 | * Name: `` 21 | * Memory allocated: `256 MB` 22 | * Trigger: `Cloud Storage` 23 | * Event Type: `Finalise/Create` 24 | * Bucket: `` 25 | * Source code. You can use 2 options: 26 | * Inline editor: 27 | Edit the code of the `main.py` in the browser. 28 | * ZIP upload: 29 | Upload a ZIP file containing the `main.py` and `requirements.txt` files. 30 | * ZIP file: `` 31 | * Stage bucket: `` 32 | * Runtime: `Python 3.7 (Beta)` 33 | * Function to execute: `gcs_event` 34 | * Region: `` 35 | * Timeout: `60 seconds` 36 | 37 | * Save the Google Cloud Function. 38 | 39 | The function is deployed and run. 40 | 41 | * Test the function. 42 | 43 | Copy a file in the source Cloud Storage bucket. 44 | 45 | You should see the next message in the Google Cloud Function log: 46 | 47 | ```bash 48 | Bucket: 49 | Object: 50 | Object size: 51 | ``` 52 | -------------------------------------------------------------------------------- /gcloudfunctiongcsevent/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # main.py 4 | # It handles a Google Cloud Funtion that sends information to the log about an object when it appears in a Cloud Storage bucket. 5 | 6 | def gcs_event(event, context): 7 | """Triggered by a change to a Cloud Storage bucket. 8 | Args: 9 | event (dict): Event payload. 10 | context (google.cloud.functions.Context): Metadata for the event. 11 | """ 12 | 13 | file = event 14 | print(f"Bucket: {file['bucket']}.") 15 | print(f"Object: {file['name']}.") 16 | print(f"Object size: {file['size']}.") -------------------------------------------------------------------------------- /gcloudfunctiongcsevent/requirements.txt: -------------------------------------------------------------------------------- 1 | # Function dependencies, for example: 2 | # package>=version 3 | -------------------------------------------------------------------------------- /gcloudfunctiongcsmove/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Function Cloud Storage Move Python example 2 | 3 | This folder contains a Google Cloud Function example in Python on Google Cloud Platform (GCP). 4 | 5 | It handles a Google Cloud Function that Moves an object when it appears in a Cloud Storage bucket to another Cloud Storage bucket. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * Google API Python Client Library 15 | 16 | * Dependencies in Python are managed with pip and expressed in a metadata file called `requirements.txt` shipped alongside your function. 17 | 18 | This `requirements.txt` file must be in the same directory as the `main.py` file that contains your function code. 19 | 20 | The `requirements.txt` file contains one line per library. Each line contains the package name, and optionally, the requested version. 21 | 22 | In order to use the Google Cloud Python Client Library, the file needs to include this dependency: 23 | 24 | ```bash 25 | google-cloud-storage==1.12.0 26 | ``` 27 | 28 | ## Using the code 29 | 30 | * Access the Google Cloud console. 31 | 32 | * Create a Cloud Storage bucket for the source and another Cloud Storage bucket for the target. 33 | 34 | * Create a Google Cloud Function: 35 | * Name: `` 36 | * Memory allocated: `256 MB` 37 | * Trigger: `Cloud Storage` 38 | * Event Type: `Finalise/Create` 39 | * Bucket: `` 40 | * Source code. You can use 2 options: 41 | * Inline editor: 42 | Edit the code of the `main.py` and `requirements.txt` files in the browser. 43 | * ZIP upload: 44 | Upload a ZIP file containing the `main.py` and `requirements.txt` files. 45 | * ZIP file: `` 46 | * Stage bucket: `` 47 | * Runtime: `Python 3.7 (Beta)` 48 | * Function to execute: `gcs_move` 49 | * Region: `` 50 | * Timeout: `60 seconds` 51 | 52 | * You can select the destination bucket name changing the value of `DESTINATION_BUCKET` variable in the code. 53 | 54 | * Save the Google Cloud Function. 55 | 56 | The function is deployed and run. 57 | 58 | * Test the function. 59 | 60 | Copy a file in the source Cloud Storage bucket. 61 | 62 | The object from the source Cloud Storage bucket should be copied to the target Cloud Storage bucket and deleted in the source Cloud Storage bucket. 63 | -------------------------------------------------------------------------------- /gcloudfunctiongcsmove/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # main.py 4 | # It handles a Google Cloud Function that moves an object when it appears in a Cloud Storage bucket to another Cloud Storage bucket. 5 | 6 | from google.cloud import storage 7 | from google.cloud.exceptions import NotFound 8 | from google.cloud.exceptions import Forbidden 9 | 10 | def gcs_move(event, context): 11 | """Triggered by a change to a Cloud Storage bucket. 12 | Args: 13 | event (dict): Event payload. 14 | context (google.cloud.functions.Context): Metadata for the event. 15 | """ 16 | 17 | DESTINATION_BUCKET = 'destinationbucket' # Destination Bucket name 18 | 19 | file = event 20 | 21 | source_bucket_name = file['bucket'] 22 | source_blob_name = file['name'] 23 | destination_bucket_name = DESTINATION_BUCKET 24 | destination_blob_name = source_blob_name 25 | 26 | print('From - bucket:', source_bucket_name) 27 | print('From - object:', source_blob_name) 28 | print('To - bucket: ', destination_bucket_name) 29 | print('To - object: ', destination_blob_name) 30 | 31 | print('Moving object ...') 32 | 33 | # Instantiate the client. 34 | client = storage.Client() 35 | 36 | try: 37 | # Get the source bucket. 38 | source_bucket = client.get_bucket(source_bucket_name) 39 | # Instantiate the source object. 40 | source_blob = source_bucket.blob(source_blob_name) 41 | # Get the destination bucket. 42 | destination_bucket = client.get_bucket(destination_bucket_name) 43 | # Copies a blob from one bucket to another one. 44 | source_bucket.copy_blob(source_blob, destination_bucket, destination_blob_name) 45 | # Delete the source object. 46 | source_blob.delete() 47 | print('\nMoved') 48 | except NotFound: 49 | print('Error: Bucket/Blob does NOT exists!!') 50 | pass 51 | except Forbidden: 52 | print('Error: Forbidden, you do not have access to it!!') 53 | pass 54 | -------------------------------------------------------------------------------- /gcloudfunctiongcsmove/requirements.txt: -------------------------------------------------------------------------------- 1 | # Function dependencies, for example: 2 | # package>=version 3 | google-cloud-storage==1.12.0 4 | -------------------------------------------------------------------------------- /gcloudfunctionhttprequest/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Function HTTP Request Python example 2 | 3 | This folder contains a Google Cloud Function example in Python on Google Cloud Platform (GCP). 4 | 5 | It handles a Google Cloud Function that responds to an HTTP request. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * The code was written for Python 3. 12 | 13 | ## Using the code 14 | 15 | * Access the Google Cloud console. 16 | 17 | * Create a Google Cloud Function: 18 | * Name: `` 19 | * Memory allocated: `256 MB` 20 | * Trigger: `HTTP` 21 | * URL: `https://-.cloudfunctions.net/` 22 | * Source code. You can use 2 options: 23 | * Inline editor: 24 | Edit the code of the `main.py` in the browser. 25 | * ZIP upload: 26 | Upload a ZIP file containing the `main.py` and `requirements.txt` files. 27 | * ZIP file: `` 28 | * Stage bucket: `` 29 | * Runtime: `Python 3.7 (Beta)` 30 | * Function to execute: `http_request` 31 | * Region: `` 32 | * Timeout: `60 seconds` 33 | 34 | * Save the Google Cloud Function. 35 | 36 | The function is deployed and run. 37 | 38 | * Test the function. 39 | 40 | You can test it in 2 ways: 41 | 42 | * First way: Test the funcion functionality in the Google Cloud console. 43 | 44 | Go to the `Function details` and select `Testing`. 45 | 46 | Enter the `Triggering event` content: 47 | 48 | ```json 49 | { 50 | "message": "Hello Peter!" 51 | } 52 | ``` 53 | 54 | Click `Test the function`. 55 | 56 | You should see the next message in the Google Cloud `console output`: 57 | 58 | ```bash 59 | Hello Peter! 60 | ``` 61 | 62 | * Second way: Test the function with a browser. 63 | 64 | Go to the URL: `https://-.cloudfunctions.net/` using a browser. 65 | 66 | You should see the response: 67 | 68 | ```bash 69 | Hello World! 70 | ``` 71 | -------------------------------------------------------------------------------- /gcloudfunctionhttprequest/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # main.py 4 | # It handles a Google Cloud Function that responds to an HTTP request. 5 | 6 | def http_request(request): 7 | """Responds to any HTTP request. 8 | Args: 9 | request (flask.Request): HTTP request object. 10 | Returns: 11 | The response text or any set of values that can be turned into a 12 | Response object using 13 | `make_response `. 14 | """ 15 | 16 | request_json = request.get_json() 17 | if request.args and 'message' in request.args: 18 | return request.args.get('message') 19 | elif request_json and 'message' in request_json: 20 | return request_json['message'] 21 | else: 22 | return f'Hello World!' 23 | -------------------------------------------------------------------------------- /gcloudfunctionhttprequest/requirements.txt: -------------------------------------------------------------------------------- 1 | # Function dependencies, for example: 2 | # package>=version 3 | -------------------------------------------------------------------------------- /gcloudfunctionpubsubevent/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Function Pub/Sub Event Python example 2 | 3 | This folder contains a Google Cloud Function example in Python on Google Cloud Platform (GCP). 4 | 5 | It handles a Google Cloud Function that sends information about a Cloud Pub/Sub event that depends on the input to the function log. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * The code was written for Python 3. 12 | 13 | ## Using the code 14 | 15 | * Access the Google Cloud console. 16 | 17 | * Create a Google Cloud Function: 18 | * Name: `` 19 | * Memory allocated: `256 MB` 20 | * Trigger: `Cloud Pub/Sub` 21 | * Topic: `projects//topics/` 22 | * Source code. You can use 2 options: 23 | * Inline editor: 24 | Edit the code of the `main.py` in the browser. 25 | * ZIP upload: 26 | Upload a ZIP file containing the `main.py` and `requirements.txt` files. 27 | * ZIP file: `` 28 | * Stage bucket: `` 29 | * Runtime: `Python 3.7 (Beta)` 30 | * Function to execute: `pusub_event` 31 | * Region: `` 32 | * Timeout: `60 seconds` 33 | 34 | * Save the Google Cloud Function. 35 | 36 | The function is deployed and run. 37 | 38 | * Test the function. 39 | 40 | Go to the `Function details` and select `Testing`. 41 | 42 | * First test: 43 | 44 | Enter the `Triggering event` content: 45 | 46 | ```json 47 | {} 48 | ``` 49 | 50 | Click `Test the function`. 51 | 52 | You should see the next message in the Google Cloud `console output`: 53 | 54 | `OK` 55 | 56 | You should see the next message in the Google Cloud Function log: 57 | 58 | ```bash 59 | Hello World! 60 | ``` 61 | 62 | * Second test: 63 | 64 | Encoding the text `Peter` to Base64 format using an external tool, you should get `UGV0ZXI=`. 65 | 66 | Enter the `Triggering event` content: 67 | 68 | ```json 69 | { 70 | "data": "UGV0ZXI=" 71 | } 72 | ``` 73 | 74 | Click `Test the function`. 75 | 76 | You should see the next message in the Google Cloud `console output`: 77 | 78 | `OK` 79 | 80 | You should see the next message in the Google Cloud Function log: 81 | 82 | ```bash 83 | Hello Peter! 84 | ``` 85 | -------------------------------------------------------------------------------- /gcloudfunctionpubsubevent/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # main.py 4 | # It handles a Google Cloud Function that sends information about a Cloud Pub/Sub event that depends on the input to the function log. 5 | 6 | def pubsub_event(event, context): 7 | """Triggered from a message on a Cloud Pub/Sub topic. 8 | Args: 9 | event (dict): Event payload. 10 | context (google.cloud.functions.Context): Metadata for the event. 11 | """ 12 | 13 | import base64 14 | 15 | if 'data' in event: 16 | name = base64.b64decode(event['data']).decode('utf-8') 17 | else: 18 | name = 'World' 19 | print('Hello {}!'.format(name)) 20 | -------------------------------------------------------------------------------- /gcloudfunctionpubsubevent/requirements.txt: -------------------------------------------------------------------------------- 1 | # Function dependencies, for example: 2 | # package>=version 3 | -------------------------------------------------------------------------------- /gcloudstoragecopy/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Storage Copy Python example 2 | 3 | This folder contains a Python application example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 4 | 5 | Copy an object from a Google Storage bucket to another Google Storage bucket in a Google Cloud Project. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * You can install and use the Google Cloud SDK. 12 | 13 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 14 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 15 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 16 | 17 | * The code was written for: 18 | 19 | * Python 3 20 | * Google API Python Client Library 21 | 22 | * Install the Google Cloud Python Client Library. 23 | 24 | Install the latest Google Cloud Python Client Library release via pip: 25 | 26 | ```bash 27 | pip install google-cloud-storage 28 | ``` 29 | 30 | ## Using the code 31 | 32 | * Configure your Google Cloud access keys. 33 | 34 | The Google Cloud client library for Java allows you to use several authentication schemes. 35 | 36 | The application uses Application Default Credentials through a JSON service account key for authenticating. 37 | 38 | The credentials are taken from `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 39 | 40 | For example: 41 | 42 | ```bash 43 | GOOGLE_APPLICATION_CREDENTIALS = /path/to/my/key.json 44 | ``` 45 | 46 | Use the [Google Cloud Platform console](http://cloud.google.com/): 47 | 48 | * Go to the Google Cloud Project. 49 | 50 | * Prepare the credentials: 51 | 52 | * Create a Service account. 53 | 54 | For example: 55 | 56 | ```bash 57 | Name: gcloud-java-examples 58 | Role: Owner 59 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 60 | ``` 61 | 62 | * Create a key as a JSON file and download it. 63 | 64 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 65 | 66 | * Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your Operating System with the path of your JSON service account key file. 67 | 68 | * Run the code. 69 | 70 | You must provide 3 parameters, replace the values of: 71 | 72 | * `` by source bucket name. 73 | * `` by source object name. 74 | * `` by destination bucket name. 75 | 76 | Run application: 77 | 78 | ```bash 79 | python gcloudstoragecopy.py 80 | ``` 81 | 82 | * Test the application. 83 | 84 | The object from the source Google Storage bucket should be copied to the target Google Storage bucket in the Google Cloud Project. 85 | -------------------------------------------------------------------------------- /gcloudstoragecopy/cloudstoragecopy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # cloudstoragecopy.py 4 | # It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 5 | # Copy an object from a Google Storage bucket to another Google Storage bucket. 6 | # You must provide 3 parameters: 7 | # SOURCE_BUCKET = Source bucket name 8 | # SOURCE_OBJECT = Source object name 9 | # DESTINATION_BUCKET = Destination bucket name 10 | 11 | import sys 12 | import os 13 | from google.cloud import storage 14 | from google.cloud.exceptions import NotFound 15 | from google.cloud.exceptions import Forbidden 16 | 17 | def main(): 18 | 19 | # Make a list of command line arguments, omitting the [0] element 20 | # which is the script itself. 21 | args = sys.argv[1:] 22 | if len(args) < 3: 23 | print('Not enough parameters.\n'\ 24 | 'Proper Usage is: python cloudstoragecopy.py '\ 25 | ' ') 26 | sys.exit(1) 27 | 28 | source_bucket_name = args[0] 29 | source_blob_name = args[1] 30 | destination_bucket_name = args[2] 31 | destination_blob_name = source_blob_name 32 | 33 | print('From - bucket: ' + source_bucket_name) 34 | print('From - object: ' + source_blob_name) 35 | print('To - bucket: ' + destination_bucket_name) 36 | print('To - object: ' + destination_blob_name) 37 | 38 | print('Copying object ...') 39 | 40 | # Instantiate the client. 41 | client = storage.Client() 42 | 43 | try: 44 | # Get the source bucket. 45 | source_bucket = client.get_bucket(source_bucket_name) 46 | # Instantiate the source object. 47 | source_blob = source_bucket.blob(source_blob_name) 48 | # Get the destination bucket. 49 | destination_bucket = client.get_bucket(destination_bucket_name) 50 | # Copies a blob from one bucket to another one. 51 | source_bucket.copy_blob(source_blob, destination_bucket, destination_blob_name) 52 | print('\nCopied') 53 | except NotFound: 54 | print('Error: Bucket/Blob does NOT exists!!') 55 | pass 56 | except Forbidden: 57 | print('Error: Forbidden, you do not have access to it!!') 58 | pass 59 | 60 | return 61 | 62 | 63 | # This is the standard boilerplate that calls the main() function. 64 | if __name__ == '__main__': 65 | main() 66 | -------------------------------------------------------------------------------- /gcloudstoragecreate/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Storage Create Python example 2 | 3 | This folder contains a Python application example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 4 | 5 | Create a new Google Storage bucket in a Google Cloud Project. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * You can install and use the Google Cloud SDK. 12 | 13 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 14 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 15 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 16 | 17 | * The code was written for: 18 | 19 | * Python 3 20 | * Google API Python Client Library 21 | 22 | * Install the Google Cloud Python Client Library. 23 | 24 | Install the latest Google Cloud Python Client Library release via pip: 25 | 26 | ```bash 27 | pip install google-cloud-storage 28 | ``` 29 | 30 | ## Using the code 31 | 32 | * Configure your Google Cloud access keys. 33 | 34 | The Google Cloud client library for Java allows you to use several authentication schemes. 35 | 36 | The application uses Application Default Credentials through a JSON service account key for authenticating. 37 | 38 | The credentials are taken from `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 39 | 40 | For example: 41 | 42 | ```bash 43 | GOOGLE_APPLICATION_CREDENTIALS = /path/to/my/key.json 44 | ``` 45 | 46 | Use the [Google Cloud Platform console](http://cloud.google.com/): 47 | 48 | * Go to the Google Cloud Project. 49 | 50 | * Prepare the credentials: 51 | 52 | * Create a Service account. 53 | 54 | For example: 55 | 56 | ```bash 57 | Name: gcloud-java-examples 58 | Role: Owner 59 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 60 | ``` 61 | 62 | * Create a key as a JSON file and download it. 63 | 64 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 65 | 66 | * Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your Operating System with the path of your JSON service account key file. 67 | 68 | * You can select the Cloud Storage bucket location changing the value of `STORAGE_LOCATION` variable in the code. 69 | 70 | * Run the code. 71 | 72 | You must provide 1 parameter, replace the value of: 73 | 74 | * `` by Bucket name. 75 | 76 | Run application: 77 | 78 | ```bash 79 | python cloudstoragecreate.py 80 | ``` 81 | 82 | * Test the application. 83 | 84 | You should see the new Cloud Storage bucket created in the Google Cloud Project. 85 | -------------------------------------------------------------------------------- /gcloudstoragecreate/cloudstoragecreate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # cloudstoragecreate.py 4 | # It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 5 | # Create a new Cloud Storage bucket. 6 | # You must provide 1 parameter: 7 | # BUCKET_NAME = Name of the bucket 8 | 9 | import sys 10 | from google.cloud import storage 11 | from google.cloud.exceptions import Conflict 12 | 13 | def main(): 14 | 15 | STORAGE_LOCATION = 'us-east1' # Cloud Storage location 16 | 17 | # Make a list of command line arguments, omitting the [0] element 18 | # which is the script itself. 19 | args = sys.argv[1:] 20 | if len(args) < 1: 21 | print('Not enough parameters.\n'\ 22 | 'Proper Usage is: python cloudstoragecreate.py ') 23 | sys.exit(1) 24 | 25 | bucket_name = args[0] 26 | print('Bucket name: ' + bucket_name) 27 | 28 | print('Creating bucket ...') 29 | 30 | # Instantiate the client. 31 | client = storage.Client() 32 | 33 | try: 34 | # Instantiate the bucket. 35 | bucket = client.bucket(bucket_name) 36 | # Create the bucket object. 37 | bucket.create(location=STORAGE_LOCATION) 38 | print('\nCreated') 39 | except Conflict: 40 | print('Error: Bucket already exists!!') 41 | pass 42 | 43 | return 44 | 45 | 46 | # This is the standard boilerplate that calls the main() function. 47 | if __name__ == '__main__': 48 | main() 49 | -------------------------------------------------------------------------------- /gcloudstoragedelete/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Storage Delete Python example 2 | 3 | This folder contains a Python application example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 4 | 5 | Delete a Google Storage bucket for a Google Cloud Project. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * You can install and use the Google Cloud SDK. 12 | 13 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 14 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 15 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 16 | 17 | * The code was written for: 18 | 19 | * Python 3 20 | * Google API Python Client Library 21 | 22 | * Install the Google Cloud Python Client Library. 23 | 24 | Install the latest Google Cloud Python Client Library release via pip: 25 | 26 | ```bash 27 | pip install google-cloud-storage 28 | ``` 29 | 30 | ## Using the code 31 | 32 | * Configure your Google Cloud access keys. 33 | 34 | The Google Cloud client library for Java allows you to use several authentication schemes. 35 | 36 | The application uses Application Default Credentials through a JSON service account key for authenticating. 37 | 38 | The credentials are taken from `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 39 | 40 | For example: 41 | 42 | ```bash 43 | GOOGLE_APPLICATION_CREDENTIALS = /path/to/my/key.json 44 | ``` 45 | 46 | Use the [Google Cloud Platform console](http://cloud.google.com/): 47 | 48 | * Go to the Google Cloud Project. 49 | 50 | * Prepare the credentials: 51 | 52 | * Create a Service account. 53 | 54 | For example: 55 | 56 | ```bash 57 | Name: gcloud-java-examples 58 | Role: Owner 59 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 60 | ``` 61 | 62 | * Create a key as a JSON file and download it. 63 | 64 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 65 | 66 | * Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your Operating System with the path of your JSON service account key file. 67 | 68 | * Run the code. 69 | 70 | You must provide 1 parameter, replace the value of: 71 | 72 | * `` by Bucket name. 73 | 74 | Run application: 75 | 76 | ```bash 77 | python cloudstoragedelete.py 78 | ``` 79 | 80 | * Test the application. 81 | 82 | You should see the Google Storage bucket deleted in the Google Cloud Project. 83 | -------------------------------------------------------------------------------- /gcloudstoragedelete/cloudstoragedelete.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # cloudstoragedelete.py 4 | # It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 5 | # Delete a Cloud Storage bucket. 6 | # You must provide 1 parameter: 7 | # BUCKET_NAME = Name of the bucket 8 | 9 | import sys 10 | from google.cloud import storage 11 | from google.cloud.exceptions import NotFound 12 | from google.cloud.exceptions import Forbidden 13 | 14 | def main(): 15 | 16 | # Make a list of command line arguments, omitting the [0] element 17 | # which is the script itself. 18 | args = sys.argv[1:] 19 | if len(args) < 1: 20 | print('Not enough parameters.\n'\ 21 | 'Proper Usage is: python cloudstoragedelete.py ') 22 | sys.exit(1) 23 | 24 | bucket_name = args[0] 25 | print('Bucket name: ' + bucket_name) 26 | 27 | print('Deleting bucket ...') 28 | 29 | # Instantiate the client. 30 | client = storage.Client() 31 | 32 | try: 33 | # Instantiate the bucket. 34 | bucket = client.bucket(bucket_name) 35 | # Delete the bucket. 36 | bucket.delete() 37 | print('\nDeleted') 38 | except NotFound: 39 | print('Error: Bucket does NOT exists!!') 40 | pass 41 | except Forbidden: 42 | print('Error: Forbidden, you do not have access to it!!') 43 | pass 44 | 45 | return 46 | 47 | 48 | # This is the standard boilerplate that calls the main() function. 49 | if __name__ == '__main__': 50 | main() 51 | -------------------------------------------------------------------------------- /gcloudstoragedeleteobject/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Storage Delete Object Python example 2 | 3 | This folder contains a Python application example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 4 | 5 | Delete an object in a Google Storage bucket for a Google Cloud Project. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * You can install and use the Google Cloud SDK. 12 | 13 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 14 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 15 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 16 | 17 | * The code was written for: 18 | 19 | * Python 3 20 | * Google API Python Client Library 21 | 22 | * Install the Google Cloud Python Client Library. 23 | 24 | Install the latest Google Cloud Python Client Library release via pip: 25 | 26 | ```bash 27 | pip install google-cloud-storage 28 | ``` 29 | 30 | ## Using the code 31 | 32 | * Configure your Google Cloud access keys. 33 | 34 | The Google Cloud client library for Java allows you to use several authentication schemes. 35 | 36 | The application uses Application Default Credentials through a JSON service account key for authenticating. 37 | 38 | The credentials are taken from `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 39 | 40 | For example: 41 | 42 | ```bash 43 | GOOGLE_APPLICATION_CREDENTIALS = /path/to/my/key.json 44 | ``` 45 | 46 | Use the [Google Cloud Platform console](http://cloud.google.com/): 47 | 48 | * Go to the Google Cloud Project. 49 | 50 | * Prepare the credentials: 51 | 52 | * Create a Service account. 53 | 54 | For example: 55 | 56 | ```bash 57 | Name: gcloud-java-examples 58 | Role: Owner 59 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 60 | ``` 61 | 62 | * Create a key as a JSON file and download it. 63 | 64 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 65 | 66 | * Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your Operating System with the path of your JSON service account key file. 67 | 68 | * Run the code. 69 | 70 | You must provide 2 parameters, replace the values of: 71 | 72 | * `` by name of the bucket. 73 | * `` by name of the object in the bucket. 74 | 75 | Run application: 76 | 77 | ```bash 78 | python cloudstoragedeleteobject.py 79 | ``` 80 | 81 | * Test the application. 82 | 83 | You should not see the object deleted in the Google Storage bucket in the Google Cloud Project. 84 | -------------------------------------------------------------------------------- /gcloudstoragedeleteobject/cloudstoragedeleteobject.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # cloudstoragedeleteobject.py 4 | # It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 5 | # Delete an object in a Cloud Storage bucket. 6 | # You must provide 1 parameter: 7 | # BUCKET_NAME = Name of the bucket 8 | # OBJECT_NAME = Name of the object in the bucket 9 | 10 | import sys 11 | from google.cloud import storage 12 | from google.cloud.exceptions import NotFound 13 | from google.cloud.exceptions import Forbidden 14 | 15 | def main(): 16 | 17 | # Make a list of command line arguments, omitting the [0] element 18 | # which is the script itself. 19 | args = sys.argv[1:] 20 | if len(args) < 2: 21 | print('Not enough parameters.\n'\ 22 | 'Proper Usage is: python cloudstoragedeleteobject.py '\ 23 | ' ') 24 | sys.exit(1) 25 | 26 | bucket_name = args[0] 27 | blob_name = args[1] 28 | print('Bucket name: ' + bucket_name) 29 | print('Object name: ' + blob_name) 30 | 31 | print('Deleting object in the bucket ...') 32 | 33 | # Instantiate the client. 34 | client = storage.Client() 35 | 36 | try: 37 | # Instantiate the bucket. 38 | bucket = client.bucket(bucket_name) 39 | # Instantiate the object. 40 | blob = bucket.blob(blob_name) 41 | # Delete the object. 42 | blob.delete() 43 | print('\nDeleted') 44 | except NotFound: 45 | print('Error: Bucket/Object does NOT exists!!') 46 | pass 47 | except Forbidden: 48 | print('Error: Forbidden, you do not have access to it!!') 49 | pass 50 | 51 | return 52 | 53 | 54 | # This is the standard boilerplate that calls the main() function. 55 | if __name__ == '__main__': 56 | main() 57 | -------------------------------------------------------------------------------- /gcloudstoragedownload/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Storage Download Python example 2 | 3 | This folder contains a Python application example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 4 | 5 | Download an object from a Cloud Storage bucket in a Google Cloud Project to a local file. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * You can install and use the Google Cloud SDK. 12 | 13 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 14 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 15 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 16 | 17 | * The code was written for: 18 | 19 | * Python 3 20 | * Google API Python Client Library 21 | 22 | * Install the Google Cloud Python Client Library. 23 | 24 | Install the latest Google Cloud Python Client Library release via pip: 25 | 26 | ```bash 27 | pip install google-cloud-storage 28 | ``` 29 | 30 | ## Using the code 31 | 32 | * Configure your Google Cloud access keys. 33 | 34 | The Google Cloud client library for Java allows you to use several authentication schemes. 35 | 36 | The application uses Application Default Credentials through a JSON service account key for authenticating. 37 | 38 | The credentials are taken from `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 39 | 40 | For example: 41 | 42 | ```bash 43 | GOOGLE_APPLICATION_CREDENTIALS = /path/to/my/key.json 44 | ``` 45 | 46 | Use the [Google Cloud Platform console](http://cloud.google.com/): 47 | 48 | * Go to the Google Cloud Project. 49 | 50 | * Prepare the credentials: 51 | 52 | * Create a Service account. 53 | 54 | For example: 55 | 56 | ```bash 57 | Name: gcloud-java-examples 58 | Role: Owner 59 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 60 | ``` 61 | 62 | * Create a key as a JSON file and download it. 63 | 64 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 65 | 66 | * Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your Operating System with the path of your JSON service account key file. 67 | 68 | * Run the code. 69 | 70 | You must provide 3 parameters, replace the values of: 71 | 72 | * `` by name of the bucket. 73 | * `` by object name in the bucket. 74 | * `` by local file name. 75 | 76 | Run application: 77 | 78 | ```bash 79 | python gcloudstoragedownload.py 80 | ``` 81 | 82 | * Test the application. 83 | 84 | You should see the new file created in your local destiny from the Google Storage bucket in a Google Cloud Project. 85 | -------------------------------------------------------------------------------- /gcloudstoragedownload/cloudstoragedownload.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # cloudstoragedownload.py 4 | # It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 5 | # Download an object from a Cloud Storage bucket to a local file. 6 | # You must provide 3 parameters: 7 | # BUCKET_NAME = Bucket name 8 | # OBJECT_NAME = Object name in the bucket 9 | # LOCAL_FILE_NAME = Local file name 10 | 11 | 12 | import sys 13 | import os 14 | from google.cloud import storage 15 | from google.cloud.exceptions import NotFound 16 | from google.cloud.exceptions import Forbidden 17 | 18 | def main(): 19 | 20 | # Make a list of command line arguments, omitting the [0] element 21 | # which is the script itself. 22 | args = sys.argv[1:] 23 | if len(args) < 3: 24 | print('Not enough parameters.\n'\ 25 | 'Proper Usage is: python cloudstoragedownload.py '\ 26 | ' ') 27 | sys.exit(1) 28 | 29 | bucket_name = args[0] 30 | blob_name = args[1] 31 | local_file_name = args[2] 32 | 33 | print('Bucket: ' + bucket_name) 34 | print('Object: ' + blob_name) 35 | print('Local file: ' + local_file_name) 36 | 37 | print('Downloading an object from a Cloud Storage bucket to a local file ...') 38 | 39 | # Instantiate the client. 40 | client = storage.Client() 41 | 42 | try: 43 | # Get the bucket. 44 | bucket = client.get_bucket(bucket_name) 45 | # Instantiate the object. 46 | blob = bucket.blob(blob_name) 47 | # Downloads an object from the bucket. 48 | blob.download_to_filename(local_file_name) 49 | print('\nDownloaded') 50 | except NotFound: 51 | print('Error: Bucket/Blob does NOT exists!!') 52 | pass 53 | except Forbidden: 54 | print('Error: Forbidden, you do not have access to it!!') 55 | pass 56 | 57 | return 58 | 59 | 60 | # This is the standard boilerplate that calls the main() function. 61 | if __name__ == '__main__': 62 | main() 63 | -------------------------------------------------------------------------------- /gcloudstoragelist/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Storage List Python example 2 | 3 | This folder contains a Python application example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 4 | 5 | List information about the objects in a Cloud Storage bucket in a Google Cloud Project. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * You can install and use the Google Cloud SDK. 12 | 13 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 14 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 15 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 16 | 17 | * The code was written for: 18 | 19 | * Python 3 20 | * Google API Python Client Library 21 | 22 | * Install the Google Cloud Python Client Library. 23 | 24 | Install the latest Google Cloud Python Client Library release via pip: 25 | 26 | ```bash 27 | pip install google-cloud-storage 28 | ``` 29 | 30 | ## Using the code 31 | 32 | * Configure your Google Cloud access keys. 33 | 34 | The Google Cloud client library for Java allows you to use several authentication schemes. 35 | 36 | The application uses Application Default Credentials through a JSON service account key for authenticating. 37 | 38 | The credentials are taken from `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 39 | 40 | For example: 41 | 42 | ```bash 43 | GOOGLE_APPLICATION_CREDENTIALS = /path/to/my/key.json 44 | ``` 45 | 46 | Use the [Google Cloud Platform console](http://cloud.google.com/): 47 | 48 | * Go to the Google Cloud Project. 49 | 50 | * Prepare the credentials: 51 | 52 | * Create a Service account. 53 | 54 | For example: 55 | 56 | ```bash 57 | Name: gcloud-java-examples 58 | Role: Owner 59 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 60 | ``` 61 | 62 | * Create a key as a JSON file and download it. 63 | 64 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 65 | 66 | * Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your Operating System with the path of your JSON service account key file. 67 | 68 | * Run the code. 69 | 70 | You must provide 1 parameter, replace the value of: 71 | 72 | * `` by Bucket name. 73 | 74 | Run application: 75 | 76 | ```bash 77 | python cloudstoragelist.py 78 | ``` 79 | 80 | * Test the application. 81 | 82 | You should see the list of all the objects in the Cloud Storage bucket in a Google Cloud Project. 83 | -------------------------------------------------------------------------------- /gcloudstoragelist/cloudstoragelist.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # cloudstoragelist.py 4 | # It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 5 | # List information about the objects in a Cloud Storage bucket. 6 | # You must provide 1 parameter: 7 | # BUCKET_NAME = Name of the bucket 8 | 9 | import sys 10 | from google.cloud import storage 11 | from google.cloud.exceptions import NotFound 12 | from google.cloud.exceptions import Forbidden 13 | 14 | def main(): 15 | 16 | # Make a list of command line arguments, omitting the [0] element 17 | # which is the script itself. 18 | args = sys.argv[1:] 19 | if len(args) < 1: 20 | print('Not enough parameters.\n'\ 21 | 'Proper Usage is: python cloudstoragelist.py ') 22 | sys.exit(1) 23 | 24 | bucket_name = args[0] 25 | print('Bucket name: ' + bucket_name) 26 | 27 | print('Listing objects ...') 28 | 29 | # Instantiate the client. 30 | client = storage.Client() 31 | 32 | try: 33 | # Instantiate the bucket. 34 | bucket = client.bucket(bucket_name) 35 | # Get the bucket. 36 | bucket = client.get_bucket(bucket_name) 37 | # Lists all the blobs in the bucket. 38 | blobs = bucket.list_blobs() 39 | for blob in blobs: 40 | print(' -', blob.name) 41 | print(' size:', blob.size) 42 | print('\nListed') 43 | except NotFound: 44 | print('Error: Bucket does NOT exists!!') 45 | pass 46 | except Forbidden: 47 | print('Error: Forbidden, you do not have access to it!!') 48 | pass 49 | 50 | return 51 | 52 | 53 | # This is the standard boilerplate that calls the main() function. 54 | if __name__ == '__main__': 55 | main() 56 | -------------------------------------------------------------------------------- /gcloudstoragelistall/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Storage List All Python example 2 | 3 | This folder contains a Python application example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 4 | 5 | List information about all Cloud Storage buckets and the objects that they contain in a Google Cloud Project. 6 | 7 | The credentials are taken from GOOGLE_APPLICATION_CREDENTIALS environment variable. 8 | 9 | ## Requirements 10 | 11 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 12 | 13 | * You can install and use the Google Cloud SDK. 14 | 15 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 16 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 17 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 18 | 19 | * The code was written for: 20 | 21 | * Python 3 22 | * Google API Python Client Library 23 | 24 | * Install the Google Cloud Python Client Library. 25 | 26 | Install the latest Google Cloud Python Client Library release via pip: 27 | 28 | ```bash 29 | pip install google-cloud-storage 30 | ``` 31 | 32 | ## Using the code 33 | 34 | * Configure your Google Cloud access keys. 35 | 36 | The Google Cloud client library for Java allows you to use several authentication schemes. 37 | 38 | The application uses Application Default Credentials through a JSON service account key for authenticating. 39 | 40 | The credentials are taken from `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 41 | 42 | For example: 43 | 44 | ```bash 45 | GOOGLE_APPLICATION_CREDENTIALS = /path/to/my/key.json 46 | ``` 47 | 48 | Use the [Google Cloud Platform console](http://cloud.google.com/): 49 | 50 | * Go to the Google Cloud Project. 51 | 52 | * Prepare the credentials: 53 | 54 | * Create a Service account. 55 | 56 | For example: 57 | 58 | ```bash 59 | Name: gcloud-java-examples 60 | Role: Owner 61 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 62 | ``` 63 | 64 | * Create a key as a JSON file and download it. 65 | 66 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 67 | 68 | * Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your Operating System with the path of your JSON service account key file. 69 | 70 | * Run the code. 71 | 72 | Run application: 73 | 74 | ```bash 75 | python cloudstoragelistall.py 76 | ``` 77 | 78 | * Test the application. 79 | 80 | You should see the list of buckets and objects stored in each Cloud Storage bucket in the Google Cloud Project. 81 | -------------------------------------------------------------------------------- /gcloudstoragelistall/cloudstoragelistall.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # cloudstoragelistall.py 4 | # It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 5 | # List information about all Cloud Storage buckets and the objects that they contain. 6 | # The credentials are taken from GOOGLE_APPLICATION_CREDENTIALS environment variable. 7 | 8 | import sys 9 | from google.cloud import storage 10 | 11 | def main(): 12 | 13 | print('Listing Cloud Storage buckets and objects ...') 14 | 15 | # Instantiate the client. 16 | client = storage.Client() 17 | 18 | # List all the buckets. 19 | buckets = client.list_buckets() 20 | for bucket in buckets: 21 | print('* Bucket:', bucket.name) 22 | # Lists all the blobs in the bucket. 23 | blobs = bucket.list_blobs() 24 | for blob in blobs: 25 | print(' - Object:', blob.name) 26 | print(' size:', blob.size) 27 | print('\nListed') 28 | 29 | return 30 | 31 | 32 | # This is the standard boilerplate that calls the main() function. 33 | if __name__ == '__main__': 34 | main() 35 | -------------------------------------------------------------------------------- /gcloudstoragelistallparam/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Storage List All Parameters Python example 2 | 3 | This folder contains a Python application example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 4 | 5 | List information about all Cloud Storage buckets and the objects that they contain in a Google Cloud Project. 6 | 7 | The user must provide the credentials using the application parameters. 8 | 9 | ## Requirements 10 | 11 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 12 | 13 | * You can install and use the Google Cloud SDK. 14 | 15 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 16 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 17 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 18 | 19 | * The code was written for: 20 | 21 | * Python 3 22 | * Google API Python Client Library 23 | 24 | * Install the Google Cloud Python Client Library. 25 | 26 | Install the latest Google Cloud Python Client Library release via pip: 27 | 28 | ```bash 29 | pip install google-cloud-storage 30 | ``` 31 | 32 | ## Using the code 33 | 34 | * Configure your Google Cloud access keys. 35 | 36 | The Google Cloud client library for Java allows you to use several authentication schemes. 37 | 38 | The application uses Application Default Credentials through a JSON service account key for authenticating. 39 | 40 | The credentials are taken from the file name provided by application parameters. 41 | 42 | Use the [Google Cloud Platform console](http://cloud.google.com/): 43 | 44 | * Go to the Google Cloud Project. 45 | 46 | * Prepare the credentials: 47 | 48 | * Create a Service account. 49 | 50 | For example: 51 | 52 | ```bash 53 | Name: gcloud-java-examples 54 | Role: Owner 55 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 56 | ``` 57 | 58 | * Create a key as a JSON file and download it. 59 | 60 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 61 | 62 | * Run the code. 63 | 64 | You must provide 1 parameter, replace the value of: 65 | 66 | * `` by path and name of the JSON credential file. 67 | 68 | Run application: 69 | 70 | ```bash 71 | python cloudstoragelistallparam.py 72 | ``` 73 | 74 | Example: 75 | 76 | ```bash 77 | python cloudstoragelistallparam.py ~/.gcloud/gcloud-java-examples.json 78 | ``` 79 | 80 | * Test the application. 81 | 82 | You should see the list of buckets and objects stored in each Cloud Storage bucket in the Google Cloud Project. 83 | -------------------------------------------------------------------------------- /gcloudstoragelistallparam/cloudstoragelistallparam.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # cloudstoragelistallparam.py 4 | # It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 5 | # List information about all Cloud Storage buckets and the objects that they contain. 6 | # The user must provide the credentials using the application parameters. 7 | # You must provide 1 parameter: 8 | # CREDENTIALS_FILE_NAME = Path and name of the JSON credential file 9 | 10 | import sys 11 | import os 12 | from google.cloud import storage 13 | 14 | def main(): 15 | 16 | # Make a list of command line arguments, omitting the [0] element 17 | # which is the script itself. 18 | args = sys.argv[1:] 19 | if len(args) < 1: 20 | print('Not enough parameters.\n'\ 21 | 'Proper Usage is: python cloudstoragelistallparam.py ') 22 | sys.exit(1) 23 | 24 | credentials_file = args[0] 25 | 26 | print('Credentials file: ' + credentials_file) 27 | 28 | if not os.path.isfile(credentials_file): 29 | print("Error: Credentials file does NOT exist!!") 30 | sys.exit(1) 31 | 32 | print('Listing Cloud Storage buckets and objects ...') 33 | 34 | # Instantiate the client. 35 | # Explicitly use service account credentials by specifying the private key file. 36 | client = storage.Client.from_service_account_json(credentials_file) 37 | 38 | # List all the buckets. 39 | buckets = client.list_buckets() 40 | for bucket in buckets: 41 | print('* Bucket:', bucket.name) 42 | # Lists all the blobs in the bucket. 43 | blobs = bucket.list_blobs() 44 | for blob in blobs: 45 | print(' - Object:', blob.name) 46 | print(' size:', blob.size) 47 | print('\nListed') 48 | 49 | return 50 | 51 | 52 | # This is the standard boilerplate that calls the main() function. 53 | if __name__ == '__main__': 54 | main() 55 | -------------------------------------------------------------------------------- /gcloudstoragemove/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Storage Move Python example 2 | 3 | This folder contains a Python application example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 4 | 5 | Move an object from a Google Storage bucket to another Google Storage bucket in a Google Cloud Project. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * You can install and use the Google Cloud SDK. 12 | 13 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 14 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 15 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 16 | 17 | * The code was written for: 18 | 19 | * Python 3 20 | * Google API Python Client Library 21 | 22 | * Install the Google Cloud Python Client Library. 23 | 24 | Install the latest Google Cloud Python Client Library release via pip: 25 | 26 | ```bash 27 | pip install google-cloud-storage 28 | ``` 29 | 30 | ## Using the code 31 | 32 | * Configure your Google Cloud access keys. 33 | 34 | The Google Cloud client library for Java allows you to use several authentication schemes. 35 | 36 | The application uses Application Default Credentials through a JSON service account key for authenticating. 37 | 38 | The credentials are taken from `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 39 | 40 | For example: 41 | 42 | ```bash 43 | GOOGLE_APPLICATION_CREDENTIALS = /path/to/my/key.json 44 | ``` 45 | 46 | Use the [Google Cloud Platform console](http://cloud.google.com/): 47 | 48 | * Go to the Google Cloud Project. 49 | 50 | * Prepare the credentials: 51 | 52 | * Create a Service account. 53 | 54 | For example: 55 | 56 | ```bash 57 | Name: gcloud-java-examples 58 | Role: Owner 59 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 60 | ``` 61 | 62 | * Create a key as a JSON file and download it. 63 | 64 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 65 | 66 | * Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your Operating System with the path of your JSON service account key file. 67 | 68 | * Run the code. 69 | 70 | You must provide 3 parameters, replace the values of: 71 | 72 | * `` by source bucket name. 73 | * `` by source object name. 74 | * `` by destination bucket name. 75 | 76 | Run application: 77 | 78 | ```bash 79 | python gcloudstoragemove.py 80 | ``` 81 | 82 | * Test the application. 83 | 84 | The object from the source Google Storage bucket should be moved to the target Google Storage bucket in the Google Cloud Project. 85 | -------------------------------------------------------------------------------- /gcloudstoragemove/cloudstoragemove.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # cloudstoragemove.py 4 | # It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 5 | # Move an object from a Google Storage bucket to another Google Storage bucket. 6 | # You must provide 3 parameters: 7 | # SOURCE_BUCKET = Source bucket name 8 | # SOURCE_OBJECT = Source object name 9 | # DESTINATION_BUCKET = Destination bucket name 10 | 11 | import sys 12 | import os 13 | from google.cloud import storage 14 | from google.cloud.exceptions import NotFound 15 | from google.cloud.exceptions import Forbidden 16 | 17 | def main(): 18 | 19 | # Make a list of command line arguments, omitting the [0] element 20 | # which is the script itself. 21 | args = sys.argv[1:] 22 | if len(args) < 3: 23 | print('Not enough parameters.\n'\ 24 | 'Proper Usage is: python cloudstoragemove.py '\ 25 | ' ') 26 | sys.exit(1) 27 | 28 | source_bucket_name = args[0] 29 | source_blob_name = args[1] 30 | destination_bucket_name = args[2] 31 | destination_blob_name = source_blob_name 32 | 33 | print('From - bucket: ' + source_bucket_name) 34 | print('From - object: ' + source_blob_name) 35 | print('To - bucket: ' + destination_bucket_name) 36 | print('To - object: ' + destination_blob_name) 37 | 38 | print('Moving object ...') 39 | 40 | # Instantiate the client. 41 | client = storage.Client() 42 | 43 | try: 44 | # Get the source bucket. 45 | source_bucket = client.get_bucket(source_bucket_name) 46 | # Instantiate the source object. 47 | source_blob = source_bucket.blob(source_blob_name) 48 | # Get the destination bucket. 49 | destination_bucket = client.get_bucket(destination_bucket_name) 50 | # Copies a blob from one bucket to another one. 51 | source_bucket.copy_blob(source_blob, destination_bucket, destination_blob_name) 52 | # Delete the source object. 53 | source_blob.delete() 54 | print('\nMoved') 55 | except NotFound: 56 | print('Error: Bucket/Blob does NOT exists!!') 57 | pass 58 | except Forbidden: 59 | print('Error: Forbidden, you do not have access to it!!') 60 | pass 61 | 62 | return 63 | 64 | 65 | # This is the standard boilerplate that calls the main() function. 66 | if __name__ == '__main__': 67 | main() 68 | -------------------------------------------------------------------------------- /gcloudstorageupload/README.md: -------------------------------------------------------------------------------- 1 | # Google Cloud Storage Upload Python example 2 | 3 | This folder contains a Python application example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 4 | 5 | Upload a local file to a Google Storage bucket in a Google Cloud Project. 6 | 7 | ## Requirements 8 | 9 | * You must have a [Google Cloud Platform (GCP)](http://cloud.google.com/) account. 10 | 11 | * You can install and use the Google Cloud SDK. 12 | 13 | The Google Cloud SDK is a set of tools for Google Cloud Platform. 14 | It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, 15 | and other products and services from the command line. You can run these tools interactively or in your automated scripts.* The code was written for Python 3 and Google Cloud Python Client Library. 16 | 17 | * The code was written for: 18 | 19 | * Python 3 20 | * Google API Python Client Library 21 | 22 | * Install the Google Cloud Python Client Library. 23 | 24 | Install the latest Google Cloud Python Client Library release via pip: 25 | 26 | ```bash 27 | pip install google-cloud-storage 28 | ``` 29 | 30 | ## Using the code 31 | 32 | * Configure your Google Cloud access keys. 33 | 34 | The Google Cloud client library for Java allows you to use several authentication schemes. 35 | 36 | The application uses Application Default Credentials through a JSON service account key for authenticating. 37 | 38 | The credentials are taken from `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 39 | 40 | For example: 41 | 42 | ```bash 43 | GOOGLE_APPLICATION_CREDENTIALS = /path/to/my/key.json 44 | ``` 45 | 46 | Use the [Google Cloud Platform console](http://cloud.google.com/): 47 | 48 | * Go to the Google Cloud Project. 49 | 50 | * Prepare the credentials: 51 | 52 | * Create a Service account. 53 | 54 | For example: 55 | 56 | ```bash 57 | Name: gcloud-java-examples 58 | Role: Owner 59 | Email: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com 60 | ``` 61 | 62 | * Create a key as a JSON file and download it. 63 | 64 | * Add the Service accounts id (Ex.: gcloud-java-examples@gcloud-java-examples.iam.gserviceaccount.com) as a member of the project in the IAM. 65 | 66 | * Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your Operating System with the path of your JSON service account key file. 67 | 68 | * Run the code. 69 | 70 | You must provide 3 parameters, replace the values of: 71 | 72 | * `` by name of the bucket. 73 | * `` by object name in the bucket. 74 | * `` by local file name. 75 | 76 | Run application: 77 | 78 | ```bash 79 | python cloudstorageupload.py 80 | ``` 81 | 82 | * Test the application. 83 | 84 | You should see the new object created in the Google Storage bucket in a Google Cloud Project. 85 | -------------------------------------------------------------------------------- /gcloudstorageupload/cloudstorageupload.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # cloudstorageupload.py 4 | # It is an example that handles Cloud Storage buckets on Google Cloud Platform (GCP). 5 | # Upload a local file to a Google Storage bucket. 6 | # You must provide 3 parameters: 7 | # BUCKET_NAME = Bucket name 8 | # OBJECT_NAME = Object name in the bucket 9 | # LOCAL_FILE_NAME = Local file name 10 | 11 | 12 | import sys 13 | import os 14 | from google.cloud import storage 15 | from google.cloud.exceptions import NotFound 16 | from google.cloud.exceptions import Forbidden 17 | 18 | def main(): 19 | 20 | # Make a list of command line arguments, omitting the [0] element 21 | # which is the script itself. 22 | args = sys.argv[1:] 23 | if len(args) < 3: 24 | print('Not enough parameters.\n'\ 25 | 'Proper Usage is: python cloudstorageupload.py '\ 26 | ' ') 27 | sys.exit(1) 28 | 29 | bucket_name = args[0] 30 | blob_name = args[1] 31 | local_file_name = args[2] 32 | 33 | print('Bucket: ' + bucket_name) 34 | print('Object: ' + blob_name) 35 | print('Local file: ' + local_file_name) 36 | 37 | if not os.path.isfile(local_file_name): 38 | print("Error: File Not Found!!") 39 | sys.exit(1) 40 | 41 | print('Uploading an object to Cloud Storage bucket from a file ...') 42 | 43 | # Instantiate the client. 44 | client = storage.Client() 45 | 46 | try: 47 | # Get the bucket. 48 | bucket = client.get_bucket(bucket_name) 49 | # Instantiate the object. 50 | blob = bucket.blob(blob_name) 51 | # Uploads a file to the bucket. 52 | blob.upload_from_filename(local_file_name) 53 | print('\nUploaded') 54 | except NotFound: 55 | print('Error: Bucket does NOT exists!!') 56 | pass 57 | except Forbidden: 58 | print('Error: Forbidden, you do not have access to it!!') 59 | pass 60 | 61 | return 62 | 63 | 64 | # This is the standard boilerplate that calls the main() function. 65 | if __name__ == '__main__': 66 | main() 67 | --------------------------------------------------------------------------------