botocore Documentation

repository·develop·Indexed 23 days ago

https://github.com/boto/botocore

botocore is a low-level interface to Amazon Web Services and serves as the foundational library for the AWS CLI and the boto3 SDK. It provides a Client interface for interacting with AWS services, featuring dynamic client generation via ClientCreator, support for paginators and waiters, and a mechanism for resolving endpoint URLs through ClientEndpointBridge.

Tokens
6.7K
Snippets
21
Records
47
Agent score
61%

What's inside botocore

  1. How Botocore Paginators work

    develop

    Some AWS operations return incomplete result sets that require subsequent requests to retrieve the full data (e.g., S3 list_objects returns up to 1000 objects at a time).

    Paginators provide an abstraction over this process. Instead of manually managing markers or tokens to fetch subsequent pages, you use a Paginator object to iterate over the entire result set automatically.

  2. Rules for using AI tooling in contributions

    develop

    The use of AI tools for assisted development is encouraged, but submissions must follow these rules:

    • Human Review: All AI-sourced issues and pull requests must be reviewed by a human before submission.
    • Disclosure: Submissions must include a statement such as: generated by AI tools, and reviewed by <person>.
    • Quality: Submissions must be genuine improvements. Nuisance PRs or attempts to artificially inflate submission counts are not acceptable and may result in restricted interaction with the repository.
  3. Optimize connection pooling with Clients

    develop

    In the old interface, connection pooling was tied to an endpoint object. In the new client interface, connection pooling is tied to the client instance.

    To reuse existing HTTP connections and improve performance, use a single client instance for multiple API calls instead of creating new clients for every request.

    # Using the same client will reuse any existing HTTP
    # connections the client was using.
    s3 = session.get_client('s3', 'us-west-2')
    for obj in s3.list_objects(Bucket='amzn-s3-demo-bucket')['Contents']:
        name = obj['Key']
        print(s3.head_object(Bucket='amzn-s3-demo-bucket', Key=name))
  4. Argument casing and parameter passing in Botocore clients

    develop

    Unlike the boto library, Botocore client methods require parameters to be passed as **kwargs using CamelCase names. This design choice ensures that the casing of input arguments matches the casing of the response data returned by AWS services, facilitating easier 'round-tripping' (using values from a response directly as inputs for subsequent calls) without manual case conversion. Additionally, the parameter names in Botocore match the names used in official AWS API documentation.

    # Botocore usage (CamelCase)
    ddb = session.create_client('dynamodb')
    ddb.describe_table(TableName='mytable')
    
    # Boto usage (snake_case)
    layer1.describe_table(table_name='mytable')
  5. How Botocore Events work

    develop
    Botocore uses an event system that allows users to register handlers (callables) to customize or extend library behavior without modifying internals. When an event is emitted, all registered handlers are invoked in the order they were registered. The primary interface for managing these events is the botocore.session.Session class, which provides methods to register and unregister handlers.
  6. Install botocore

    develop

    You can install botocore directly via pip, or install it from source using a virtual environment.

    Install via pip

    $ pip install botocore

    Install from source

    Assuming you have python and virtualenv installed:

    $ git clone https://github.com/boto/botocore.git
    $ cd botocore
    $ python -m venv .venv
    $ source .venv/bin/activate
    $ python -m pip install -r requirements.txt
    $ python -m pip install -e .
  7. Create and use a Paginator

    develop

    To use pagination, follow these steps:

    1. Create a session and a client using botocore.session.get_session().
    2. Obtain a reusable Paginator object by calling client.get_paginator('operation_name').
    3. Call the .paginate() method on the paginator, passing in the required operation parameters. This returns a PageIterator.
    4. Iterate over the PageIterator to process each page of results.
    import botocore.session
    
    # Create a session and a client
    session = botocore.session.get_session()
    client = session.create_client('s3', region_name='us-west-2')
    
    # Create a reusable Paginator
    paginator = client.get_paginator('list_objects')
    
    # Create a PageIterator from the Paginator
    page_iterator = paginator.paginate(Bucket='amzn-s3-demo-bucket')
    
    for page in page_iterator:
        print(page['Contents'])
  8. Guidelines for submitting code to Botocore

    develop

    When contributing code to Botocore, adhere to the following requirements:

    • Licensing: All submitted code is released under the Apache license.
    • Testing: Maintain high code coverage. Every bug fix and feature addition must include unit tests. Coverage is monitored via coveralls.
    • Data Files:
      • Issues with JSON service descriptions (e.g., botocore/data/aws/s3/2006-03-01/service-2.json) should be reported as issues for upstream fixes.
      • Paginators, waiters, and endpoints are generated upstream. If you find issues in botocore/data/ files like _endpoints.json, *.paginators-1.json, or *.waiters-2.json, report them via GitHub issues.
      • Changes to botocore/data/_retry.json are encouraged.
    • Compatibility: Code must work on all supported Python versions and be cross-platform (Linux, Windows, and Mac OS X).
    • Feature Requests: For significant new features, discuss them via a GitHub issue before implementation to avoid duplication of effort.
  9. Generate Botocore documentation locally

    develop

    Botocore uses Sphinx to generate its documentation. To build the HTML documentation on your local machine, install the necessary requirements and run the Sphinx make command from the docs directory.

    $ pip install -r requirements-docs.txt
    $ cd docs
    $ make html
  10. Configure AWS credentials and region

    develop

    Before using botocore, you must configure your AWS credentials and default region.

    Set up credentials

    Create or edit your credentials file (e.g., ~/.aws/credentials) and add your keys:

    [default]
    aws_access_key_id = YOUR_KEY
    aws_secret_access_key = YOUR_SECRET

    Set up default region

    Create or edit your config file (e.g., ~/.aws/config) to specify a region:

    [default]
    region=us-east-1
  11. Update operation and method names for Clients

    develop

    The naming convention for calling operations changes between the two interfaces:

    • Old Interface: Operations are retrieved using the service's casing (typically CamelCase), e.g., service.get_operation('ListObjects').
    • New Client Interface: Methods are snake_case to follow Python conventions and map 1-to-1 with operation names, e.g., client.list_objects().
    # Old
    service = session.get_service('s3')
    list_objects = service.get_operation('ListObjects')
    
    # New
    s3 = session.get_client('s3', 'us-west-2')
    list_objects = s3.list_objects