本文整理汇总了Python中boto3.session.client方法的典型用法代码示例。如果您正苦于以下问题:Python session.client方法的具体用法?Python session.client怎么用?Python session.client使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto3.session
的用法示例。
在下文中一共展示了session.client方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def __init__(self, client=None, **kwargs):
self.dimensions = []
self.timers = {}
self.dimension_stack = []
self.storage_resolution = 60
self.use_stream_id = kwargs.get('UseStreamId', True)
if self.use_stream_id:
self.stream_id = str(uuid.uuid4())
self.with_dimension('MetricStreamId', self.stream_id)
else:
self.stream_id = None
if client:
self.client = client
else:
profile = kwargs.get('Profile')
if profile:
session = boto3.session.Session(profile_name=profile)
self.client = session.client('cloudwatch')
else:
self.client = boto3.client('cloudwatch')
示例2: __aenter__
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def __aenter__(self):
client = await self.client.__aenter__()
service_model = client.meta.service_model
# Create a ServiceContext object to serve as a reference to
# important read-only information about the general service.
service_context = boto3.utils.ServiceContext(
service_name=self.service_name, service_model=service_model,
resource_json_definitions=self.resource_model['resources'],
service_waiter_model=boto3.utils.LazyLoadedWaiterModel(
self.session._session, self.service_name, self.api_version)
)
# Create the service resource class.
self.cls = (await self.session.resource_factory.load_from_definition(
resource_name=self.service_name,
single_resource_json_definition=self.resource_model['service'],
service_context=service_context
))(client=client)
return self.cls
示例3: get_user_data_configuration
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def get_user_data_configuration():
"""Retrieve and update the application configuration with information from the user-data
Returns:
`None`
"""
from cloud_inquisitor import get_local_aws_session, app_config
kms_region = app_config.kms_region
session = get_local_aws_session()
if session.get_credentials().method == 'iam-role':
kms = session.client('kms', region_name=kms_region)
else:
sts = session.client('sts')
audit_role = sts.assume_role(RoleArn=app_config.aws_api.instance_role_arn, RoleSessionName='cloud_inquisitor')
kms = boto3.session.Session(
audit_role['Credentials']['AccessKeyId'],
audit_role['Credentials']['SecretAccessKey'],
audit_role['Credentials']['SessionToken'],
).client('kms', region_name=kms_region)
user_data_url = app_config.user_data_url
res = requests.get(user_data_url)
if res.status_code == 200:
data = kms.decrypt(CiphertextBlob=b64decode(res.content))
kms_config = json.loads(zlib.decompress(data['Plaintext']).decode('utf-8'))
app_config.database_uri = kms_config['db_uri']
else:
raise RuntimeError('Failed loading user-data, cannot continue: {}: {}'.format(res.status_code, res.content))
示例4: _record_metric
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def _record_metric(self, metric_data):
logger.debug('log: {}'.format(metric_data))
if metric_data:
self.client.put_metric_data(
Namespace=self.namespace,
MetricData=metric_data,
)
示例5: get_metrics
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def get_metrics(self, **kwargs):
mn = kwargs.get('MetricName')
return self.client.list_metrics(Namespace=self.namespace,
MetricName=mn)['Metrics']
示例6: create_bucket
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def create_bucket(session: boto3.session.Session, bucket_name):
s3_client = session.client('s3')
if not bucket_exists(s3client=s3client, bucket_name=bucket_name):
s3_client.create_bucket(
ACL='public-read',
Bucket=bucket_name,
CreateBucketConfiguration={
'LocationConstraint': s3_client.meta.region_name,
},
)
示例7: __init__
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def __init__(self, session, service_name, region_name, api_version, use_ssl, verify,
endpoint_url, aws_access_key_id, aws_secret_access_key, aws_session_token,
config, resource_model):
self.service_name = service_name
self.resource_model = resource_model
self.session = session
self.api_version = api_version
self.cls = None
self.client = session.client(
service_name, region_name=region_name, api_version=api_version,
use_ssl=use_ssl, verify=verify, endpoint_url=endpoint_url,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token, config=config)
示例8: __init__
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def __init__(self, service_name, aws_access_key_id, aws_secret_access_key, region_name):
session = boto3.session.Session(aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region_name)
self.aws_client = session.client(service_name)
示例9: get_aws_session
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def get_aws_session(account):
"""Function to return a boto3 Session based on the account passed in the first argument.
Args:
account (:obj:`Account`): Account to create the session object for
Returns:
:obj:`boto3:boto3.session.Session`
"""
from cloud_inquisitor.config import dbconfig
from cloud_inquisitor.plugins.types.accounts import AWSAccount
if not isinstance(account, AWSAccount):
raise InquisitorError('Non AWSAccount passed to get_aws_session, got {}'.format(account.__class__.__name__))
# If no keys are on supplied for the account, use sts.assume_role instead
session = get_local_aws_session()
if session.get_credentials().method in ['iam-role', 'env', 'explicit']:
sts = session.client('sts')
else:
# If we are not running on an EC2 instance, assume the instance role
# first, then assume the remote role
temp_sts = session.client('sts')
audit_sts_role = temp_sts.assume_role(
RoleArn=app_config.aws_api.instance_role_arn,
RoleSessionName='inquisitor'
)
sts = boto3.session.Session(
audit_sts_role['Credentials']['AccessKeyId'],
audit_sts_role['Credentials']['SecretAccessKey'],
audit_sts_role['Credentials']['SessionToken']
).client('sts')
role = sts.assume_role(
RoleArn='arn:aws:iam::{}:role/{}'.format(
account.account_number,
dbconfig.get('role_name', default='cinq_role')
),
RoleSessionName='inquisitor'
)
sess = boto3.session.Session(
role['Credentials']['AccessKeyId'],
role['Credentials']['SecretAccessKey'],
role['Credentials']['SessionToken']
)
return sess
示例10: resource
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def resource(self, service_name, region_name=None, api_version=None,
use_ssl=True, verify=None, endpoint_url=None,
aws_access_key_id=None, aws_secret_access_key=None,
aws_session_token=None, config=None):
try:
resource_model = self._loader.load_service_model(
service_name, 'resources-1', api_version)
except UnknownServiceError:
available = self.get_available_resources()
has_low_level_client = (
service_name in self.get_available_services())
raise ResourceNotExistsError(service_name, available,
has_low_level_client)
except DataNotFoundError:
# This is because we've provided an invalid API version.
available_api_versions = self._loader.list_api_versions(
service_name, 'resources-1')
raise UnknownAPIVersionError(
service_name, api_version, ', '.join(available_api_versions))
if api_version is None:
# Even though botocore's load_service_model() can handle
# using the latest api_version if not provided, we need
# to track this api_version in boto3 in order to ensure
# we're pairing a resource model with a client model
# of the same API version. It's possible for the latest
# API version of a resource model in boto3 to not be
# the same API version as a service model in botocore.
# So we need to look up the api_version if one is not
# provided to ensure we load the same API version of the
# client.
#
# Note: This is relying on the fact that
# loader.load_service_model(..., api_version=None)
# and loader.determine_latest_version(..., 'resources-1')
# both load the same api version of the file.
api_version = self._loader.determine_latest_version(
service_name, 'resources-1')
# Creating a new resource instance requires the low-level client
# and service model, the resource version and resource JSON data.
# We pass these to the factory and get back a class, which is
# instantiated on top of the low-level client.
if config is not None:
if config.user_agent_extra is None:
config = copy.deepcopy(config)
config.user_agent_extra = 'Resource'
else:
config = AioConfig(user_agent_extra='Resource')
# client = blah part has been moved into a dodgy context class
return ResourceCreaterContext(self, service_name, region_name, api_version,
use_ssl, verify, endpoint_url, aws_access_key_id,
aws_secret_access_key, aws_session_token, config,
resource_model)
示例11: _register_default_handlers
# 需要导入模块: from boto3 import session [as 别名]
# 或者: from boto3.session import client [as 别名]
def _register_default_handlers(self):
# S3 customizations
self._session.register(
'creating-client-class.s3',
boto3.utils.lazy_call(
'aioboto3.s3.inject.inject_s3_transfer_methods'))
self._session.register(
'creating-resource-class.s3.Bucket',
boto3.utils.lazy_call(
'boto3.s3.inject.inject_bucket_methods'))
self._session.register(
'creating-resource-class.s3.Object',
boto3.utils.lazy_call(
'boto3.s3.inject.inject_object_methods'))
self._session.register(
'creating-resource-class.s3.ObjectSummary',
boto3.utils.lazy_call(
'boto3.s3.inject.inject_object_summary_methods'))
# DynamoDb customizations
self._session.register(
'creating-resource-class.dynamodb',
boto3.utils.lazy_call(
'boto3.dynamodb.transform.register_high_level_interface'),
unique_id='high-level-dynamodb')
self._session.register(
'creating-resource-class.dynamodb.Table',
boto3.utils.lazy_call(
'aioboto3.dynamodb.table.register_table_methods'),
unique_id='high-level-dynamodb-table')
# EC2 Customizations
self._session.register(
'creating-resource-class.ec2.ServiceResource',
boto3.utils.lazy_call(
'boto3.ec2.createtags.inject_create_tags'))
self._session.register(
'creating-resource-class.ec2.Instance',
boto3.utils.lazy_call(
'boto3.ec2.deletetags.inject_delete_tags',
event_emitter=self.events))