├── .gitignore ├── LICENSE ├── README.md ├── awsec2instances-capi ├── README.md ├── awshelper.py └── ec2instances.py ├── awsec2instances-rapi ├── README.md ├── awshelper.py └── ec2instances.py ├── awslambdacreate-capi ├── README.md └── lambdacreate.py ├── awslambdadelete-capi ├── README.md └── lambdadelete.py ├── awslambdahello ├── README.md ├── lambda_function.py └── testhello ├── awslambdahellojson ├── README.md ├── lambda_function.py └── testhello.json ├── awslambdahttprequest ├── README.md └── lambda_function.py ├── awslambdainvoke-capi ├── README.md └── lambdainvoke.py ├── awslambdalist-capi ├── README.md └── lambdalist.py ├── awslambdalistall-capi ├── README.md └── lambdalistall.py ├── awslambdas3copy-capi ├── README.md └── lambda_function.py ├── awslambdas3copy-rapi ├── README.md └── lambda_function.py ├── awslambdas3event ├── README.md └── lambda_function.py ├── awslambdas3move-capi ├── README.md └── lambda_function.py ├── awslambdas3move-rapi ├── README.md └── lambda_function.py ├── awslambdaupdate-capi ├── README.md └── lambdaupdate.py ├── awss3copy-capi ├── README.md └── s3copy.py ├── awss3copy-rapi ├── README.md └── s3copy.py ├── awss3create-capi ├── README.md └── s3create.py ├── awss3create-rapi ├── README.md └── s3create.py ├── awss3delete-capi ├── README.md └── s3delete.py ├── awss3delete-rapi ├── README.md └── s3delete.py ├── awss3deleteobject-capi ├── README.md └── s3deleteobject.py ├── awss3deleteobject-rapi ├── README.md └── s3deleteobject.py ├── awss3download-capi ├── README.md └── s3download.py ├── awss3download-rapi ├── README.md └── s3download.py ├── awss3list-capi ├── README.md └── s3list.py ├── awss3list-rapi ├── README.md └── s3list.py ├── awss3listall-capi ├── README.md └── s3listall.py ├── awss3listall-rapi ├── README.md └── s3listall.py ├── awss3move-capi ├── README.md └── s3move.py ├── awss3move-rapi ├── README.md └── s3move.py ├── awss3upload-capi ├── README.md └── s3upload.py └── awss3upload-rapi ├── README.md └── s3upload.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 AWS (Amazon Web Services) 2 | 3 | This repo contains Python code examples on AWS (Amazon Web Services). 4 | 5 | These examples show how to use Python 3 and Boto 3 in order to manage Amazon services on AWS. These use Resource API (high-level) and Client API (low-level) of Boto 3. 6 | 7 | Boto 3 is the AWS (Amazon Web Services) SDK for Python, which allows Python developers to write software that makes use of Amazon services like EC2 and S3. Boto provides an easy to use, object-oriented API as well as low-level direct service access. 8 | 9 | Boto 3 offers two different styles of API: 10 | 11 | * Resource API (high-level): It provides an object-oriented abstraction on top (object.delete(), object.put(), etc.). 12 | * Client API (low-level): It maps directly to the underlying RPC-style service operations (put_object, delete_object, etc.). 13 | 14 | ## Quick start 15 | 16 | You must have an [AWS (Amazon Web Services)](http://aws.amazon.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 - Amazon EC2:** 25 | 26 | * Using the Client API (low-level) of Boto 3: 27 | * [awsec2instances-capi](/awsec2instances-capi) - AWS EC2 instances (Client API): Example of how to handle AWS EC2 instances. It uses the Client API (low-level) of Boto 3. 28 | 29 | * Using the Resource API (high-level) of Boto 3: 30 | * [awsec2instances-rapi](/awsec2instances-rapi) - AWS EC2 instances (Resource API): Example of how to handle AWS EC2 instances. It uses the Resource API (high-level) of Boto 3. 31 | 32 | **Compute - AWS Lambda:** 33 | 34 | * Lambda function handler: 35 | * [awslambdahello](/awslambdahello) - AWS Lambda Function Hello World: Example of how to handle an AWS simple Lambda function and a text input. 36 | * [awslambdahellojson](/awslambdahellojson) - AWS Lambda Function Hello World JSON: Example of how to handle an AWS simple Lambda function and a JSON input. 37 | * [awslambdahttprequest](/awslambdahttprequest) - AWS Lambda Function Http Request: Example of how to handle an AWS Lambda Function, show the parameters of the request and repond a message including the parameters. 38 | * [awslambdas3event](/awslambdas3event) - AWS Lambda Function S3 Event: Example of how to handle an AWS Lambda Function and send information to the log about an object when it appears in a S3 bucket. 39 | 40 | * Lambda function handler using the Client API (low-level) of Boto 3: 41 | * [awslambdas3copy-capi](/awslambdas3copy-capi) - AWS Lambda Function S3 Copy: Example of how to handle an AWS Lambda function and copy an object when it appears in a S3 bucket to another S3 bucket. It uses the Client API (low-level) of Boto 3. 42 | * [awslambdas3move-capi](/awslambdas3move-capi) - AWS Lambda Function S3 Move: Example of how to handle an AWS Lambda function and move an object when it appears in a S3 bucket to another S3 bucket. It uses the Client API (low-level) of Boto 3. 43 | 44 | * Lambda function handler using the Resource API (high-level) of Boto 3: 45 | * [awslambdas3copy-rapi](/awslambdas3copy-rapi) - AWS Lambda Function S3 Copy: Example of how to handle an AWS Lambda function and copy an object when it appears in a S3 bucket to another S3 bucket. It uses the Resource API (high-level) of Boto 3. 46 | * [awslambdas3move-rapi](/awslambdas3move-rapi) - AWS Lambda Function S3 Move: Example of how to handle an AWS Lambda function and move an object when it appears in a S3 bucket to another S3 bucket. It uses the Resource API (high-level) of Boto 3. 47 | 48 | * Lambda service client - Service operations - Using the Client API (low-level) of Boto 3: 49 | * [awslambdacreate-capi](/awslambdacreate-capi) - AWS Lambda Function Create: Example of how to handle an AWS Lambda function and create it. It uses the Client API (low-level) of Boto 3. 50 | * [awslambdaupdate-capi](/awslambdaupdate-capi) - AWS Lambda Function Update: Example of how to handle an AWS Lambda function and update it. It uses the Client API (low-level) of Boto 3. 51 | * [awslambdainvoke-capi](/awslambdainvoke-capi) - AWS Lambda Function Invoke: Example of how to handle an AWS Lambda function and invoke it. It uses the Client API (low-level) of Boto 3. 52 | * [awslambdalist-capi](/awslambdalist-capi) - AWS Lambda Function List: Example of how to handle an AWS Lambda function and list its information. It uses the Client API (low-level) of Boto 3. 53 | * [awslambdalistall-capi](/awslambdalistall-capi) - AWS Lambda Function List All: Example of how to handle AWS Lambda functions and list all Lambda functions and their information. It uses the Client API (low-level) of Boto 3. 54 | * [awslambdadelete-capi](/awslambdadelete-capi) - AWS Lambda Function Delete: Example of how to handle an AWS Lambda function and delete it. It uses the Client API (low-level) of Boto 3. 55 | 56 | **Storage - Amazon S3:** 57 | 58 | * Using the Client API (low-level) of Boto 3: 59 | * [awss3create-capi](/awss3create-capi) - AWS S3 Create (Client API): Example of how to handle S3 buckets and create a new S3 bucket. It uses the Client API (low-level) of Boto 3. 60 | * [awss3delete-capi](/awss3delete-capi) - AWS S3 Delete (Client API): Example of how to handle S3 buckets and delete a S3 bucket. It uses the Client API (low-level) of Boto 3. 61 | * [awss3list-capi](/awss3list-capi) - AWS S3 List (Client API): Example of how to handle S3 buckets and list information about objects in a S3 bucket. It uses the Client API (low-level) of Boto 3. 62 | * [awss3listall-capi](/awss3listall-capi) - AWS S3 List All (Client API): Example of how to handle S3 buckets and list information about all S3 buckets and the objects they contain. It uses the Client API (low-level) of Boto 3. 63 | * [awss3upload-capi](/awss3upload-capi) - AWS S3 List (Client API): Example of how to handle S3 buckets and upload a local file to a S3 bucket. It uses the Client API (low-level) of Boto 3. 64 | * [awss3download-capi](/awss3download-capi) - AWS S3 List (Client API): Example of how to handle S3 buckets and download an object from a S3 bucket to a local file. It uses the Client API (low-level) of Boto 3. 65 | * [awss3deleteobject-capi](/awss3deleteobject-capi) - AWS S3 Delete Object (Client API): Example of how to handle S3 buckets and delete an object in a S3 bucket. It uses the Client API (low-level) of Boto 3. 66 | * [awss3copy-capi](/awss3copy-capi) - AWS S3 Copy (Client API): Example of how to handle S3 buckets and copy an object from a S3 bucket to another S3 bucket. It uses the Client API (low-level) of Boto 3. 67 | * [awss3move-capi](/awss3move-capi) - AWS S3 Move (Client API): Example of how to handle S3 buckets and move an object from a S3 bucket to another S3 bucket. It uses the Client API (low-level) of Boto 3. 68 | 69 | * Using the Resource API (high-level) of Boto 3: 70 | * [awss3create-rapi](/awss3create-rapi) - AWS S3 Create (Resource API): Example of how to handle S3 buckets and create a new S3 bucket. It uses the Resource API (high-level) of Boto 3. 71 | * [awss3delete-rapi](/awss3delete-rapi) - AWS S3 Delete (Resource API): Example of how to handle S3 buckets and delete a S3 bucket. It uses the Resource API (high-level) of Boto 3. 72 | * [awss3list-rapi](/awss3list-rapi) - AWS S3 List (Resource API): Example of how to handle S3 buckets and list information about objects in a S3 bucket. It uses the Resource API (high-level) of Boto 3. 73 | * [awss3listall-rapi](/awss3listall-rapi) - AWS S3 List All (Resource API): Example of how to handle S3 buckets and list information about all S3 buckets and the objects they contain. It uses the Resource API (high-level) of Boto 3. 74 | * [awss3upload-rapi](/awss3upload-rapi) - AWS S3 List (Resource API): Example of how to handle S3 buckets and upload a local file to a S3 bucket. It uses the Resource API (high-level) of Boto 3. 75 | * [awss3download-rapi](/awss3download-rapi) - AWS S3 List (Resource API): Example of how to handle S3 buckets and download an object from a S3 bucket to a local file. It uses the Resource API (high-level) of Boto 3. 76 | * [awss3deleteobject-rapi](/awss3deleteobject-rapi) - AWS S3 Delete Object (Resource API): Example of how to handle S3 buckets and delete an object in a S3 bucket. It uses the Resource API (high-level) of Boto 3. 77 | * [awss3copy-rapi](/awss3copy-rapi) - AWS S3 Copy (Resource API): Example of how to handle S3 buckets and copy an object from a S3 bucket to another S3 bucket. It uses the Resource API (high-level) of Boto 3. 78 | * [awss3move-rapi](/awss3move-rapi) - AWS S3 Move (Resource API): Example of how to handle S3 buckets and move an object from a S3 bucket to another S3 bucket. It uses the Resource API (high-level) of Boto 3. 79 | 80 | ## License 81 | 82 | This code is released under the MIT License. See LICENSE file. 83 | -------------------------------------------------------------------------------- /awsec2instances-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS EC2 Instances Python example 2 | 3 | This folder contains a Python application example that handles EC2 instances on AWS (Amazon Web Services). 4 | 5 | Manage Amazon EC2 Instances using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Run the code. 74 | 75 | Run application: 76 | 77 | ```bash 78 | python ec2instances.py 79 | ``` 80 | 81 | You can select an option in the menu in order to run every command: 82 | 83 | * 1 = Describe all instances 84 | * 2 = Run new instance 85 | * 3 = Describe instance 86 | * 4 = Start instance 87 | * 5 = Stop instance 88 | * 6 = Reboot instance 89 | * 7 = Terminate instance 90 | 91 | * Test the application. 92 | 93 | You should see the new instance and modification of states with the AWS console. 94 | -------------------------------------------------------------------------------- /awsec2instances-capi/awshelper.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # awshelper.py 4 | # It has methods for managing AWS EC2 instances. 5 | # It uses Client API (low-level) of Boto3. 6 | 7 | import sys 8 | import boto3 9 | import botocore 10 | 11 | region = 'eu-west-1' # AWS region 12 | 13 | def describe_instances(): 14 | """ 15 | Describes all EC2 instances associated with an AWS account 16 | """ 17 | # Create an EC2 Client 18 | ec2_client = boto3.client('ec2', region_name=region) 19 | # Describe instances 20 | response = ec2_client.describe_instances() 21 | print(response) 22 | print('\nInstances:') 23 | for i in response['Reservations']: 24 | for j in i['Instances']: 25 | print('state of the instance "' + j['InstanceId'] + '" is: "' + j['State']['Name'] + '"') 26 | return 27 | 28 | 29 | def run_instance(): 30 | """ 31 | Run an EC2 instance 32 | """ 33 | ami_id = "ami-785db401" # AMI Id 34 | instance_type = "t2.micro" # Instance Type 35 | 36 | # Create an EC2 Client 37 | ec2_client = boto3.client('ec2', region_name=region) 38 | # Run an instance 39 | response = ec2_client.run_instances(ImageId=ami_id, InstanceType=instance_type, 40 | MaxCount=1, MinCount=1) 41 | print(response) 42 | Instance_id = response['Instances'][0]['InstanceId'] 43 | print('\nInstance Id: ' + Instance_id) 44 | print('Image Id: ' + response['Instances'][0]['ImageId']) 45 | print('Instance Type: ' + response['Instances'][0]['InstanceType']) 46 | print('State: ' + response['Instances'][0]['State']['Name']) 47 | 48 | return Instance_id 49 | 50 | 51 | def describe_instance(instance_id): 52 | """ 53 | Describes an EC2 instance 54 | """ 55 | # Create an EC2 Client 56 | ec2_client = boto3.client('ec2', region_name=region) 57 | try: 58 | # Describe an instance 59 | response = ec2_client.describe_instances(InstanceIds=[instance_id]) 60 | print(response) 61 | print('\nInstance Id: ' + instance_id) 62 | print('Instance Id: ' + response['Reservations'][0]['Instances'][0]['InstanceId']) 63 | print('Image Id: ' + response['Reservations'][0]['Instances'][0]['ImageId']) 64 | print('Instance Type: ' + response['Reservations'][0]['Instances'][0]['InstanceType']) 65 | print('State: ' + response['Reservations'][0]['Instances'][0]['State']['Name']) 66 | if response['Reservations'][0]['Instances'][0]['State']['Name'] == 'running': 67 | print('Private DNS Name: ' + response['Reservations'][0]['Instances'][0]['PrivateDnsName']) 68 | print('Private IP: ' + response['Reservations'][0]['Instances'][0]['PrivateIpAddress']) 69 | print('Public DNS Name: ' + response['Reservations'][0]['Instances'][0]['PublicDnsName']) 70 | print('Public IP: ' + response['Reservations'][0]['Instances'][0]['PublicIpAddress']) 71 | except botocore.exceptions.ClientError as e: 72 | if e.response['Error']['Code'] == "MissingParameter": 73 | print("Error: Missing instance id!!") 74 | else: 75 | raise 76 | return 77 | 78 | 79 | def start_instance(instance_id): 80 | """ 81 | Start an EC2 instance 82 | """ 83 | # Create an EC2 Client 84 | ec2_client = boto3.client('ec2', region_name=region) 85 | try: 86 | # Start an instance 87 | response = ec2_client.start_instances(InstanceIds=[instance_id], DryRun=False) 88 | print(response) 89 | print("\nSuccessfully starting instance: ", instance_id) 90 | except botocore.exceptions.ClientError as e: 91 | if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": 92 | print("Error: Invalid instance id!!") 93 | else: 94 | raise 95 | return 96 | 97 | 98 | def stop_instance(instance_id): 99 | """ 100 | Stop an EC2 instance 101 | """ 102 | # Create an EC2 Client 103 | ec2_client = boto3.client('ec2', region_name=region) 104 | try: 105 | # Stop an instance 106 | response = ec2_client.stop_instances(InstanceIds=[instance_id], DryRun=False) 107 | print(response) 108 | print("\nSuccessfully stopping instance: ", instance_id) 109 | except botocore.exceptions.ClientError as e: 110 | if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": 111 | print("Error: Invalid instance id!!") 112 | else: 113 | raise 114 | return 115 | 116 | 117 | def reboot_instance(instance_id): 118 | """ 119 | Reboot an EC2 instance 120 | """ 121 | # Create an EC2 Client 122 | ec2_client = boto3.client('ec2', region_name=region) 123 | try: 124 | # Reboot an instance 125 | response = ec2_client.reboot_instances(InstanceIds=[instance_id], DryRun=False) 126 | print(response) 127 | print("\nSuccessfully rebooting instance: ", instance_id) 128 | except botocore.exceptions.ClientError as e: 129 | if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": 130 | print("Error: Invalid instance id!!") 131 | else: 132 | raise 133 | return 134 | 135 | 136 | def terminate_instance(instance_id): 137 | """ 138 | Terminate an EC2 instance 139 | """ 140 | # Create an EC2 Client 141 | ec2_client = boto3.client('ec2', region_name=region) 142 | try: 143 | # Terminate an instance 144 | response = ec2_client.terminate_instances(InstanceIds=[instance_id], DryRun=False) 145 | print(response) 146 | print("\nSuccessfully terminating instance: ", instance_id) 147 | except botocore.exceptions.ClientError as e: 148 | if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": 149 | print("Error: Invalid instance id!!") 150 | else: 151 | raise 152 | return 153 | -------------------------------------------------------------------------------- /awsec2instances-capi/ec2instances.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # ec2instances.py 4 | # It is an example of how to handle AWS EC2 instances. 5 | # It uses Client API (low-level) of Boto3. 6 | 7 | import sys 8 | import awshelper 9 | 10 | def print_menu(): 11 | print('\nMENU') 12 | print('0 = Quit') 13 | print('1 = Describe all instances') 14 | print('2 = Run new instance') 15 | print('3 = Describe instance') 16 | print('4 = Start instance') 17 | print('5 = Stop instance') 18 | print('6 = Reboot instance') 19 | print('7 = Terminate instance') 20 | return 21 | 22 | 23 | def main(): 24 | instance_id = '' 25 | option = -1 26 | 27 | while option != 0: 28 | print_menu() 29 | try: 30 | option = int(input('\nEnter an option? ')) 31 | if option == 0: 32 | print('Bye') 33 | elif option == 1: 34 | awshelper.describe_instances() 35 | elif option == 2: 36 | instance_id = awshelper.run_instance() 37 | elif option == 3: 38 | awshelper.describe_instance(instance_id) 39 | elif option == 4: 40 | awshelper.start_instance(instance_id) 41 | elif option == 5: 42 | awshelper.stop_instance(instance_id) 43 | elif option == 6: 44 | awshelper.reboot_instance(instance_id) 45 | elif option == 7: 46 | awshelper.terminate_instance(instance_id) 47 | else: 48 | print('\nERROR: Enter a valid option!!') 49 | except ValueError: 50 | print('\nERROR: Enter a valid option!!') 51 | 52 | return 53 | 54 | 55 | # This is the standard boilerplate that calls the main() function. 56 | if __name__ == '__main__': 57 | main() 58 | -------------------------------------------------------------------------------- /awsec2instances-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS EC2 Instances Python example 2 | 3 | This folder contains a Python application example that handles EC2 instances on AWS (Amazon Web Services). 4 | 5 | Manage Amazon EC2 Instances using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Run the code. 74 | 75 | Run application: 76 | 77 | ```bash 78 | python ec2instances.py 79 | ``` 80 | 81 | You can select an option in the menu in order to run every command: 82 | 83 | * 1 = Describe all instances 84 | * 2 = Run new instance 85 | * 3 = Describe instance 86 | * 4 = Start instance 87 | * 5 = Stop instance 88 | * 6 = Reboot instance 89 | * 7 = Terminate instance 90 | 91 | * Test the application. 92 | 93 | You should see the new instance and modification of states with the AWS console. 94 | -------------------------------------------------------------------------------- /awsec2instances-rapi/awshelper.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # awshelper.py 4 | # It has methods for managing AWS EC2 instances. 5 | # It uses Resource API (high-level) of Boto3. 6 | 7 | import sys 8 | import boto3 9 | import botocore 10 | 11 | region = 'eu-west-1' # AWS region 12 | 13 | def describe_instances(): 14 | """ 15 | Describes all EC2 instances associated with an AWS account 16 | """ 17 | # Instantiate the service resource object 18 | ec2_resource = boto3.resource('ec2', region_name=region) 19 | # Describe instances 20 | instances = ec2_resource.instances.all() 21 | for instance in instances: 22 | print('State of the instance "' + instance.id + '" is: "' + instance.state['Name'] + '"') 23 | return 24 | 25 | 26 | def run_instance(): 27 | """ 28 | Run an EC2 instance 29 | """ 30 | ami_id = "ami-785db401" # AMI Id 31 | instance_type = "t2.micro" # Instance Type 32 | 33 | # Instantiate the service resource object 34 | ec2_resource = boto3.resource('ec2', region_name=region) 35 | # Run an instance 36 | instances = ec2_resource.create_instances(ImageId=ami_id, InstanceType=instance_type, 37 | MaxCount=1, MinCount=1) 38 | Instance_id = instances[0].id 39 | print('\nInstance Id: ' + Instance_id) 40 | print('Image Id: ' + instances[0].image_id) 41 | print('Instance Type: ' + instances[0].instance_type) 42 | print('State: ' + instances[0].state['Name']) 43 | return Instance_id 44 | 45 | 46 | def describe_instance(instance_id): 47 | """ 48 | Describes an EC2 instance 49 | """ 50 | # Instantiate the service resource object 51 | ec2_resource = boto3.resource('ec2', region_name=region) 52 | try: 53 | # Describe an instance 54 | instance = ec2_resource.Instance(instance_id) 55 | print('\nInstance Id: ' + instance_id) 56 | print('Instance Id: ' + instance.id) 57 | print('Image Id: ' + instance.image_id) 58 | print('Instance Type: ' + instance.instance_type) 59 | print('State: ' + instance.state['Name']) 60 | if instance.state['Name'] == 'running': 61 | print('Private DNS Name: ' + instance.private_dns_name) 62 | print('Private IP: ' + instance.private_ip_address) 63 | print('Public DNS Name: ' + instance.public_dns_name) 64 | print('Public IP: ' + instance.public_ip_address) 65 | except botocore.exceptions.ClientError as e: 66 | if e.response['Error']['Code'] == "MissingParameter": 67 | print("Error: Missing instance id!!") 68 | else: 69 | raise 70 | return 71 | 72 | 73 | def start_instance(instance_id): 74 | """ 75 | Start an EC2 instance 76 | """ 77 | # Instantiate the service resource object 78 | ec2_resource = boto3.resource('ec2', region_name=region) 79 | try: 80 | # Start an instance 81 | response = ec2_resource.Instance(instance_id).start(DryRun=False) 82 | print(response) 83 | print("\nSuccessfully starting instance: ", instance_id) 84 | except botocore.exceptions.ClientError as e: 85 | if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": 86 | print("Error: Invalid instance id!!") 87 | else: 88 | raise 89 | return 90 | 91 | 92 | def stop_instance(instance_id): 93 | """ 94 | Stop an EC2 instance 95 | """ 96 | # Instantiate the service resource object 97 | ec2_resource = boto3.resource('ec2', region_name=region) 98 | try: 99 | # Stop an instance 100 | response = ec2_resource.Instance(instance_id).stop(DryRun=False) 101 | print(response) 102 | print("\nSuccessfully stopping instance: ", instance_id) 103 | except botocore.exceptions.ClientError as e: 104 | if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": 105 | print("Error: Invalid instance id!!") 106 | else: 107 | raise 108 | return 109 | 110 | 111 | def reboot_instance(instance_id): 112 | """ 113 | Reboot an EC2 instance 114 | """ 115 | # Instantiate the service resource object 116 | ec2_resource = boto3.resource('ec2', region_name=region) 117 | try: 118 | # Reboot an instance 119 | response = ec2_resource.Instance(instance_id).reboot(DryRun=False) 120 | print(response) 121 | print("\nSuccessfully rebooting instance: ", instance_id) 122 | except botocore.exceptions.ClientError as e: 123 | if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": 124 | print("Error: Invalid instance id!!") 125 | else: 126 | raise 127 | return 128 | 129 | 130 | def terminate_instance(instance_id): 131 | """ 132 | Terminate an EC2 instance 133 | """ 134 | # Instantiate the service resource object 135 | ec2_resource = boto3.resource('ec2', region_name=region) 136 | try: 137 | # Terminate an instance 138 | response = ec2_resource.Instance(instance_id).terminate(DryRun=False) 139 | print(response) 140 | print("\nSuccessfully terminating instance: ", instance_id) 141 | except botocore.exceptions.ClientError as e: 142 | if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": 143 | print("Error: Invalid instance id!!") 144 | else: 145 | raise 146 | return 147 | -------------------------------------------------------------------------------- /awsec2instances-rapi/ec2instances.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # ec2instances.py 4 | # It is an example of how to handle AWS EC2 instances. 5 | # It uses Resource API (high-level) of Boto3. 6 | 7 | import sys 8 | import awshelper 9 | 10 | def print_menu(): 11 | print('\nMENU') 12 | print('0 = Quit') 13 | print('1 = Describe all instances') 14 | print('2 = Run new instance') 15 | print('3 = Describe instance') 16 | print('4 = Start instance') 17 | print('5 = Stop instance') 18 | print('6 = Reboot instance') 19 | print('7 = Terminate instance') 20 | return 21 | 22 | 23 | def main(): 24 | instance_id = '' 25 | option = -1 26 | 27 | while option != 0: 28 | print_menu() 29 | try: 30 | option = int(input('\nEnter an option? ')) 31 | if option == 0: 32 | print('Bye') 33 | elif option == 1: 34 | awshelper.describe_instances() 35 | elif option == 2: 36 | instance_id = awshelper.run_instance() 37 | elif option == 3: 38 | awshelper.describe_instance(instance_id) 39 | elif option == 4: 40 | awshelper.start_instance(instance_id) 41 | elif option == 5: 42 | awshelper.stop_instance(instance_id) 43 | elif option == 6: 44 | awshelper.reboot_instance(instance_id) 45 | elif option == 7: 46 | awshelper.terminate_instance(instance_id) 47 | else: 48 | print('\nERROR: Enter a valid option!!') 49 | except ValueError: 50 | print('\nERROR: Enter a valid option!!') 51 | 52 | return 53 | 54 | 55 | # This is the standard boilerplate that calls the main() function. 56 | if __name__ == '__main__': 57 | main() 58 | -------------------------------------------------------------------------------- /awslambdacreate-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function Create Python example 2 | 3 | This folder contains a Python application example that handles Lambda functions on AWS (Amazon Web Services). 4 | 5 | Create an AWS Lambda function using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * You need a `.py` file where the code of the Lambda function is located. 74 | 75 | You can use the code obtained from the AWS Lambda Function Hello World JSON Java example: [awslambdahellojson](/awslambdahellojson). 76 | 77 | This application compresses the `.py` file in a `ZIP` file to deploy. 78 | 79 | * You can select the AWS region of the Lambda function changing the value of `REGION` variable in the code. 80 | 81 | * You have to create an AWS role that has Lambda permissions. 82 | 83 | * Run the code. 84 | 85 | You must provide 4 parameters, replace the values of: 86 | 87 | * `` by Lambda function name. 88 | * `` by path to the `.py` file where the code of the Lambda function is located. 89 | * `` by role ARN that has Lambda permissions. 90 | * `` by fully qualifed method name (Ex: lambda_function.lambda_handler). 91 | 92 | Run application: 93 | 94 | ```bash 95 | python lambdacreate.py 96 | ``` 97 | 98 | You must use as a function role the ARN. 99 | 100 | Ex.: `arn:aws:iam::123456789012:role/service-role/lambda-basic-execution` 101 | 102 | * Test the application. 103 | 104 | You should see the response of the Lambda function. 105 | 106 | For example: 107 | 108 | * `Creating function ...` 109 | * `Created function "FunctionName" with ARN: "arn:aws:lambda:eu-west-1:123456789012:function:FunctionName"` 110 | 111 | And the Lambda function created. 112 | -------------------------------------------------------------------------------- /awslambdacreate-capi/lambdacreate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # lambdacreate.py 4 | # It is an example that handles Lambda functions on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Create a Lambda function. 7 | # You must provide 1 parameter: 8 | # FUNCTION_NAME = Lambda function name 9 | # FUNCTION_FILE = The path to the JAR or ZIP firl where the code of the Lambda function is located 10 | # FUNCTION_ROLE = The role ARN that has Lambda permissions 11 | # FUNCTION_HANDLER = The fully qualifed method name (Ex: lambda_function.lambda_handler) 12 | 13 | import sys 14 | import io 15 | import zipfile 16 | import boto3 17 | import botocore 18 | 19 | def create_lambda_deployment_package(function_file_name): 20 | # Creates a Lambda deployment package in ZIP format in an in-memory buffer. 21 | # This buffer can be passed directly to AWS Lambda when creating the function. 22 | # Parameters: 23 | # function_file_name (string): The name of the file that contains the Lambda handler function. 24 | # Return: 25 | # The deployment package. 26 | 27 | buffer = io.BytesIO() 28 | with zipfile.ZipFile(buffer, 'w') as zipped: 29 | zipped.write(function_file_name) 30 | buffer.seek(0) 31 | return buffer.read() 32 | 33 | 34 | def main(): 35 | 36 | REGION = 'eu-west-1' # AWS region 37 | 38 | # Make a list of command line arguments, omitting the [0] element 39 | # which is the script itself. 40 | args = sys.argv[1:] 41 | if len(args) < 4: 42 | print('Not enough parameters.\n'\ 43 | 'Proper Usage is: python lambdacreate.py '\ 44 | ' ') 45 | sys.exit(1) 46 | 47 | function_name = args[0] 48 | function_file = args[1] 49 | function_role = args[2] 50 | function_handler = args[3] 51 | 52 | print('Lambda function name: ' + function_name) 53 | print('Lambda function file: ' + function_file) 54 | print('Lambda function role: ' + function_role) 55 | print('Lambda function handler: ' + function_handler) 56 | 57 | # Create a Lambda Client 58 | lambda_client = boto3.client('lambda', region_name=REGION) 59 | 60 | # Create Lambda function 61 | try: 62 | print('Creating function ...') 63 | 64 | deployment_package = create_lambda_deployment_package(function_file) 65 | 66 | response = lambda_client.create_function( 67 | FunctionName=function_name, 68 | Description='Created by the Lambda Python API', 69 | Runtime='python3.8', 70 | Role=function_role, 71 | Handler=function_handler, 72 | Code={'ZipFile': deployment_package}, 73 | Publish=True) 74 | 75 | print('Response:') 76 | print(response) 77 | function_arn = response['FunctionArn'] 78 | print('\nCreated function "' + function_name + '" with ARN: "' + function_arn + '"') 79 | 80 | except botocore.exceptions.ClientError as e: 81 | if e.response['Error']['Code'] == "AccessDeniedException": 82 | print("Error: Access Denied!!") 83 | elif e.response['Error']['Code'] == "ValidationException": 84 | print("Error: Name Not Valid!!") 85 | else: 86 | raise 87 | 88 | return 89 | 90 | 91 | # This is the standard boilerplate that calls the main() function. 92 | if __name__ == '__main__': 93 | main() 94 | -------------------------------------------------------------------------------- /awslambdadelete-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function Delete Python example 2 | 3 | This folder contains a Python application example that handles Lambda functions on AWS (Amazon Web Services). 4 | 5 | Delete an AWS Lambda function using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * You can create a Lambda function on AWS. 74 | 75 | You can use the AWS Lambda Function Hello World JSON Python example: [awslambdahellojson](/awslambdahellojson). 76 | 77 | * You can select the AWS region of the Lambda function changing the value of `REGION` variable in the code. 78 | 79 | * Run the code. 80 | 81 | You must provide 1 parameter, replace the value of: 82 | 83 | * `` by Lambda function name. 84 | 85 | Run application: 86 | 87 | ```bash 88 | python lambdadelete.py 89 | ``` 90 | 91 | You can use as name of the Lambda function 2 name formats: 92 | 93 | * Function name 94 | 95 | Ex.: `HelloJsonPython` 96 | 97 | * Function ARN 98 | 99 | Ex.: `arn:aws:lambda:eu-west-1:123456789012:function:HelloJsonPython` 100 | 101 | You can retrieve the function ARN by looking at the function in the AWS Console. 102 | 103 | * Test the application. 104 | 105 | The Lambda function is deleted and you should see the message "Deleted". 106 | 107 | -------------------------------------------------------------------------------- /awslambdadelete-capi/lambdadelete.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # lambdadelete.py 4 | # It is an example that handles Lambda functions on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Delete a Lambda function. 7 | # You must provide 1 parameter: 8 | # FUNCTION_NAME = Lambda function name 9 | 10 | import sys 11 | import boto3 12 | import botocore 13 | 14 | def main(): 15 | 16 | REGION = 'eu-west-1' # AWS region 17 | 18 | # Make a list of command line arguments, omitting the [0] element 19 | # which is the script itself. 20 | args = sys.argv[1:] 21 | if len(args) < 1: 22 | print('Not enough parameters.\n'\ 23 | 'Proper Usage is: python lambdadelete.py ') 24 | sys.exit(1) 25 | 26 | function_name = args[0] 27 | print('Lambda function name: ' + function_name) 28 | 29 | # Create a Lambda Client 30 | lambda_client = boto3.client('lambda', region_name=REGION) 31 | 32 | # Delete Lambda function 33 | try: 34 | print('Deleting function ...') 35 | 36 | response = lambda_client.delete_function( 37 | FunctionName=function_name) 38 | 39 | print('Response:') 40 | print(response) 41 | print('\nDeleted') 42 | 43 | except botocore.exceptions.ClientError as e: 44 | if e.response['Error']['Code'] == "ResourceNotFoundException": 45 | print("Error: Function Not Found!!") 46 | elif e.response['Error']['Code'] == "AccessDeniedException": 47 | print("Error: Access Denied!!") 48 | elif e.response['Error']['Code'] == "ValidationException": 49 | print("Error: Name Not Valid!!") 50 | else: 51 | raise 52 | 53 | return 54 | 55 | 56 | # This is the standard boilerplate that calls the main() function. 57 | if __name__ == '__main__': 58 | main() 59 | -------------------------------------------------------------------------------- /awslambdahello/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function Hello World Python example 2 | 3 | This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services). 4 | 5 | It handles a simple AWS Lambda function that shows the content (text) of the call to the lambda function and returns a message including this content. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for Python 3. 12 | 13 | ## Using the code 14 | 15 | * Access the AWS console. 16 | 17 | * Select AWS Lambda in the services menu. 18 | 19 | * Create an AWS lambda function. 20 | * Name: `` 21 | * Runtime: `Python 3.8` 22 | * Handler: `lambda_function.lambda_handler` 23 | * Role: `lambda-basic-execution` 24 | * Runtime Settings for the lambda function: 25 | * Memory (MB): `128` 26 | * Timeout: `3 sec` 27 | * The resources that the function's role has access to: 28 | * `Amazon CloudWatch Logs` 29 | * The triggers: 30 | * `Nothing` 31 | 32 | * Write the code. 33 | 34 | The content of `lambda_function.py` file. 35 | 36 | * Save the Lambda function. 37 | 38 | It deploys the Lambda function. 39 | 40 | * Create and configure a Test event. 41 | 42 | Input file content: 43 | 44 | ```bash 45 | "Peter" 46 | ``` 47 | 48 | * Run the code. 49 | 50 | Run the code in an AWS lambda function using the test button. 51 | 52 | * Test the AWS Lambda function. 53 | 54 | You should see the next message in the log: 55 | 56 | ```bash 57 | "Hello Peter" 58 | ``` 59 | -------------------------------------------------------------------------------- /awslambdahello/lambda_function.py: -------------------------------------------------------------------------------- 1 | # lamdda_function.py 2 | # It handles a simple AWS Lambda function that shows the content (text) of the call 3 | # to the lambda function and returns a message including this content. 4 | 5 | def lambda_handler(event, context): 6 | return 'Hello ' + event 7 | -------------------------------------------------------------------------------- /awslambdahello/testhello: -------------------------------------------------------------------------------- 1 | "Peter" 2 | -------------------------------------------------------------------------------- /awslambdahellojson/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function Hello World JSON Python example 2 | 3 | This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services). 4 | 5 | It handles a simple AWS Lambda function that shows the content (JSON) of the call to the lambda function and returns a message including this content. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for Python 3. 12 | 13 | ## Using the code 14 | 15 | * Access the AWS console. 16 | 17 | * Select AWS Lambda in the services menu. 18 | 19 | * Create an AWS lambda function. 20 | * Name: `` 21 | * Runtime: `Python 3.8` 22 | * Handler: `lambda_function.lambda_handler` 23 | * Role: `lambda-basic-execution` 24 | * Runtime Settings for the lambda function: 25 | * Memory (MB): `128` 26 | * Timeout: `3 sec` 27 | * The resources that the function's role has access to: 28 | * `Amazon CloudWatch Logs` 29 | * The triggers: 30 | * `Nothing` 31 | 32 | * Write the code. 33 | 34 | The content of `lambda_function.py` file. 35 | 36 | * Save the Lambda function. 37 | 38 | It deploys the Lambda function. 39 | 40 | * Create and configure a Test event. 41 | 42 | Input JSON file content: 43 | 44 | ```bash 45 | { 46 | "first_name": "Peter", 47 | "last_name": "Parker" 48 | } 49 | ``` 50 | 51 | * Run the code. 52 | 53 | Run the code in an AWS lambda function using the test button. 54 | 55 | * Test the AWS Lambda function. 56 | 57 | You should see the next message in the log: 58 | 59 | ```bash 60 | { 61 | "message": "Hello Peter Parker!" 62 | } 63 | ``` 64 | -------------------------------------------------------------------------------- /awslambdahellojson/lambda_function.py: -------------------------------------------------------------------------------- 1 | # lamdda_function.py 2 | # It handles a simple AWS Lambda function that shows the content (JSON) of the call 3 | # to the lambda function and returns a message including this content. 4 | 5 | def lambda_handler(event, context): 6 | message = 'Hello {} {}!'.format(event['first_name'], 7 | event['last_name']) 8 | return { 9 | 'message' : message 10 | } 11 | -------------------------------------------------------------------------------- /awslambdahellojson/testhello.json: -------------------------------------------------------------------------------- 1 | { 2 | "first_name": "Peter", 3 | "last_name": "Parker" 4 | } -------------------------------------------------------------------------------- /awslambdahttprequest/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function Http Request Python example 2 | 3 | This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services). 4 | 5 | It handles an AWS Lambda function that it is invoked by an http request. It shows the parameters of the request and responds a message including the parameters. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for Python 3. 12 | 13 | ## Using the code 14 | 15 | * Access the AWS console. 16 | 17 | * Select AWS Lambda in the services menu. 18 | 19 | * Create an AWS lambda function. 20 | * Name: `` 21 | * Runtime: `Python 3.8` 22 | * Handler: `lambda_function.lambda_handler` 23 | * Role: `lambda-basic-execution` 24 | * Runtime Settings for the lambda function: 25 | * Memory (MB): `128` 26 | * Timeout: `3 sec` 27 | * The resources that the function's role has access to: 28 | * `Amazon CloudWatch Logs` 29 | * The triggers: 30 | * `API Gateway` 31 | * Details below. 32 | 33 | * Write the code of the Lambda funtion. 34 | 35 | The content of `lambda_function.py` file. 36 | 37 | * Deploy the Lambda function. 38 | 39 | It deploys the Lambda function to AWS. 40 | 41 | * Create an `API Gateway` trigger. 42 | 43 | This allows to call the lambda function using an HTTP API. 44 | 45 | * Name: `-API` 46 | * API: `Create an API` 47 | * API type: `HTTP API` 48 | * Security: `Open` 49 | 50 | You will get an API endpoint, which can be copied and run in your browser's address bar. 51 | 52 | It looks like the following URL: 53 | 54 | ```bash 55 | https://.execute-api..amazonaws.com// 56 | ``` 57 | 58 | For example: 59 | 60 | ```bash 61 | https://abcdefg5jk.execute-api.eu-west-1.amazonaws.com/default/HttpRequestPython` 62 | ``` 63 | 64 | * Run the code. 65 | 66 | To run the code, you need to use 2 parameters: 67 | 68 | * `first_name` 69 | * `last_name` 70 | 71 | You call the API endpoint with this format: 72 | 73 | ```bash 74 | https://.execute-api..amazonaws.com//?first_name=&last_name= 75 | ``` 76 | 77 | For example: 78 | 79 | ```bash 80 | https://abcdefg5jk.execute-api.eu-west-1.amazonaws.com/default/HttpRequestPython?first_name=Peter&last_name=Parker 81 | ``` 82 | 83 | * Test the AWS Lambda function. 84 | 85 | Go to the URL of API endpoint that you have got: `https://.execute-api..amazonaws.com//?first_name=Peter&last_name=Parker` using a browser. 86 | 87 | You should see the next response if you have added the right paramenters: 88 | 89 | ```bash 90 | "Hello Peter Parker!" 91 | ``` 92 | 93 | You should see the next response if you have not added any paramenter: 94 | 95 | ```bash 96 | "Who are you?" 97 | ``` 98 | -------------------------------------------------------------------------------- /awslambdahttprequest/lambda_function.py: -------------------------------------------------------------------------------- 1 | # lamdda_function.py 2 | # It handles an AWS Lambda function that it is invoked by an http request. 3 | # It shows the parameters of the request and reponds a message including the parameters. 4 | 5 | import json 6 | 7 | def lambda_handler(event, context): 8 | if 'queryStringParameters' in event: # If parameters 9 | print(event['queryStringParameters']['first_name']) 10 | print(event['queryStringParameters']['last_name']) 11 | body = 'Hello {} {}!'.format(event['queryStringParameters']['first_name'], 12 | event['queryStringParameters']['last_name']) 13 | else: # If no parameters 14 | print('No parameters!') 15 | body = 'Who are you?' 16 | 17 | return { 18 | 'statusCode': 200, 19 | 'body': json.dumps(body) 20 | } 21 | -------------------------------------------------------------------------------- /awslambdainvoke-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function Invoke Python example 2 | 3 | This folder contains a Python application example that handles Lambda functions on AWS (Amazon Web Services). 4 | 5 | Invoke an AWS Lambda function using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * You can create a Lambda function on AWS. 74 | 75 | You can use the AWS Lambda Function Hello World JSON Python example: [awslambdahellojson](/awslambdahellojson). 76 | 77 | * You can select the AWS region of the Lambda function changing the value of `REGION` variable in the code. 78 | 79 | * You can change the values of the payload to the Lambda function in the code (payload): 80 | 81 | ```bash 82 | { 83 | 'first_name': 'Peter', 84 | 'last_name': 'Parker' 85 | } 86 | ``` 87 | 88 | * Run the code. 89 | 90 | You must provide 1 parameter, replace the value of: 91 | 92 | * `` by the Lambda function name. 93 | 94 | Run application: 95 | 96 | ```bash 97 | python lambdainvoke.py 98 | ``` 99 | 100 | You can use as name of the Lambda function 2 name formats: 101 | 102 | * Function name 103 | 104 | Ex.: `HelloJsonPython` 105 | 106 | * Function ARN 107 | 108 | Ex.: `arn:aws:lambda:eu-west-1:123456789012:function:HelloJsonPython` 109 | 110 | You can retrieve the function ARN by looking at the function in the AWS Console. 111 | 112 | * Test the application. 113 | 114 | You should see the response of the Lambda function. 115 | 116 | For example: 117 | 118 | * `Function response payload:` 119 | * `{'message': 'Hello Peter Parker!'}` 120 | -------------------------------------------------------------------------------- /awslambdainvoke-capi/lambdainvoke.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # lambdainvoke.py 4 | # It is an example that handles Lambda functions on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Invoke a Lambda function. 7 | # You must provide 1 parameter: 8 | # FUNCTION_NAME = Lambda function name 9 | 10 | import sys 11 | import json 12 | import boto3 13 | import botocore 14 | 15 | def main(): 16 | 17 | REGION = 'eu-west-1' # AWS region 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) < 1: 23 | print('Not enough parameters.\n'\ 24 | 'Proper Usage is: python lambdainvoke.py ') 25 | sys.exit(1) 26 | 27 | function_name = args[0] 28 | print('Lambda function name: ' + function_name) 29 | 30 | # Create a Lambda Client 31 | lambda_client = boto3.client('lambda', region_name=REGION) 32 | 33 | # Invoke lambda function 34 | try: 35 | print('Invoking function ...') 36 | payload = { 37 | 'first_name': 'Peter', 38 | 'last_name': 'Parker' 39 | } 40 | payload = json.dumps(payload) 41 | 42 | response = lambda_client.invoke( 43 | FunctionName=function_name, 44 | InvocationType='RequestResponse', 45 | Payload=payload) 46 | 47 | print('Response:') 48 | print(response) 49 | print('\nFunction response payload:') 50 | print(json.loads(response['Payload'].read())) 51 | 52 | except botocore.exceptions.ClientError as e: 53 | if e.response['Error']['Code'] == "ResourceNotFoundException": 54 | print("Error: Function Not Found!!") 55 | elif e.response['Error']['Code'] == "AccessDeniedException": 56 | print("Error: Access Denied!!") 57 | elif e.response['Error']['Code'] == "ValidationException": 58 | print("Error: Name Not Valid!!") 59 | else: 60 | raise 61 | 62 | return 63 | 64 | 65 | # This is the standard boilerplate that calls the main() function. 66 | if __name__ == '__main__': 67 | main() 68 | -------------------------------------------------------------------------------- /awslambdalist-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function List Python example 2 | 3 | This folder contains a Python application example that handles Lambda functions on AWS (Amazon Web Services). 4 | 5 | List information of an AWS Lambda function using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * You can create a Lambda function on AWS. 74 | 75 | You can use the AWS Lambda Function Hello World JSON Python example: [awslambdahellojson](/awslambdahellojson). 76 | 77 | * You can select the AWS region of the Lambda function changing the value of `REGION` variable in the code. 78 | 79 | * Run the code. 80 | 81 | You must provide 1 parameter, replace the value of: 82 | 83 | * `` by Lambda function name. 84 | 85 | Run application: 86 | 87 | ```bash 88 | python lambdalist.py 89 | ``` 90 | 91 | You can use as name of the Lambda function 2 name formats: 92 | 93 | * Function name 94 | 95 | Ex.: `HelloJsonPython` 96 | 97 | * Function ARN 98 | 99 | Ex.: `arn:aws:lambda:eu-west-1:123456789012:function:HelloJsonPython` 100 | 101 | You can retrieve the function ARN by looking at the function in the AWS Console. 102 | 103 | * Test the application. 104 | 105 | You should see the information of the Lambda function. 106 | -------------------------------------------------------------------------------- /awslambdalist-capi/lambdalist.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # lambdalist.py 4 | # It is an example that handles Lambda functions on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # List information of a Lambda function. 7 | # You must provide 1 parameter: 8 | # FUNCTION_NAME = Lambda function name 9 | 10 | import sys 11 | import boto3 12 | import botocore 13 | 14 | def main(): 15 | 16 | REGION = 'eu-west-1' # AWS region 17 | 18 | # Make a list of command line arguments, omitting the [0] element 19 | # which is the script itself. 20 | args = sys.argv[1:] 21 | if len(args) < 1: 22 | print('Not enough parameters.\n'\ 23 | 'Proper Usage is: python lambdalist.py ') 24 | sys.exit(1) 25 | 26 | function_name = args[0] 27 | print('Lambda function name: ' + function_name) 28 | 29 | # Create a Lambda Client 30 | lambda_client = boto3.client('lambda', region_name=REGION) 31 | 32 | # List lambda function configuration 33 | try: 34 | print('Listing function ...') 35 | 36 | config = lambda_client.get_function_configuration( 37 | FunctionName=function_name) 38 | 39 | print('Function name: ', config['FunctionName']) 40 | print(' - ARN: ', config['FunctionArn']) 41 | print(' - Runtime: ', config['Runtime']) 42 | print(' - Role: ', config['Role']) 43 | print(' - Handler: ', config['Handler']) 44 | print(' - Description: ', config['Description']) 45 | print(' - Timeout: ', config['Timeout']) 46 | print(' - MemorySize: ', config['MemorySize']) 47 | print(' - LastModified: ', config['LastModified']) 48 | print(' - Description: ', config['Description']) 49 | print(' - CodeSize: ', config['CodeSize']) 50 | print(' - Version: ', config['Version']) 51 | 52 | except botocore.exceptions.ClientError as e: 53 | if e.response['Error']['Code'] == "ResourceNotFoundException": 54 | print("Error: Function Not Found!!") 55 | elif e.response['Error']['Code'] == "AccessDeniedException": 56 | print("Error: Access Denied!!") 57 | elif e.response['Error']['Code'] == "ValidationException": 58 | print("Error: Name Not Valid!!") 59 | else: 60 | raise 61 | 62 | return 63 | 64 | 65 | # This is the standard boilerplate that calls the main() function. 66 | if __name__ == '__main__': 67 | main() 68 | -------------------------------------------------------------------------------- /awslambdalistall-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function List All Python example 2 | 3 | This folder contains a Python application example that handles Lambda functions on AWS (Amazon Web Services). 4 | 5 | List all AWS Lambda functions and their information using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * You can create some Lambda functions on AWS. 74 | 75 | * You can use the AWS Lambda Function Hello World JSON Python example: [awslambdahellojson](/awslambdahellojson). 76 | * You can use the AWS Lambda Function Hello World Python example: [awslambdahello](/awslambdahello). 77 | 78 | * You can select the AWS region of the Lambda functions changing the value of `REGION` variable in the code. 79 | 80 | * Run the code. 81 | 82 | Run application: 83 | 84 | ```bash 85 | python lambdalistall.py 86 | ``` 87 | 88 | * Test the application. 89 | 90 | You should see the list of all Lambda functions and their information. 91 | -------------------------------------------------------------------------------- /awslambdalistall-capi/lambdalistall.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # lambdalistall.py 4 | # It is an example that handles Lambda functions on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # List all Lambda functions and their information. 7 | 8 | import sys 9 | import boto3 10 | import botocore 11 | 12 | def main(): 13 | 14 | REGION = 'eu-west-1' # AWS region 15 | 16 | # Create a Lambda Client 17 | lambda_client = boto3.client('lambda', region_name=REGION) 18 | 19 | # List lambda functions 20 | try: 21 | print('Listing functions ...') 22 | list_functions_resp = lambda_client.list_functions() 23 | for function in list_functions_resp['Functions']: 24 | print('Function name: ', function['FunctionName']) 25 | print(' - ARN: ', function['FunctionArn']) 26 | print(' - Runtime: ', function['Runtime']) 27 | print(' - Role: ', function['Role']) 28 | print(' - Handler: ', function['Handler']) 29 | print(' - Description: ', function['Description']) 30 | print(' - Timeout: ', function['Timeout']) 31 | print(' - MemorySize: ', function['MemorySize']) 32 | print(' - LastModified: ', function['LastModified']) 33 | print(' - Description: ', function['Description']) 34 | print(' - CodeSize: ', function['CodeSize']) 35 | print(' - Version: ', function['Version']) 36 | 37 | except botocore.exceptions.ClientError as e: 38 | raise 39 | 40 | return 41 | 42 | 43 | # This is the standard boilerplate that calls the main() function. 44 | if __name__ == '__main__': 45 | main() 46 | -------------------------------------------------------------------------------- /awslambdas3copy-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function S3 Copy Python example 2 | 3 | This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services). 4 | 5 | It handles an AWS Lambda function that copies an object when it appears in a S3 bucket to another S3 bucket using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * The AWS Lambda execution environment include SDK for Python (Boto 3). 19 | 20 | ## Using the code 21 | 22 | * You can select the destination bucket name using an AWS Lambda environment variable: `TARGET_BUCKET` 23 | 24 | * Access the AWS console. 25 | 26 | * Create a S3 bucket for the source and another S3 bucket for the target. 27 | 28 | * Create an IAM Policy: ex. `Policy-my-buckets` 29 | 30 | Changing: 31 | 32 | * `sourcebucket` to the name of your source bucket. 33 | * `targetbucket` to the name of your target bucket. 34 | 35 | Content of the IAM policy: 36 | 37 | ```bash 38 | { 39 | "Version": "2012-10-17", 40 | "Statement": [ 41 | { 42 | "Effect": "Allow", 43 | "Action": [ 44 | "s3:GetObject" 45 | ], 46 | "Resource": [ 47 | "arn:aws:s3:::sourcebucket/*" 48 | ] 49 | }, 50 | { 51 | "Effect": "Allow", 52 | "Action": [ 53 | "s3:PutObject" 54 | ], 55 | "Resource": [ 56 | "arn:aws:s3:::targetbucket/*" 57 | ] 58 | }, 59 | { 60 | "Effect": "Allow", 61 | "Action": [ 62 | "cloudwatch:*" 63 | ], 64 | "Resource": [ 65 | "*" 66 | ] 67 | }, 68 | { 69 | "Effect": "Allow", 70 | "Action": [ 71 | "logs:*" 72 | ], 73 | "Resource": [ 74 | "*" 75 | ] 76 | } 77 | ] 78 | } 79 | ``` 80 | 81 | * Create a role: `Role-my-buckets`. 82 | 83 | This role uses the policy `Policy-my-buckets` 84 | 85 | * Create an AWS lambda function. 86 | * Name: `` 87 | * Runtime: `Python 3.8` 88 | * Handler: `lambda_function.lambda_handler` 89 | * Role: `Role-my-buckets` 90 | * Runtime Settings for the lambda function: 91 | * Memory (MB): `1024` 92 | * Timeout: `10 sec` 93 | * The resources that the function's role has access to: 94 | * `Amazon CloudWatch` 95 | * `Amazon CloudWatch Logs` 96 | * `Amazon S3` 97 | * Lambda obtained information from the policy statements: `Managed policy Policy-my-buckets`: 98 | * `s3:GetObject` --> `Allow: arn:aws:s3:::sourcebucket/*` 99 | * `s3:PutObject` --> `Allow: arn:aws:s3:::targetbucket/*` 100 | * The triggers: 101 | * `S3` 102 | * Bucket: `` 103 | * Event type: `ObjectCreated` 104 | * Enable trigger: `Yes` 105 | 106 | * Create the AWS Lambda environment variable `TARGET_BUCKET` and set its value to the name of your target bucket. 107 | 108 | * Write the code. 109 | 110 | The content of `lambda_function.py` file. 111 | 112 | * Save the Lambda function. 113 | 114 | It deploys the Lambda function. 115 | 116 | * Test the function: 117 | 118 | Copy a file in the source S3 bucket. 119 | 120 | The object from the source S3 bucket should be copied to the target S3 bucket. 121 | 122 | You should see the next messages in the log: 123 | 124 | ```bash 125 | "From - bucket:: " 126 | "From - object: " 127 | "To - bucket: " 128 | "To - object: " 129 | "Copying object ..." 130 | "Copied" 131 | ``` -------------------------------------------------------------------------------- /awslambdas3copy-capi/lambda_function.py: -------------------------------------------------------------------------------- 1 | # lamdda_function.py 2 | # It handles an AWS Lambda function that copies an object when it appears 3 | # in a S3 bucket to another S3 bucket. 4 | # It uses Client API (low-level) of Boto3. 5 | 6 | import os 7 | import boto3 8 | import botocore 9 | 10 | # Create an S3 Client 11 | s3_client = boto3.client('s3') 12 | 13 | def lambda_handler(event, context): 14 | source_bucket_name = event['Records'][0]['s3']['bucket']['name'] 15 | source_key = event['Records'][0]['s3']['object']['key'] 16 | # Retrieve environment variable TARGET_BUCKET 17 | destination_bucket_name = os.environ.get('TARGET_BUCKET', None) 18 | if destination_bucket_name is None: 19 | # Environment variable TARGET_BUCKET does not exist 20 | print('Error: TARGET_BUCKET Lambda environment variable does not exist!!') 21 | return 22 | destination_key = source_key 23 | print('From - bucket: ' + source_bucket_name) 24 | print('From - object: ' + source_key) 25 | print('To - bucket: ' + destination_bucket_name) 26 | print('To - object: ' + destination_key) 27 | 28 | # Copy the object 29 | try: 30 | print('Copying object ...') 31 | copy_source = { 32 | 'Bucket': source_bucket_name, 33 | 'Key': source_key 34 | } 35 | s3_client.copy(copy_source, destination_bucket_name, destination_key) 36 | print('Copied') 37 | 38 | except botocore.exceptions.ClientError as e: 39 | if e.response['Error']['Code'] == "AccessDenied": 40 | print("Error: Access denied!!") 41 | elif e.response['Error']['Code'] == "InvalidBucketName": 42 | print("Error: Invalid bucket name!!") 43 | elif e.response['Error']['Code'] == "NoSuchBucket": 44 | print("Error: No such bucket!!") 45 | else: 46 | raise 47 | -------------------------------------------------------------------------------- /awslambdas3copy-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function S3 Copy Python example 2 | 3 | This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services). 4 | 5 | It handles an AWS Lambda function that copies an object when it appears in a S3 bucket to another S3 bucket using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * The AWS Lambda execution environment include SDK for Python (Boto 3). 19 | 20 | ## Using the code 21 | 22 | * You can select the destination bucket name using an AWS Lambda environment variable: `TARGET_BUCKET` 23 | 24 | * Access the AWS console. 25 | 26 | * Create a S3 bucket for the source and another S3 bucket for the target. 27 | 28 | * Create an IAM Policy: ex. `Policy-my-buckets` 29 | 30 | Changing: 31 | 32 | * `sourcebucket` to the name of your source bucket. 33 | * `targetbucket` to the name of your target bucket. 34 | 35 | Content of the IAM policy: 36 | 37 | ```bash 38 | { 39 | "Version": "2012-10-17", 40 | "Statement": [ 41 | { 42 | "Effect": "Allow", 43 | "Action": [ 44 | "s3:GetObject" 45 | ], 46 | "Resource": [ 47 | "arn:aws:s3:::sourcebucket/*" 48 | ] 49 | }, 50 | { 51 | "Effect": "Allow", 52 | "Action": [ 53 | "s3:PutObject" 54 | ], 55 | "Resource": [ 56 | "arn:aws:s3:::targetbucket/*" 57 | ] 58 | }, 59 | { 60 | "Effect": "Allow", 61 | "Action": [ 62 | "cloudwatch:*" 63 | ], 64 | "Resource": [ 65 | "*" 66 | ] 67 | }, 68 | { 69 | "Effect": "Allow", 70 | "Action": [ 71 | "logs:*" 72 | ], 73 | "Resource": [ 74 | "*" 75 | ] 76 | } 77 | ] 78 | } 79 | ``` 80 | 81 | * Create a role: `Role-my-buckets`. 82 | 83 | This role uses the policy `Policy-my-buckets` 84 | 85 | * Create an AWS lambda function. 86 | * Name: `` 87 | * Runtime: `Python 3.8` 88 | * Handler: `lambda_function.lambda_handler` 89 | * Role: `Role-my-buckets` 90 | * Runtime Settings for the lambda function: 91 | * Memory (MB): `1024` 92 | * Timeout: `10 sec` 93 | * The resources that the function's role has access to: 94 | * `Amazon CloudWatch` 95 | * `Amazon CloudWatch Logs` 96 | * `Amazon S3` 97 | * Lambda obtained information from the policy statements: `Managed policy Policy-my-buckets`: 98 | * `s3:GetObject` --> `Allow: arn:aws:s3:::sourcebucket/*` 99 | * `s3:PutObject` --> `Allow: arn:aws:s3:::targetbucket/*` 100 | * The triggers: 101 | * `S3` 102 | * Bucket: `` 103 | * Event type: `ObjectCreated` 104 | * Enable trigger: `Yes` 105 | 106 | * Create the AWS Lambda environment variable `TARGET_BUCKET` and set its value to the name of your target bucket. 107 | 108 | * Write the code. 109 | 110 | The content of `lambda_function.py` file. 111 | 112 | * Save the Lambda function. 113 | 114 | It deploys the Lambda function. 115 | 116 | * Test the function: 117 | 118 | Copy a file in the source S3 bucket. 119 | 120 | The object from the source S3 bucket should be copied to the target S3 bucket. 121 | 122 | You should see the next messages in the log: 123 | 124 | ```bash 125 | "From - bucket:: " 126 | "From - object: " 127 | "To - bucket: " 128 | "To - object: " 129 | "Copying object ..." 130 | "Copied" 131 | ``` -------------------------------------------------------------------------------- /awslambdas3copy-rapi/lambda_function.py: -------------------------------------------------------------------------------- 1 | # lamdda_function.py 2 | # It handles an AWS Lambda function that copies an object when it appears 3 | # in a S3 bucket to another S3 bucket. 4 | # It uses Resource API (high-level) of Boto3. 5 | 6 | import os 7 | import boto3 8 | import botocore 9 | 10 | # Instantiate the service resource object 11 | s3_resource = boto3.resource('s3') 12 | 13 | def lambda_handler(event, context): 14 | source_bucket_name = event['Records'][0]['s3']['bucket']['name'] 15 | source_key = event['Records'][0]['s3']['object']['key'] 16 | # Retrieve environment variable TARGET_BUCKET 17 | destination_bucket_name = os.environ.get('TARGET_BUCKET', None) 18 | if destination_bucket_name is None: 19 | # Environment variable TARGET_BUCKET does not exist 20 | print('Error: TARGET_BUCKET Lambda environment variable does not exist!!') 21 | return 22 | destination_key = source_key 23 | print('From - bucket: ' + source_bucket_name) 24 | print('From - object: ' + source_key) 25 | print('To - bucket: ' + destination_bucket_name) 26 | print('To - object: ' + destination_key) 27 | 28 | # Copy the object 29 | try: 30 | print('Copying object ...') 31 | # Instantiate the destination bucket 32 | dest_bucket = s3_resource.Bucket(destination_bucket_name) 33 | copy_source = { 34 | 'Bucket': source_bucket_name, 35 | 'Key': source_key 36 | } 37 | dest_bucket.copy(copy_source, destination_key) 38 | print('Copied') 39 | 40 | except botocore.exceptions.ClientError as e: 41 | if e.response['Error']['Code'] == "AccessDenied": 42 | print("Error: Access denied!!") 43 | elif e.response['Error']['Code'] == "InvalidBucketName": 44 | print("Error: Invalid bucket name!!") 45 | elif e.response['Error']['Code'] == "NoSuchBucket": 46 | print("Error: No such bucket!!") 47 | else: 48 | raise 49 | -------------------------------------------------------------------------------- /awslambdas3event/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function S3 Event Python example 2 | 3 | This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services). 4 | 5 | It handles an AWS Lambda function that sends information to the log about an object when it appears in a S3 bucket. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for Python 3. 12 | 13 | ## Using the code 14 | 15 | * Access the AWS console. 16 | 17 | * Create a S3 bucket. 18 | 19 | * Create an AWS lambda function. 20 | * Name: `` 21 | * Runtime: `Python 3.8` 22 | * Handler: `lambda_function.lambda_handler` 23 | * Role: `lambda-basic-execution` 24 | * Runtime Settings for the lambda function: 25 | * Memory (MB): `128` 26 | * Timeout: `3 sec` 27 | * The resources that the function's role has access to: 28 | * `Amazon CloudWatch Logs` 29 | * The triggers: 30 | * `S3` 31 | * Bucket: `` 32 | * Event type: `ObjectCreated` 33 | * Enable trigger: `Yes` 34 | 35 | * Write the code. 36 | 37 | The content of `lambda_function.py` file. 38 | 39 | * Save the Lambda function. 40 | 41 | It deploys the Lambda function. 42 | 43 | * Test the function: 44 | 45 | Copy a file in the source S3 bucket. 46 | 47 | You should see the next message in the log: 48 | 49 | ```bash 50 | "Input: " 51 | "Bucket: " 52 | "Object: " 53 | ``` -------------------------------------------------------------------------------- /awslambdas3event/lambda_function.py: -------------------------------------------------------------------------------- 1 | # lamdda_function.py 2 | # It handles an AWS Lambda function that sends information to the log about 3 | # an object when it appears in a S3 bucket. 4 | 5 | def lambda_handler(event, context): 6 | bucket_name = event['Records'][0]['s3']['bucket']['name'] 7 | key_name = event['Records'][0]['s3']['object']['key'] 8 | 9 | print("Input:", event) 10 | print("Bucket:", bucket_name) 11 | print("Object:", key_name) 12 | -------------------------------------------------------------------------------- /awslambdas3move-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function S3 Move Python example 2 | 3 | This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services). 4 | 5 | It handles an AWS Lambda function that moves an object when it appears in a S3 bucket to another S3 bucket using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * The AWS Lambda execution environment include SDK for Python (Boto 3). 19 | 20 | ## Using the code 21 | 22 | * You can select the destination bucket name using an AWS Lambda environment variable: `TARGET_BUCKET` 23 | 24 | * Access the AWS console. 25 | 26 | * Create a S3 bucket for the source and another S3 bucket for the target. 27 | 28 | * Create an IAM Policy: ex. `Policy-my-buckets` 29 | 30 | Changing: 31 | 32 | * `sourcebucket` to the name of your source bucket. 33 | * `targetbucket` to the name of your target bucket. 34 | 35 | Content of the IAM policy: 36 | 37 | ```bash 38 | { 39 | "Version": "2012-10-17", 40 | "Statement": [ 41 | { 42 | "Effect": "Allow", 43 | "Action": [ 44 | "s3:GetObject" 45 | "s3:DeleteObject" 46 | ], 47 | "Resource": [ 48 | "arn:aws:s3:::sourcebucket/*" 49 | ] 50 | }, 51 | { 52 | "Effect": "Allow", 53 | "Action": [ 54 | "s3:PutObject" 55 | ], 56 | "Resource": [ 57 | "arn:aws:s3:::targetbucket/*" 58 | ] 59 | }, 60 | { 61 | "Effect": "Allow", 62 | "Action": [ 63 | "cloudwatch:*" 64 | ], 65 | "Resource": [ 66 | "*" 67 | ] 68 | }, 69 | { 70 | "Effect": "Allow", 71 | "Action": [ 72 | "logs:*" 73 | ], 74 | "Resource": [ 75 | "*" 76 | ] 77 | } 78 | ] 79 | } 80 | ``` 81 | 82 | * Create a role: `Role-my-buckets`. 83 | 84 | This role uses the policy `Policy-my-buckets` 85 | 86 | * Create an AWS lambda function. 87 | * Name: `` 88 | * Runtime: `Python 3.8` 89 | * Handler: `lambda_function.lambda_handler` 90 | * Role: `Role-my-buckets` 91 | * Runtime Settings for the lambda function: 92 | * Memory (MB): `1024` 93 | * Timeout: `10 sec` 94 | * The resources that the function's role has access to: 95 | * `Amazon CloudWatch` 96 | * `Amazon CloudWatch Logs` 97 | * `Amazon S3` 98 | * Lambda obtained information from the policy statements: `Managed policy Policy-my-buckets`: 99 | * `s3:GetObject` --> `Allow: arn:aws:s3:::sourcebucket/*` 100 | * `s3:DeleteObject` --> `Allow: arn:aws:s3:::sourcebucket/*` 101 | * `s3:PutObject` --> `Allow: arn:aws:s3:::targetbucket/*` 102 | * The triggers: 103 | * `S3` 104 | * Bucket: `` 105 | * Event type: `ObjectCreated` 106 | * Enable trigger: `Yes` 107 | 108 | * Create the AWS Lambda environment variable `TARGET_BUCKET` and set its value to the name of your target bucket. 109 | 110 | * Write the code. 111 | 112 | The content of `lambda_function.py` file. 113 | 114 | * Save the Lambda function. 115 | 116 | It deploys the Lambda function. 117 | 118 | * Test the function: 119 | 120 | Copy a file in the source S3 bucket. 121 | 122 | The object from the source S3 bucket should be copied to the target S3 bucket and deleted in the source S3 bucket. 123 | 124 | You should see the next messages in the log: 125 | 126 | ```bash 127 | "From - bucket:: " 128 | "From - object: " 129 | "To - bucket: " 130 | "To - object: " 131 | "Moving object ..." 132 | "Moved" 133 | ``` -------------------------------------------------------------------------------- /awslambdas3move-capi/lambda_function.py: -------------------------------------------------------------------------------- 1 | # lamdda_function.py 2 | # It handles an AWS Lambda function that moves an object when it appears 3 | # in a S3 bucket to another S3 bucket. 4 | # It uses Client API (low-level) of Boto3. 5 | 6 | import os 7 | import boto3 8 | import botocore 9 | 10 | # Create as S3 Client 11 | s3_client = boto3.client('s3') 12 | 13 | def lambda_handler(event, context): 14 | source_bucket_name = event['Records'][0]['s3']['bucket']['name'] 15 | source_key = event['Records'][0]['s3']['object']['key'] 16 | # Retrieve environment variable TARGET_BUCKET 17 | destination_bucket_name = os.environ.get('TARGET_BUCKET', None) 18 | if destination_bucket_name is None: 19 | # Environment variable TARGET_BUCKET does not exist 20 | print('Error: TARGET_BUCKET Lambda environment variable does not exist!!') 21 | return 22 | destination_key = source_key 23 | print('From - bucket: ' + source_bucket_name) 24 | print('From - object: ' + source_key) 25 | print('To - bucket: ' + destination_bucket_name) 26 | print('To - object: ' + destination_key) 27 | 28 | try: 29 | # Copy the object 30 | print('Moving object ...') 31 | copy_source = { 32 | 'Bucket': source_bucket_name, 33 | 'Key': source_key 34 | } 35 | s3_client.copy(copy_source, destination_bucket_name, destination_key) 36 | # Delete the object from source bucket 37 | response = s3_client.delete_object(Bucket=source_bucket_name, Key=source_key) 38 | print(response) 39 | print('\nMoved') 40 | 41 | except botocore.exceptions.ClientError as e: 42 | if e.response['Error']['Code'] == "AccessDenied": 43 | print("Error: Access denied!!") 44 | elif e.response['Error']['Code'] == "InvalidBucketName": 45 | print("Error: Invalid bucket name!!") 46 | elif e.response['Error']['Code'] == "NoSuchBucket": 47 | print("Error: No such bucket!!") 48 | else: 49 | raise 50 | -------------------------------------------------------------------------------- /awslambdas3move-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function S3 Move Python example 2 | 3 | This folder contains an AWS Lambda Function example in Python on AWS (Amazon Web Services). 4 | 5 | It handles an AWS Lambda function that moves an object when it appears in a S3 bucket to another S3 bucket using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * The AWS Lambda execution environment include SDK for Python (Boto 3). 19 | 20 | ## Using the code 21 | 22 | * You can select the destination bucket name using an AWS Lambda environment variable: `TARGET_BUCKET` 23 | 24 | * Access the AWS console. 25 | 26 | * Create a S3 bucket for the source and another S3 bucket for the target. 27 | 28 | * Create an IAM Policy: ex. `Policy-my-buckets` 29 | 30 | Changing: 31 | 32 | * `sourcebucket` to the name of your source bucket. 33 | * `targetbucket` to the name of your target bucket. 34 | 35 | Content of the IAM policy: 36 | 37 | ```bash 38 | { 39 | "Version": "2012-10-17", 40 | "Statement": [ 41 | { 42 | "Effect": "Allow", 43 | "Action": [ 44 | "s3:GetObject" 45 | "s3:DeleteObject" 46 | ], 47 | "Resource": [ 48 | "arn:aws:s3:::sourcebucket/*" 49 | ] 50 | }, 51 | { 52 | "Effect": "Allow", 53 | "Action": [ 54 | "s3:PutObject" 55 | ], 56 | "Resource": [ 57 | "arn:aws:s3:::targetbucket/*" 58 | ] 59 | }, 60 | { 61 | "Effect": "Allow", 62 | "Action": [ 63 | "cloudwatch:*" 64 | ], 65 | "Resource": [ 66 | "*" 67 | ] 68 | }, 69 | { 70 | "Effect": "Allow", 71 | "Action": [ 72 | "logs:*" 73 | ], 74 | "Resource": [ 75 | "*" 76 | ] 77 | } 78 | ] 79 | } 80 | ``` 81 | 82 | * Create a role: `Role-my-buckets`. 83 | 84 | This role uses the policy `Policy-my-buckets` 85 | 86 | * Create an AWS lambda function. 87 | * Name: `` 88 | * Runtime: `Python 3.8` 89 | * Handler: `lambda_function.lambda_handler` 90 | * Role: `Role-my-buckets` 91 | * Runtime Settings for the lambda function: 92 | * Memory (MB): `1024` 93 | * Timeout: `10 sec` 94 | * The resources that the function's role has access to: 95 | * `Amazon CloudWatch` 96 | * `Amazon CloudWatch Logs` 97 | * `Amazon S3` 98 | * Lambda obtained information from the policy statements: `Managed policy Policy-my-buckets`: 99 | * `s3:GetObject` --> `Allow: arn:aws:s3:::sourcebucket/*` 100 | * `s3:DeleteObject` --> `Allow: arn:aws:s3:::sourcebucket/*` 101 | * `s3:PutObject` --> `Allow: arn:aws:s3:::targetbucket/*` 102 | * The triggers: 103 | * `S3` 104 | * Bucket: `` 105 | * Event type: `ObjectCreated` 106 | * Enable trigger: `Yes` 107 | 108 | * Create the AWS Lambda environment variable `TARGET_BUCKET` and set its value to the name of your target bucket. 109 | 110 | * Write the code. 111 | 112 | The content of `lambda_function.py` file. 113 | 114 | * Save the Lambda function. 115 | 116 | It deploys the Lambda function. 117 | 118 | * Test the function: 119 | 120 | Copy a file in the source S3 bucket. 121 | 122 | The object from the source S3 bucket should be copied to the target S3 bucket and deleted in the source S3 bucket. 123 | 124 | You should see the next messages in the log: 125 | 126 | ```bash 127 | "From - bucket:: " 128 | "From - object: " 129 | "To - bucket: " 130 | "To - object: " 131 | "Moving object ..." 132 | "Moved" 133 | ``` -------------------------------------------------------------------------------- /awslambdas3move-rapi/lambda_function.py: -------------------------------------------------------------------------------- 1 | # lamdda_function.py 2 | # It handles an AWS Lambda function that moves an object when it appears 3 | # in a S3 bucket to another S3 bucket. 4 | # It uses Resource API (high-level) of Boto3. 5 | 6 | import os 7 | import boto3 8 | import botocore 9 | 10 | # Instantiate the service resource object 11 | s3_resource = boto3.resource('s3') 12 | 13 | def lambda_handler(event, context): 14 | source_bucket_name = event['Records'][0]['s3']['bucket']['name'] 15 | source_key = event['Records'][0]['s3']['object']['key'] 16 | # Retrieve environment variable TARGET_BUCKET 17 | destination_bucket_name = os.environ.get('TARGET_BUCKET', None) 18 | if destination_bucket_name is None: 19 | # Environment variable TARGET_BUCKET does not exist 20 | print('Error: TARGET_BUCKET Lambda environment variable does not exist!!') 21 | return 22 | destination_key = source_key 23 | print('From - bucket: ' + source_bucket_name) 24 | print('From - object: ' + source_key) 25 | print('To - bucket: ' + destination_bucket_name) 26 | print('To - object: ' + destination_key) 27 | 28 | try: 29 | print('Moving object ...') 30 | # Instantiate the destination bucket 31 | dest_bucket = s3_resource.Bucket(destination_bucket_name) 32 | # Copy the object 33 | copy_source = { 34 | 'Bucket': source_bucket_name, 35 | 'Key': source_key 36 | } 37 | dest_bucket.copy(copy_source, destination_key) 38 | # Instantiate the source bucket 39 | source_bucket = s3_resource.Bucket(source_bucket_name) 40 | # Delete the object from source bucket 41 | source_obj = source_bucket.Object(source_key) 42 | response = source_obj.delete() 43 | print(response) 44 | print('\nMoved') 45 | 46 | except botocore.exceptions.ClientError as e: 47 | if e.response['Error']['Code'] == "AccessDenied": 48 | print("Error: Access denied!!") 49 | elif e.response['Error']['Code'] == "InvalidBucketName": 50 | print("Error: Invalid bucket name!!") 51 | elif e.response['Error']['Code'] == "NoSuchBucket": 52 | print("Error: No such bucket!!") 53 | else: 54 | raise 55 | -------------------------------------------------------------------------------- /awslambdaupdate-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Function Update Python example 2 | 3 | This folder contains a Python application example that handles Lambda functions on AWS (Amazon Web Services). 4 | 5 | Update an AWS Lambda function using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * The AWS Lambda function to update must already exist. 74 | 75 | You can use the AWS Lambda Function Hello World JSON Java example: [awslambdahellojson](/awslambdahellojson). 76 | 77 | * You need a `.py` file where the code of the Lambda function is located. 78 | 79 | You can use the code obtained from the AWS Lambda Function Hello World JSON Java example: [awslambdahellojson](/awslambdahellojson). 80 | 81 | This application compresses the `.py` file in a `ZIP` file to deploy. 82 | 83 | * You can select the AWS region of the Lambda function changing the value of `REGION` variable in the code. 84 | 85 | * You have to create an AWS role that has Lambda permissions. 86 | 87 | * Run the code. 88 | 89 | You must provide 4 parameters, replace the values of: 90 | 91 | * `` by the Lambda function name. 92 | * `` by the path to the `.py` file where the code of the Lambda function is located. 93 | * `` by the role ARN that has Lambda permissions. 94 | * `` by the fully qualifed method name (Ex: lambda_function.lambda_handler). 95 | 96 | Run application: 97 | 98 | ```bash 99 | python lambdaupdate.py 100 | ``` 101 | 102 | You must use as a function role the ARN. 103 | 104 | Ex.: `arn:aws:iam::123456789012:role/service-role/lambda-basic-execution` 105 | 106 | * Test the application. 107 | 108 | You should see the response of the Lambda function. 109 | 110 | For example: 111 | 112 | * `Updating function ...` 113 | * `Updated function "FunctionName"` 114 | 115 | And the Lambda function updated. 116 | -------------------------------------------------------------------------------- /awslambdaupdate-capi/lambdaupdate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # lambdaupdate.py 4 | # It is an example that handles Lambda functions on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Update a Lambda function. 7 | # You must provide 1 parameter: 8 | # FUNCTION_NAME = Lambda function name 9 | # FUNCTION_FILE = The path to the .py file where the code of the Lambda function is located 10 | # FUNCTION_ROLE = The role ARN that has Lambda permissions 11 | # FUNCTION_HANDLER = The fully qualifed method name (Ex: lambda_function.lambda_handler) 12 | 13 | import sys 14 | import io 15 | import zipfile 16 | import boto3 17 | import botocore 18 | 19 | def create_lambda_deployment_package(function_file_name): 20 | # Creates a Lambda deployment package in ZIP format in an in-memory buffer. 21 | # This buffer can be passed directly to AWS Lambda when creating the function. 22 | # Parameters: 23 | # function_file_name (string): The name of the file that contains the Lambda handler function. 24 | # Return: 25 | # The deployment package. 26 | 27 | buffer = io.BytesIO() 28 | with zipfile.ZipFile(buffer, 'w') as zipped: 29 | zipped.write(function_file_name) 30 | buffer.seek(0) 31 | return buffer.read() 32 | 33 | 34 | def main(): 35 | 36 | REGION = 'eu-west-1' # AWS region 37 | 38 | # Make a list of command line arguments, omitting the [0] element 39 | # which is the script itself. 40 | args = sys.argv[1:] 41 | if len(args) < 4: 42 | print('Not enough parameters.\n'\ 43 | 'Proper Usage is: python lambdaupdate.py '\ 44 | ' ') 45 | sys.exit(1) 46 | 47 | function_name = args[0] 48 | function_file = args[1] 49 | function_role = args[2] 50 | function_handler = args[3] 51 | 52 | print('Lambda function name: ' + function_name) 53 | print('Lambda function file: ' + function_file) 54 | print('Lambda function role: ' + function_role) 55 | print('Lambda function handler: ' + function_handler) 56 | 57 | # Create a Lambda Client 58 | lambda_client = boto3.client('lambda', region_name=REGION) 59 | 60 | # Create Lambda function 61 | try: 62 | print('Updating function ...') 63 | 64 | deployment_package = create_lambda_deployment_package(function_file) 65 | 66 | # Update Lambda function configuration 67 | response = lambda_client.update_function_configuration( 68 | FunctionName=function_name, 69 | Description='Updated by the Lambda Python API', 70 | Runtime='python3.8', 71 | Role=function_role, 72 | Handler=function_handler) 73 | 74 | print('Response (Conf.):') 75 | print(response) 76 | function_arn_conf = response['FunctionArn'] 77 | 78 | # Update Lambda function code 79 | response = lambda_client.update_function_code( 80 | FunctionName=function_name, 81 | ZipFile=deployment_package, 82 | Publish=True) 83 | 84 | print('Response (Code):') 85 | print(response) 86 | function_arn_code = response['FunctionArn'] 87 | 88 | print('\nUpdated function "' + function_name + '"') 89 | print('The function ARN (Conf.) is ' + function_arn_conf) 90 | print('The function ARN (Code) is ' + function_arn_code) 91 | 92 | except botocore.exceptions.ClientError as e: 93 | if e.response['Error']['Code'] == "AccessDeniedException": 94 | print("Error: Access Denied!!") 95 | elif e.response['Error']['Code'] == "ValidationException": 96 | print("Error: Name Not Valid!!") 97 | else: 98 | raise 99 | 100 | return 101 | 102 | 103 | # This is the standard boilerplate that calls the main() function. 104 | if __name__ == '__main__': 105 | main() 106 | -------------------------------------------------------------------------------- /awss3copy-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Copy Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Copy an object from a S3 bucket to another S3 bucket using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Create a S3 bucket for the source and another S3 bucket for the target. 74 | 75 | * Copy a file to the source S3 bucket. 76 | 77 | * Run the code. 78 | 79 | You must provide 3 parameters, replace the values of: 80 | 81 | * `` by source bucket name. 82 | * `` by source file name. 83 | * `` by destination bucket name. 84 | 85 | Run application: 86 | 87 | ```bash 88 | python s3copy.py 89 | ``` 90 | 91 | * Test the application. 92 | 93 | The object from the source S3 bucket should be copied to the target S3 bucket. 94 | -------------------------------------------------------------------------------- /awss3copy-capi/s3copy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3copy.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Copy an object from a S3 bucket to another S3 bucket. 7 | # You must provide 3 parameters: 8 | # SOURCE_BUCKET = Source bucket name 9 | # SOURCE_OBJECT = Source file name 10 | # DESTINATION_BUCKET = Destination bucket name 11 | 12 | import sys 13 | import boto3 14 | import botocore 15 | 16 | def main(): 17 | 18 | # Make a list of command line arguments, omitting the [0] element 19 | # which is the script itself. 20 | args = sys.argv[1:] 21 | if len(args) < 3: 22 | print('Not enough parameters.\n'\ 23 | 'Proper Usage is: python s3copy.py '\ 24 | ' ') 25 | sys.exit(1) 26 | 27 | source_bucket_name = args[0] 28 | source_key = args[1] 29 | destination_bucket_name = args[2] 30 | destination_key = source_key 31 | print('From - bucket: ' + source_bucket_name) 32 | print('From - object: ' + source_key) 33 | print('To - bucket: ' + destination_bucket_name) 34 | print('To - object: ' + destination_key) 35 | 36 | # Create an S3 Client 37 | s3_client = boto3.client('s3') 38 | 39 | # Copy the object 40 | try: 41 | print('Copying object ...') 42 | copy_source = { 43 | 'Bucket': source_bucket_name, 44 | 'Key': source_key 45 | } 46 | s3_client.copy(copy_source, destination_bucket_name, destination_key) 47 | print('Copied') 48 | 49 | except botocore.exceptions.ClientError as e: 50 | if e.response['Error']['Code'] == "404": 51 | print("Error: Not Found, problem with the parameters!!") 52 | elif e.response['Error']['Code'] == "400": 53 | print("Error: Bad request, problem with the bucket!!") 54 | elif e.response['Error']['Code'] == "403": 55 | print("Error: Forbidden, bucket forbidden!!") 56 | elif e.response['Error']['Code'] == "AccessDenied": 57 | print("Error: Access denied!!") 58 | elif e.response['Error']['Code'] == "InvalidBucketName": 59 | print("Error: Invalid bucket name!!") 60 | elif e.response['Error']['Code'] == "NoSuchBucket": 61 | print("Error: No such bucket!!") 62 | else: 63 | raise 64 | 65 | return 66 | 67 | 68 | # This is the standard boilerplate that calls the main() function. 69 | if __name__ == '__main__': 70 | main() 71 | -------------------------------------------------------------------------------- /awss3copy-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Copy Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Copy an object from a S3 bucket to another S3 bucket using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Create a S3 bucket for the source and another S3 bucket for the target. 74 | 75 | * Copy a file to the source S3 bucket. 76 | 77 | * Run the code. 78 | 79 | You must provide 3 parameters, replace the values of: 80 | 81 | * `` by source bucket name. 82 | * `` by source file name. 83 | * `` by destination bucket name. 84 | 85 | Run application: 86 | 87 | ```bash 88 | python s3copy.py 89 | ``` 90 | 91 | * Test the application. 92 | 93 | The object from the source S3 bucket should be copied to the target S3 bucket. 94 | -------------------------------------------------------------------------------- /awss3copy-rapi/s3copy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3copy.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Resource API (high-level) of Boto3. 6 | # Copy an object from a S3 bucket to another S3 bucket. 7 | # You must provide 3 parameters: 8 | # SOURCE_BUCKET = Source bucket name 9 | # SOURCE_OBJECT = Source file name 10 | # DESTINATION_BUCKET = Destination bucket name 11 | 12 | import sys 13 | import boto3 14 | import botocore 15 | 16 | def main(): 17 | 18 | # Make a list of command line arguments, omitting the [0] element 19 | # which is the script itself. 20 | args = sys.argv[1:] 21 | if len(args) < 3: 22 | print('Not enough parameters.\n'\ 23 | 'Proper Usage is: python s3copy.py '\ 24 | ' ') 25 | sys.exit(1) 26 | 27 | source_bucket_name = args[0] 28 | source_key = args[1] 29 | destination_bucket_name = args[2] 30 | destination_key = source_key 31 | print('From - bucket: ' + source_bucket_name) 32 | print('From - object: ' + source_key) 33 | print('To - bucket: ' + destination_bucket_name) 34 | print('To - object: ' + destination_key) 35 | 36 | # Instantiate the service resource object 37 | s3_resource = boto3.resource('s3') 38 | 39 | # Copy the object 40 | try: 41 | print('Copying object ...') 42 | # Instantiate the destination bucket 43 | dest_bucket = s3_resource.Bucket(destination_bucket_name) 44 | copy_source = { 45 | 'Bucket': source_bucket_name, 46 | 'Key': source_key 47 | } 48 | dest_bucket.copy(copy_source, destination_key) 49 | print('Copied') 50 | 51 | except botocore.exceptions.ClientError as e: 52 | if e.response['Error']['Code'] == "404": 53 | print("Error: Not Found, problem with the parameters!!") 54 | elif e.response['Error']['Code'] == "400": 55 | print("Error: Bad request, problem with the bucket!!") 56 | elif e.response['Error']['Code'] == "403": 57 | print("Error: Forbidden, bucket forbidden!!") 58 | elif e.response['Error']['Code'] == "AccessDenied": 59 | print("Error: Access denied!!") 60 | elif e.response['Error']['Code'] == "InvalidBucketName": 61 | print("Error: Invalid bucket name!!") 62 | elif e.response['Error']['Code'] == "NoSuchBucket": 63 | print("Error: No such bucket!!") 64 | else: 65 | raise 66 | 67 | return 68 | 69 | 70 | # This is the standard boilerplate that calls the main() function. 71 | if __name__ == '__main__': 72 | main() 73 | -------------------------------------------------------------------------------- /awss3create-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Create Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Create a new S3 bucket using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * You can select the bucket region changing the value of `REGION` variable in the code. 74 | 75 | * Run the code. 76 | 77 | You must provide 1 parameter, replace the value of: 78 | 79 | * `` by bucket name. 80 | 81 | Run application: 82 | 83 | ```bash 84 | python s3create.py 85 | ``` 86 | 87 | * Test the application. 88 | 89 | You should see the new S3 bucket created. 90 | -------------------------------------------------------------------------------- /awss3create-capi/s3create.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3create.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Create a new S3 bucket. 7 | # You must provide 1 parameter: 8 | # BUCKET_NAME = Name of the bucket 9 | 10 | import sys 11 | import boto3 12 | import botocore 13 | 14 | def main(): 15 | 16 | REGION = 'eu-west-1' # AWS region 17 | 18 | # Make a list of command line arguments, omitting the [0] element 19 | # which is the script itself. 20 | args = sys.argv[1:] 21 | if len(args) < 1: 22 | print('Not enough parameters.\n'\ 23 | 'Proper Usage is: python s3create.py ') 24 | sys.exit(1) 25 | 26 | bucket_name = args[0] 27 | print('Bucket name: ' + bucket_name) 28 | 29 | # Create an S3 Client 30 | s3_client = boto3.client('s3') 31 | 32 | # Create the bucket object 33 | try: 34 | print('Creating bucket ...') 35 | response = s3_client.create_bucket(ACL='private', 36 | Bucket=bucket_name, 37 | CreateBucketConfiguration={ 38 | 'LocationConstraint': REGION}) 39 | print(response) 40 | print('\nCreated') 41 | print(response['Location']) 42 | 43 | except botocore.exceptions.ClientError as e: 44 | if e.response['Error']['Code'] == "BucketAlreadyOwnedByYou": 45 | print("Error: Bucket already created and owned by you!!") 46 | elif e.response['Error']['Code'] == "BucketAlreadyExists": 47 | print("Error: Bucket already exist!!") 48 | elif e.response['Error']['Code'] == "InvalidBucketName": 49 | print("Error: Invalid Bucket name!!") 50 | else: 51 | raise 52 | 53 | return 54 | 55 | 56 | # This is the standard boilerplate that calls the main() function. 57 | if __name__ == '__main__': 58 | main() 59 | -------------------------------------------------------------------------------- /awss3create-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Create Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Create a new S3 bucket using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * You can select the bucket region changing the value of `REGION` variable in the code. 74 | 75 | * Run the code. 76 | 77 | You must provide 1 parameter, replace the value of: 78 | 79 | * `` by bucket name. 80 | 81 | Run application: 82 | 83 | ```bash 84 | python s3create.py 85 | ``` 86 | 87 | * Test the application. 88 | 89 | You should see the new S3 bucket created. 90 | -------------------------------------------------------------------------------- /awss3create-rapi/s3create.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3create.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Resource API (high-level) of Boto3. 6 | # Create a new S3 bucket. 7 | # You must provide 1 parameter: 8 | # BUCKET_NAME = Name of the bucket 9 | 10 | import sys 11 | import boto3 12 | import botocore 13 | 14 | def main(): 15 | 16 | REGION = 'eu-west-1' 17 | 18 | # Make a list of command line arguments, omitting the [0] element 19 | # which is the script itself. 20 | args = sys.argv[1:] 21 | if len(args) < 1: 22 | print('Not enough parameters.\n'\ 23 | 'Proper Usage is: python s3create.py ') 24 | sys.exit(1) 25 | 26 | bucket_name = args[0] 27 | print('Bucket name: ' + bucket_name) 28 | 29 | # Instantiate the service resource object 30 | s3_resource = boto3.resource('s3') 31 | 32 | # Instantiate the bucket object 33 | bucket = s3_resource.Bucket(bucket_name) 34 | 35 | # Create the bucket object 36 | try: 37 | print('Creating bucket ...') 38 | response = bucket.create(ACL='private', 39 | CreateBucketConfiguration={ 40 | 'LocationConstraint': REGION}) 41 | print(response) 42 | print('\nCreated') 43 | print(response['Location']) 44 | 45 | except botocore.exceptions.ClientError as e: 46 | if e.response['Error']['Code'] == "BucketAlreadyOwnedByYou": 47 | print("Error: Bucket already created and owned by you!!") 48 | elif e.response['Error']['Code'] == "BucketAlreadyExists": 49 | print("Error: Bucket already exist!!") 50 | elif e.response['Error']['Code'] == "InvalidBucketName": 51 | print("Error: Invalid Bucket name!!") 52 | else: 53 | raise 54 | 55 | return 56 | 57 | 58 | # This is the standard boilerplate that calls the main() function. 59 | if __name__ == '__main__': 60 | main() 61 | -------------------------------------------------------------------------------- /awss3delete-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Delete Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Delete a S3 bucket using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Run the code. 74 | 75 | You must provide 1 parameter, replace the value of: 76 | 77 | * `` by bucket name. 78 | 79 | Run application: 80 | 81 | ```bash 82 | python s3delete.py 83 | ``` 84 | 85 | * Test the application. 86 | 87 | You should not see the S3 bucket deleted. 88 | -------------------------------------------------------------------------------- /awss3delete-capi/s3delete.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3delete.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Delete a S3 bucket. 7 | # You must provide 1 parameter: 8 | # BUCKET_NAME = Name of the bucket 9 | 10 | import sys 11 | import boto3 12 | import botocore 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 s3delete.py ') 22 | sys.exit(1) 23 | 24 | bucket_name = args[0] 25 | print('Bucket name: ' + bucket_name) 26 | 27 | # Create an S3 Client 28 | s3_client = boto3.client('s3') 29 | 30 | # Delete the bucket object 31 | try: 32 | print('Deleting bucket ...') 33 | response = s3_client.delete_bucket(Bucket=bucket_name) 34 | print(response) 35 | print('\nDeleted') 36 | 37 | except botocore.exceptions.ClientError as e: 38 | if e.response['Error']['Code'] == "NoSuchBucket": 39 | print("Error: Bucket does not exist!!") 40 | elif e.response['Error']['Code'] == "InvalidBucketName": 41 | print("Error: Invalid Bucket name!!") 42 | elif e.response['Error']['Code'] == "AllAccessDisabled": 43 | print("Error: You do not have access to the Bucket!!") 44 | else: 45 | raise 46 | 47 | return 48 | 49 | 50 | # This is the standard boilerplate that calls the main() function. 51 | if __name__ == '__main__': 52 | main() 53 | -------------------------------------------------------------------------------- /awss3delete-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Delete Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Delete a S3 bucket using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Run the code. 74 | 75 | You must provide 1 parameter, replace the value of: 76 | 77 | * `` by bucket name. 78 | 79 | Run application: 80 | 81 | ```bash 82 | python s3delete.py 83 | ``` 84 | 85 | * Test the application. 86 | 87 | You should not see the S3 bucket deleted. 88 | -------------------------------------------------------------------------------- /awss3delete-rapi/s3delete.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3delete.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Resource API (high-level) of Boto3. 6 | # Delete a S3 bucket. 7 | # You must provide 1 parameter: 8 | # BUCKET_NAME = Name of the bucket 9 | 10 | import sys 11 | import boto3 12 | import botocore 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 s3delete.py ') 22 | sys.exit(1) 23 | 24 | bucket_name = args[0] 25 | print('Bucket name: ' + bucket_name) 26 | 27 | # Instantiate the service resource object 28 | s3_resource = boto3.resource('s3') 29 | 30 | # Instantiate the bucket object 31 | bucket = s3_resource.Bucket(bucket_name) 32 | 33 | # Delete the bucket object 34 | try: 35 | print('Deleting bucket ...') 36 | response = bucket.delete() 37 | print(response) 38 | print('\nDeleted') 39 | 40 | except botocore.exceptions.ClientError as e: 41 | if e.response['Error']['Code'] == "NoSuchBucket": 42 | print("Error: Bucket does not exist!!") 43 | elif e.response['Error']['Code'] == "InvalidBucketName": 44 | print("Error: Invalid Bucket name!!") 45 | elif e.response['Error']['Code'] == "AllAccessDisabled": 46 | print("Error: You do not have access to the Bucket!!") 47 | else: 48 | raise 49 | 50 | return 51 | 52 | 53 | # This is the standard boilerplate that calls the main() function. 54 | if __name__ == '__main__': 55 | main() 56 | -------------------------------------------------------------------------------- /awss3deleteobject-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Delete Object Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Delete an object in a S3 bucket using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | O 73 | * Run the code. 74 | 75 | You must provide 2 parameters, replace the values of: 76 | 77 | * `` by Bucket name. 78 | * `` by Object name. 79 | 80 | Run application: 81 | 82 | ```bash 83 | python s3deleteobject.py 84 | ``` 85 | 86 | * Test the application. 87 | 88 | You should not see the object deleted in the S3 bucket. 89 | -------------------------------------------------------------------------------- /awss3deleteobject-capi/s3deleteobject.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3deleteobject.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Delete an object in a S3 bucket. 7 | # You must provide 2 parameters: 8 | # BUCKET_NAME = Name of the bucket 9 | # OBJECT_NAME = Name of the object in the bucket 10 | 11 | import sys 12 | import boto3 13 | import botocore 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 s3deleteobject.py '\ 23 | ' ') 24 | sys.exit(1) 25 | 26 | bucket_name = args[0] 27 | key_name = args[1] 28 | print('Bucket name: ' + bucket_name) 29 | print('Object name: ' + key_name) 30 | 31 | # Create an S3 Client 32 | s3_client = boto3.client('s3') 33 | 34 | # Delete the bucket object 35 | try: 36 | print('Deleting object ...') 37 | # Delete the object from bucket 38 | response = s3_client.delete_object(Bucket=bucket_name, Key=key_name) 39 | print(response) 40 | print('\nDeleted') 41 | 42 | except botocore.exceptions.ClientError as e: 43 | if e.response['Error']['Code'] == "AccessDenied": 44 | print("Error: Access denied!!") 45 | elif e.response['Error']['Code'] == "InvalidBucketName": 46 | print("Error: Invalid Bucket name!!") 47 | elif e.response['Error']['Code'] == "NoSuchBucket": 48 | print("Error: Bucket does not exist!!") 49 | elif e.response['Error']['Code'] == "AllAccessDisabled": 50 | print("Error: You do not have access to the Bucket!!") 51 | else: 52 | raise 53 | 54 | return 55 | 56 | 57 | # This is the standard boilerplate that calls the main() function. 58 | if __name__ == '__main__': 59 | main() 60 | -------------------------------------------------------------------------------- /awss3deleteobject-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Delete Object Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Delete an object in a S3 bucket using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Run the code. 74 | 75 | You must provide 2 parameters, replace the values of: 76 | 77 | * `` by Bucket name. 78 | * `` by Object name. 79 | 80 | Run application: 81 | 82 | ```bash 83 | python s3deleteobject.py 84 | ``` 85 | 86 | * Test the application. 87 | 88 | You should not see the object deleted in the S3 bucket. 89 | -------------------------------------------------------------------------------- /awss3deleteobject-rapi/s3deleteobject.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3deleteobject.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Resource API (high-level) of Boto3. 6 | # Delete an object in a S3 bucket. 7 | # You must provide 2 parameters: 8 | # BUCKET_NAME = Name of the bucket 9 | # OBJECT_NAME = Name of the object in the bucket 10 | 11 | import sys 12 | import boto3 13 | import botocore 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 s3deleteobject.py '\ 23 | ' ') 24 | sys.exit(1) 25 | 26 | bucket_name = args[0] 27 | key_name = args[1] 28 | print('Bucket name: ' + bucket_name) 29 | print('Object name: ' + key_name) 30 | 31 | # Instantiate the service resource object 32 | s3_resource = boto3.resource('s3') 33 | 34 | # Instantiate the bucket object 35 | bucket = s3_resource.Bucket(bucket_name) 36 | 37 | # Delete the bucket object 38 | try: 39 | print('Deleting object ...') 40 | obj = bucket.Object(key_name) 41 | response = obj.delete() 42 | print(response) 43 | print('\nDeleted') 44 | 45 | except botocore.exceptions.ClientError as e: 46 | if e.response['Error']['Code'] == "AccessDenied": 47 | print("Error: Access denied!!") 48 | elif e.response['Error']['Code'] == "InvalidBucketName": 49 | print("Error: Invalid Bucket name!!") 50 | elif e.response['Error']['Code'] == "NoSuchBucket": 51 | print("Error: Bucket does not exist!!") 52 | elif e.response['Error']['Code'] == "AllAccessDisabled": 53 | print("Error: You do not have access to the Bucket!!") 54 | else: 55 | raise 56 | 57 | return 58 | 59 | 60 | # This is the standard boilerplate that calls the main() function. 61 | if __name__ == '__main__': 62 | main() 63 | -------------------------------------------------------------------------------- /awss3download-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Download Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Download an object from a S3 bucket to a local file using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Run the code. 74 | 75 | You must provide 3 parameters, replace the values of: 76 | 77 | * `` by Bucket name. 78 | * `` by Object file name in the bucket. 79 | * `` by local file name. 80 | 81 | Run application: 82 | 83 | ```bash 84 | python s3download.py 85 | ``` 86 | 87 | * Test the application. 88 | 89 | You should see the new file created in your local destiny from the S3 bucket. 90 | -------------------------------------------------------------------------------- /awss3download-capi/s3download.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3download.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Download an object from a S3 bucket to a local file. 7 | # You must provide 3 parameters: 8 | # BUCKET_NAME = Name of the bucket 9 | # OBJECT_NAME = Object file name in the bucket 10 | # LOCAL_FILE_NAME = Local file name 11 | 12 | import sys 13 | import boto3 14 | import botocore 15 | 16 | def main(): 17 | 18 | # Make a list of command line arguments, omitting the [0] element 19 | # which is the script itself. 20 | args = sys.argv[1:] 21 | if len(args) < 3: 22 | print('Not enough parameters.\n'\ 23 | 'Proper Usage is: python s3download.py '\ 24 | ' ') 25 | sys.exit(1) 26 | 27 | bucket_name = args[0] 28 | key_name = args[1] 29 | local_file_name = args[2] 30 | 31 | print('Bucket: ' + bucket_name) 32 | print('Object/Key: ' + key_name) 33 | print('Local file: ' + local_file_name) 34 | 35 | # Create an S3 Client 36 | s3_client = boto3.client('s3') 37 | 38 | # Download object 39 | try: 40 | print('Downloading object ...') 41 | s3_client.download_file(bucket_name, key_name, local_file_name) 42 | print('Downloaded') 43 | 44 | except botocore.exceptions.ClientError as e: 45 | if e.response['Error']['Code'] == "404": 46 | print("Error: Not Found, problem with the parameters!!") 47 | elif e.response['Error']['Code'] == "400": 48 | print("Error: Bad request, problem with the bucket!!") 49 | else: 50 | raise 51 | 52 | return 53 | 54 | 55 | # This is the standard boilerplate that calls the main() function. 56 | if __name__ == '__main__': 57 | main() 58 | -------------------------------------------------------------------------------- /awss3download-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Download Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Download an object from a S3 bucket to a local file using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Run the code. 74 | 75 | You must provide 3 parameters, replace the values of: 76 | 77 | * `` by Bucket name. 78 | * `` by Object file name in the bucket. 79 | * `` by local file name. 80 | 81 | Run application: 82 | 83 | ```bash 84 | python s3download.py 85 | ``` 86 | 87 | * Test the application. 88 | 89 | You should see the new file created in your local destiny from the S3 bucket. 90 | -------------------------------------------------------------------------------- /awss3download-rapi/s3download.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3download.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Resource API (high-level) of Boto3. 6 | # Download an object from a S3 bucket to a local file. 7 | # You must provide 3 parameters: 8 | # BUCKET_NAME = Name of the bucket 9 | # OBJECT_NAME = Object file name in the bucket 10 | # LOCAL_FILE_NAME = Local file name 11 | 12 | import sys 13 | import boto3 14 | import botocore 15 | 16 | def main(): 17 | 18 | # Make a list of command line arguments, omitting the [0] element 19 | # which is the script itself. 20 | args = sys.argv[1:] 21 | if len(args) < 3: 22 | print('Not enough parameters.\n'\ 23 | 'Proper Usage is: python s3download.py '\ 24 | ' ') 25 | sys.exit(1) 26 | 27 | bucket_name = args[0] 28 | key_name = args[1] 29 | local_file_name = args[2] 30 | 31 | print('Bucket: ' + bucket_name) 32 | print('Object/Key: ' + key_name) 33 | print('Local file: ' + local_file_name) 34 | 35 | # Instantiate the service resource object 36 | s3_resource = boto3.resource('s3') 37 | 38 | try: 39 | # Instantiate the bucket object 40 | bucket = s3_resource.Bucket(bucket_name) 41 | print('Downloading object ...') 42 | # Download object 43 | bucket.download_file(key_name, local_file_name) 44 | print('Downloaded') 45 | 46 | except botocore.exceptions.ClientError as e: 47 | if e.response['Error']['Code'] == "404": 48 | print("Error: Not Found, problem with the parameters!!") 49 | elif e.response['Error']['Code'] == "400": 50 | print("Error: Bad request, problem with the bucket!!") 51 | else: 52 | raise 53 | 54 | return 55 | 56 | 57 | # This is the standard boilerplate that calls the main() function. 58 | if __name__ == '__main__': 59 | main() 60 | -------------------------------------------------------------------------------- /awss3list-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 List Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | List information about the objects in a S3 bucket using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Create a S3 bucket. 74 | 75 | * Copy some files to the S3 bucket. 76 | 77 | * Run the code. 78 | 79 | You must provide 1 parameter, replace the value of: 80 | 81 | * `` by bucket name. 82 | 83 | Run application: 84 | 85 | ```bash 86 | python s3list.py 87 | ``` 88 | 89 | * Test the application. 90 | 91 | You should see the list of objects stored in the S3 bucket. 92 | -------------------------------------------------------------------------------- /awss3list-capi/s3list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3list.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # List information about the objects in a S3 bucket. 7 | # You must provide 1 parameter: 8 | # BUCKET_NAME = Name of the bucket 9 | 10 | import sys 11 | import boto3 12 | import botocore 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 s3list.py ') 22 | sys.exit(1) 23 | 24 | bucket_name = args[0] 25 | print('Bucket name: ' + bucket_name) 26 | 27 | # Create an S3 Client 28 | s3_client = boto3.client('s3') 29 | 30 | print('Listing objects ...') 31 | # List buckets 32 | try: 33 | s3objects = s3_client.list_objects_v2(Bucket=bucket_name) 34 | if s3objects['KeyCount'] > 0: 35 | for s3object in s3objects['Contents']: 36 | print(' - Object: ' + s3object['Key']) 37 | 38 | except botocore.exceptions.ClientError as e: 39 | if e.response['Error']['Code'] == "NoSuchBucket": 40 | print("Error: Bucket does not exist!!") 41 | elif e.response['Error']['Code'] == "InvalidBucketName": 42 | print("Error: Invalid Bucket name!!") 43 | elif e.response['Error']['Code'] == "AllAccessDisabled": 44 | print("Error: You do not have access to the Bucket!!") 45 | else: 46 | raise 47 | 48 | return 49 | 50 | 51 | # This is the standard boilerplate that calls the main() function. 52 | if __name__ == '__main__': 53 | main() 54 | -------------------------------------------------------------------------------- /awss3list-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 List Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | List information about the objects in a S3 bucket using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Create a S3 bucket. 74 | 75 | * Copy some files to the S3 bucket. 76 | 77 | * Run the code. 78 | 79 | You must provide 1 parameter, replace the value of: 80 | 81 | * `` by bucket name. 82 | 83 | Run application: 84 | 85 | ```bash 86 | python s3list.py 87 | ``` 88 | 89 | * Test the application. 90 | 91 | You should see the list of objects stored in the S3 bucket. 92 | -------------------------------------------------------------------------------- /awss3list-rapi/s3list.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3list.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Resource API (high-level) of Boto3. 6 | # List information about the objects in a S3 bucket. 7 | # You must provide 1 parameter: 8 | # BUCKET_NAME = Name of the bucket 9 | 10 | import sys 11 | import boto3 12 | import botocore 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 s3list.py ') 22 | sys.exit(1) 23 | 24 | bucket_name = args[0] 25 | print('Bucket name: ' + bucket_name) 26 | 27 | # Instantiate the service resource object 28 | s3_resource = boto3.resource('s3') 29 | 30 | # Instantiate the bucket object 31 | bucket = s3_resource.Bucket(bucket_name) 32 | 33 | print('Listing objects ...') 34 | # List buckets 35 | try: 36 | for s3object in bucket.objects.all(): 37 | print(' - Object: ' + s3object.key) 38 | 39 | except botocore.exceptions.ClientError as e: 40 | if e.response['Error']['Code'] == "NoSuchBucket": 41 | print("Error: Bucket does not exist!!") 42 | elif e.response['Error']['Code'] == "InvalidBucketName": 43 | print("Error: Invalid Bucket name!!") 44 | elif e.response['Error']['Code'] == "AllAccessDisabled": 45 | print("Error: You do not have access to the Bucket!!") 46 | else: 47 | raise 48 | 49 | return 50 | 51 | 52 | # This is the standard boilerplate that calls the main() function. 53 | if __name__ == '__main__': 54 | main() 55 | -------------------------------------------------------------------------------- /awss3listall-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 List All Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | List information about all S3 buckets and the objects they contain using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Create some S3 buckets. 74 | 75 | * Copy some files to the S3 buckets. 76 | 77 | * Run the code. 78 | 79 | Run application: 80 | 81 | ```bash 82 | python s3listall.py 83 | ``` 84 | 85 | * Test the application. 86 | 87 | You should see the list of buckets and objects stored in each S3 buckets. 88 | -------------------------------------------------------------------------------- /awss3listall-capi/s3listall.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3listall.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # List information about all S3 buckets and the objects they contain. 7 | 8 | import sys 9 | import boto3 10 | import botocore 11 | 12 | def main(): 13 | 14 | # Create an S3 Client 15 | s3_client = boto3.client('s3') 16 | 17 | print('Listing S3 buckets and objects ...') 18 | # List buckets 19 | list_buckets_resp = s3_client.list_buckets() 20 | for bucket in list_buckets_resp['Buckets']: 21 | print('* Bucket: ' + bucket['Name']) 22 | s3objects = s3_client.list_objects_v2(Bucket=bucket['Name']) 23 | if s3objects['KeyCount'] > 0: 24 | for s3object in s3objects['Contents']: 25 | print(' - Object: ' + s3object['Key']) 26 | 27 | return 28 | 29 | 30 | # This is the standard boilerplate that calls the main() function. 31 | if __name__ == '__main__': 32 | main() 33 | -------------------------------------------------------------------------------- /awss3listall-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 List All Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | List information about all S3 buckets and the objects they contain using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Create some S3 buckets. 74 | 75 | * Copy some files to the S3 buckets. 76 | 77 | * Run the code. 78 | 79 | Run application: 80 | 81 | ```bash 82 | python s3listall.py 83 | ``` 84 | 85 | * Test the application. 86 | 87 | You should see the list of buckets and objects stored in each S3 buckets. 88 | -------------------------------------------------------------------------------- /awss3listall-rapi/s3listall.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3listall.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Resource API (high-level) of Boto3. 6 | # List information about all S3 buckets and the objetcs they contain. 7 | 8 | import sys 9 | import boto3 10 | import botocore 11 | 12 | def main(): 13 | 14 | # Instantiate the service resource object 15 | s3_resource = boto3.resource('s3') 16 | 17 | print('Listing S3 buckets and objects ...') 18 | # List buckets 19 | for bucket in s3_resource.buckets.all(): 20 | print('* Bucket: ' + bucket.name) 21 | for s3object in bucket.objects.all(): 22 | print(' - Object: ' + s3object.key) 23 | 24 | return 25 | 26 | 27 | # This is the standard boilerplate that calls the main() function. 28 | if __name__ == '__main__': 29 | main() 30 | -------------------------------------------------------------------------------- /awss3move-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Move Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Move an object from a S3 bucket to another S3 bucket using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Create a S3 bucket for the source and another S3 bucket for the target. 74 | 75 | * Copy a file to the source S3 bucket. 76 | 77 | * Run the code. 78 | 79 | You must provide 3 parameters, replace the values of: 80 | 81 | * `` by source bucket name. 82 | * `` by source file name. 83 | * `` by destination bucket name. 84 | 85 | Run application: 86 | 87 | ```bash 88 | python s3move.py 89 | ``` 90 | 91 | * Test the application. 92 | 93 | The object from the source S3 bucket should be copied to the target S3 bucket and deleted in the source S3 bucket. 94 | -------------------------------------------------------------------------------- /awss3move-capi/s3move.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3move.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Move an object from a S3 bucket to another S3 bucket. 7 | # You must provide 3 parameters: 8 | # SOURCE_BUCKET = Source bucket name 9 | # SOURCE_OBJECT = Source file name 10 | # DESTINATION_BUCKET = Destination bucket name 11 | 12 | import sys 13 | import boto3 14 | import botocore 15 | 16 | def main(): 17 | 18 | # Make a list of command line arguments, omitting the [0] element 19 | # which is the script itself. 20 | args = sys.argv[1:] 21 | if len(args) < 3: 22 | print('Not enough parameters.\n'\ 23 | 'Proper Usage is: python s3move.py '\ 24 | ' ') 25 | sys.exit(1) 26 | 27 | source_bucket_name = args[0] 28 | source_key = args[1] 29 | destination_bucket_name = args[2] 30 | destination_key = source_key 31 | print('From - bucket: ' + source_bucket_name) 32 | print('From - object: ' + source_key) 33 | print('To - bucket: ' + destination_bucket_name) 34 | print('To - object: ' + destination_key) 35 | 36 | # Create an S3 Client 37 | s3_client = boto3.client('s3') 38 | 39 | try: 40 | # Copy the object 41 | print('Moving object ...') 42 | copy_source = { 43 | 'Bucket': source_bucket_name, 44 | 'Key': source_key 45 | } 46 | s3_client.copy(copy_source, destination_bucket_name, destination_key) 47 | # Delete the object from source bucket 48 | response = s3_client.delete_object(Bucket=source_bucket_name, Key=source_key) 49 | print(response) 50 | print('\nMoved') 51 | 52 | except botocore.exceptions.ClientError as e: 53 | if e.response['Error']['Code'] == "404": 54 | print("Error: Not Found, problem with the parameters!!") 55 | elif e.response['Error']['Code'] == "400": 56 | print("Error: Bad request, problem with the bucket!!") 57 | elif e.response['Error']['Code'] == "403": 58 | print("Error: Forbidden, bucket forbidden!!") 59 | elif e.response['Error']['Code'] == "AccessDenied": 60 | print("Error: Access denied!!") 61 | elif e.response['Error']['Code'] == "InvalidBucketName": 62 | print("Error: Invalid bucket name!!") 63 | elif e.response['Error']['Code'] == "NoSuchBucket": 64 | print("Error: No such bucket!!") 65 | else: 66 | raise 67 | 68 | return 69 | 70 | 71 | # This is the standard boilerplate that calls the main() function. 72 | if __name__ == '__main__': 73 | main() 74 | -------------------------------------------------------------------------------- /awss3move-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Move Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Move an object from a S3 bucket to another S3 bucket using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Create a S3 bucket for the source and another S3 bucket for the target. 74 | 75 | * Copy a file to the source S3 bucket. 76 | 77 | * Run the code. 78 | 79 | You must provide 3 parameters, replace the values of: 80 | 81 | * `` by source bucket name. 82 | * `` by source file name. 83 | * `` by destination bucket name. 84 | 85 | Run application: 86 | 87 | ```bash 88 | python s3move.py 89 | ``` 90 | 91 | * Test the application. 92 | 93 | The object from the source S3 bucket should be copied to the target S3 bucket and deleted in the source S3 bucket. 94 | -------------------------------------------------------------------------------- /awss3move-rapi/s3move.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3move.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Resource API (high-level) of Boto3. 6 | # Move an object from a S3 bucket to another S3 bucket. 7 | # You must provide 3 parameters: 8 | # SOURCE_BUCKET = Source bucket name 9 | # SOURCE_OBJECT = Source file name 10 | # DESTINATION_BUCKET = Destination bucket name 11 | 12 | import sys 13 | import boto3 14 | import botocore 15 | 16 | def main(): 17 | 18 | # Make a list of command line arguments, omitting the [0] element 19 | # which is the script itself. 20 | args = sys.argv[1:] 21 | if len(args) < 3: 22 | print('Not enough parameters.\n'\ 23 | 'Proper Usage is: python s3move.py '\ 24 | ' ') 25 | sys.exit(1) 26 | 27 | source_bucket_name = args[0] 28 | source_key = args[1] 29 | destination_bucket_name = args[2] 30 | destination_key = source_key 31 | print('From - bucket: ' + source_bucket_name) 32 | print('From - object: ' + source_key) 33 | print('To - bucket: ' + destination_bucket_name) 34 | print('To - object: ' + destination_key) 35 | 36 | # Instantiate the service resource object 37 | s3_resource = boto3.resource('s3') 38 | 39 | try: 40 | print('Moving object ...') 41 | # Instantiate the destination bucket 42 | dest_bucket = s3_resource.Bucket(destination_bucket_name) 43 | # Copy the object 44 | copy_source = { 45 | 'Bucket': source_bucket_name, 46 | 'Key': source_key 47 | } 48 | dest_bucket.copy(copy_source, destination_key) 49 | # Instantiate the source bucket 50 | source_bucket = s3_resource.Bucket(source_bucket_name) 51 | # Delete the object from source bucket 52 | source_obj = source_bucket.Object(source_key) 53 | response = source_obj.delete() 54 | print(response) 55 | print('\nMoved') 56 | 57 | except botocore.exceptions.ClientError as e: 58 | if e.response['Error']['Code'] == "404": 59 | print("Error: Not Found, problem with the parameters!!") 60 | elif e.response['Error']['Code'] == "400": 61 | print("Error: Bad request, problem with the bucket!!") 62 | elif e.response['Error']['Code'] == "403": 63 | print("Error: Forbidden, bucket forbidden!!") 64 | elif e.response['Error']['Code'] == "AccessDenied": 65 | print("Error: Access denied!!") 66 | elif e.response['Error']['Code'] == "InvalidBucketName": 67 | print("Error: Invalid bucket name!!") 68 | elif e.response['Error']['Code'] == "NoSuchBucket": 69 | print("Error: No such bucket!!") 70 | else: 71 | raise 72 | 73 | return 74 | 75 | 76 | # This is the standard boilerplate that calls the main() function. 77 | if __name__ == '__main__': 78 | main() 79 | -------------------------------------------------------------------------------- /awss3upload-capi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Upload Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Upload a local file to a S3 bucket using the Client API (low-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Client API (low-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Run the code. 74 | 75 | You must provide 3 parameters, replace the values of: 76 | 77 | * `` by Bucket name. 78 | * `` by Object file name in the bucket. 79 | * `` by local file name. 80 | 81 | Run application: 82 | 83 | ```bash 84 | python s3upload.py 85 | ``` 86 | 87 | * Test the application. 88 | 89 | You should see the new object created in the S3 bucket. 90 | -------------------------------------------------------------------------------- /awss3upload-capi/s3upload.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3upload.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Client API (low-level) of Boto3. 6 | # Upload a local file to a S3 bucket. 7 | # You must provide 1 parameter: 8 | # BUCKET_NAME = Name of the bucket 9 | # OBJECT_NAME = Object file name in the bucket 10 | # LOCAL_FILE_NAME = Local file name 11 | 12 | import sys 13 | import os 14 | import boto3 15 | import botocore 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 s3upload.py '\ 25 | ' ') 26 | sys.exit(1) 27 | 28 | bucket_name = args[0] 29 | key_name = args[1] 30 | local_file_name = args[2] 31 | print('Bucket: ' + bucket_name) 32 | print('Object/Key: ' + key_name) 33 | print('Local file: ' + local_file_name) 34 | 35 | # Create an S3 Client 36 | s3_client = boto3.client('s3') 37 | 38 | if not os.path.isfile(local_file_name): 39 | print("Error: File Not Found!!") 40 | sys.exit(1) 41 | 42 | # Upload object 43 | try: 44 | print('Uploading object ...') 45 | s3_client.upload_file(local_file_name, bucket_name, key_name) 46 | print('Uploaded') 47 | 48 | except botocore.exceptions.ClientError as e: 49 | if e.response['Error']['Code'] == "NoSuchBucket": 50 | print("Error: Bucket does not exist!!") 51 | elif e.response['Error']['Code'] == "InvalidBucketName": 52 | print("Error: Invalid Bucket name!!") 53 | elif e.response['Error']['Code'] == "AllAccessDisabled": 54 | print("Error: You do not have access to the Bucket!!") 55 | else: 56 | raise 57 | 58 | return 59 | 60 | 61 | # This is the standard boilerplate that calls the main() function. 62 | if __name__ == '__main__': 63 | main() 64 | -------------------------------------------------------------------------------- /awss3upload-rapi/README.md: -------------------------------------------------------------------------------- 1 | # AWS S3 Upload Python example 2 | 3 | This folder contains a Python application example that handles S3 buckets on AWS (Amazon Web Services). 4 | 5 | Upload a local file to a S3 bucket using the Resource API (high-level) of Boto 3. 6 | 7 | ## Requirements 8 | 9 | * You must have an [Amazon Web Services (AWS)](http://aws.amazon.com/) account. 10 | 11 | * The code was written for: 12 | 13 | * Python 3 14 | * AWS SDK for Python (Boto3) 15 | 16 | * This example uses Resource API (high-level) of Boto 3. 17 | 18 | * Install the AWS SDK for Python (Boto3). 19 | 20 | Install the latest Boto 3 release via pip: 21 | 22 | ```bash 23 | pip install boto3 24 | ``` 25 | 26 | ## Using the code 27 | 28 | * Configure your AWS access keys. 29 | 30 | **Important:** For security, it is strongly recommend that you use IAM users instead of the root account for AWS access. 31 | 32 | When you initialize a new service client without supplying any arguments, the AWS SDK for Java attempts to find AWS credentials by using the default credential provider chain. 33 | 34 | Setting your credentials for use by the AWS SDK for Java can be done in a number of ways, but here are the recommended approaches: 35 | 36 | * The default credential profiles file. 37 | 38 | Set credentials in the AWS credentials profile file on your local system, located at: 39 | 40 | * `~/.aws/credentials` on Linux, macOS, or Unix. 41 | 42 | * `C:\Users\USERNAME\.aws\credentials` on Windows. 43 | 44 | This file should contain lines in the following format: 45 | 46 | ```bash 47 | [default] 48 | aws_access_key_id = 49 | aws_secret_access_key = 50 | ``` 51 | Replace the values of `` and `` by your AWS credentials. 52 | 53 | * Environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`. 54 | 55 | Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 56 | 57 | To set these variables on Linux, macOS, or Unix, use `export`: 58 | 59 | ```bash 60 | export AWS_ACCESS_KEY_ID= 61 | export AWS_SECRET_ACCESS_KEY= 62 | ``` 63 | 64 | To set these variables on Windows, use `set`: 65 | 66 | ```bash 67 | set AWS_ACCESS_KEY_ID= 68 | set AWS_SECRET_ACCESS_KEY= 69 | ``` 70 | 71 | Replace the values of `` and `` by your AWS credentials. 72 | 73 | * Run the code. 74 | 75 | You must provide 3 parameters, replace the values of: 76 | 77 | * `` by Bucket name. 78 | * `` by Object file name in the bucket. 79 | * `` by local file name. 80 | 81 | Run application: 82 | 83 | ```bash 84 | python s3upload.py 85 | ``` 86 | 87 | * Test the application. 88 | 89 | You should see the new object created in the S3 bucket. 90 | -------------------------------------------------------------------------------- /awss3upload-rapi/s3upload.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # s3upload.py 4 | # It is an example that handles S3 buckets on AWS. 5 | # It uses Resource API (high-level) of Boto3. 6 | # Upload a local file to a S3 bucket. 7 | # You must provide 1 parameter: 8 | # BUCKET_NAME = Name of the bucket 9 | # OBJECT_NAME = Object file name in the bucket 10 | # LOCAL_FILE_NAME = Local file name 11 | 12 | import sys 13 | import os 14 | import boto3 15 | import botocore 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 s3upload.py '\ 25 | ' ') 26 | sys.exit(1) 27 | 28 | bucket_name = args[0] 29 | key_name = args[1] 30 | local_file_name = args[2] 31 | print('Bucket: ' + bucket_name) 32 | print('Object/Key: ' + key_name) 33 | print('Local file: ' + local_file_name) 34 | 35 | # Instantiate the service resource object 36 | s3_resource = boto3.resource('s3') 37 | 38 | if not os.path.isfile(local_file_name): 39 | print("Error: File Not Found!!") 40 | sys.exit(1) 41 | 42 | try: 43 | # Instantiate the bucket object 44 | bucket = s3_resource.Bucket(bucket_name) 45 | print('Uploading object ...') 46 | # Upload object 47 | bucket.upload_file(local_file_name, key_name) 48 | print('Uploaded') 49 | 50 | except botocore.exceptions.ClientError as e: 51 | if e.response['Error']['Code'] == "NoSuchBucket": 52 | print("Error: Bucket does not exist!!") 53 | elif e.response['Error']['Code'] == "InvalidBucketName": 54 | print("Error: Invalid Bucket name!!") 55 | elif e.response['Error']['Code'] == "AllAccessDisabled": 56 | print("Error: You do not have access to the Bucket!!") 57 | else: 58 | raise 59 | 60 | return 61 | 62 | 63 | # This is the standard boilerplate that calls the main() function. 64 | if __name__ == '__main__': 65 | main() 66 | --------------------------------------------------------------------------------