b3u module

Boto3 URI utility library that supports extraction of Boto3 configuration data from AWS resource URIs.

class b3u.b3u.b3u(uri: str)

Bases: object

credentials() dict

Extract configuration data (only credentials) from a URI string.

>>> b3u('s3://abc:xyz@bucket/object.data').credentials()
{'aws_access_key_id': 'abc', 'aws_secret_access_key': 'xyz'}

The format abc:xyz:123 can be used to specify a session token (which is the third component, 123, in this example) as part of a URI.

>>> cs = b3u('s3://abc:xyz:123@bucket/object.data').credentials()
>>> for (k, v) in sorted(cs.items()):
...     print(k, v)
aws_access_key_id abc
aws_secret_access_key xyz
aws_session_token 123

If the aws_secret_access_key contains a slash, it should not be escaped or URL encoded.

Returns

A dictionary with the following keys (if available): aws_access_key_id, aws_secret_access_key, aws_session_token

configuration(safe: bool = True) dict

Extract configuration data (both credentials and non-credentials) from a URI string.

Parameters

safe – If true, only return standard AWS properties that can be passed to. If false, returns all.

>>> b3u('s3://abc:xyz@bucket/object.data?other_param=other_value').configuration()
{'aws_access_key_id': 'abc', 'aws_secret_access_key': 'xyz'}
>>> b3u('s3://abc:xyz@bucket/object.data?other_param=other_value').configuration(False)
{'aws_access_key_id': 'abc', 'aws_secret_access_key': 'xyz', 'other_param': 'other_value'}
for_client(safe: bool = True) dict

Extract parameters for a client constructor from a URI string.

Parameters

safe – If true, only return standard AWS properties that can be passed to. If false, return all.

>>> ps = b3u('s3://abc:xyz@bucket/object.data?region_name=us-east-1').for_client()
>>> for (k, v) in sorted(ps.items()):
...     print(k, v)
aws_access_key_id abc
aws_secret_access_key xyz
region_name us-east-1
service_name s3
>>> ps = b3u('s3://abc:xyz@bucket/object.data?other_param=other_value').for_client(False)
>>> for (k, v) in sorted(ps.items()):
...     print(k, v)
aws_access_key_id abc
aws_secret_access_key xyz
other_param other_value
service_name s3
for_resource(safe: bool = True) dict

Extract parameters for a resource constructor from a URI string. This function is a synonym for the for_client function.

Parameters

safe – If true, only return standard AWS properties that can be passed to. If false, returns all

>>> ps = b3u('s3://abc:xyz@bucket/object.data?region_name=us-east-1').for_resource()
>>> for (k, v) in sorted(ps.items()):
...     print(k, v)
aws_access_key_id abc
aws_secret_access_key xyz
region_name us-east-1
service_name s3
for_get() dict

Extract resource names from a URI for supported AWS services. Currently, only S3 and SSM are supported.

>>> b3u('s3://abc:xyz@bucket/object.data').for_get()
{'Bucket': 'bucket', 'Key': 'object.data'}
>>> b3u('ssm://ABC:XYZ@/path/to/parameter?region_name=us-east-1').for_get()
{'Name': '/path/to/parameter'}
cred() dict

Concise synonym for the credentials function.

>>> cs = b3u('s3://abc:xyz:123@bucket/object.data').cred()
>>> for (k, v) in sorted(cs.items()):
...     print(k, v)
aws_access_key_id abc
aws_secret_access_key xyz
aws_session_token 123
conf(safe: bool = True) dict

Concise synonym for the _configuration function.

>>> b3u('s3://abc:xyz@bucket/object.data').conf()
{'aws_access_key_id': 'abc', 'aws_secret_access_key': 'xyz'}
to_string() str

Constructs a uri based off of whatever the current properties of this object are

>>> b3u('s3://abc:xyz@bucket/object.data?region_name=us-east-1').to_string()
's3://abc:xyz@bucket/object.data?region_name=us-east-1'
>>> b = b3u('s3://abc:xyz@bucket/object.data?region_name=us-east-1')
>>> b.aws_access_key_id = 'LMN'
>>> b.to_string()
's3://LMN:xyz@bucket/object.data?region_name=us-east-1'