本文整理汇总了Python中boto3.setup_default_session方法的典型用法代码示例。如果您正苦于以下问题:Python boto3.setup_default_session方法的具体用法?Python boto3.setup_default_session怎么用?Python boto3.setup_default_session使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto3
的用法示例。
在下文中一共展示了boto3.setup_default_session方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_queue
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def test_create_queue(self):
boto3.setup_default_session()
sqs_inst = sqs.SQSTalk()
sqs_inst._get_or_create_queue('LowPriorityTest1')
sqs_inst._get_or_create_queue('HighPriorityTest2')
expected_low_queue = sqs_inst._sqs.Queue('https://queue.amazonaws.com/123456789012/'
'LowPriorityTest1')
expected_hi_queue = sqs_inst._sqs.Queue('https://queue.amazonaws.com/123456789012/'
'HighPriorityTest2')
self.assertEqual(len(sqs_inst._queues), 2)
self.assertEqual(expected_low_queue, sqs_inst._queues['LowPriorityTest1'])
self.assertEqual(expected_hi_queue, sqs_inst._queues['HighPriorityTest2'])
示例2: test_get_queues
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def test_get_queues(self):
boto3.setup_default_session()
sqs_inst = sqs.SQSTalk()
sqs_inst._get_or_create_queue('LowPriorityTest3')
sqs_inst._get_or_create_queue('HighPriorityTest4')
queues = sqs_inst.get_all_queues()
print(queues)
self.assertEqual(len(queues), 2)
queues = sqs_inst.get_all_queues('Low')
self.assertEqual(len(queues), 1)
sqs_inst.get_all_queues('High')
self.assertEqual(len(queues), 1)
示例3: set_up_boto3
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def set_up_boto3():
"""
A DRY place to make sure AWS credentials in settings override
environment based credentials. Boto3 will fall back to:
http://boto3.readthedocs.io/en/latest/guide/configuration.html
Taken from https://github.com/datadesk/django-bakery/blob/
a2f1f74b03951450d797ec70cc9872d6c694e1e3/bakery/management/commands/__init__.py#L8
"""
session_kwargs = {}
if hasattr(settings, "AWS_ACCESS_KEY_ID"):
session_kwargs["aws_access_key_id"] = settings.AWS_ACCESS_KEY_ID
if hasattr(settings, "AWS_SECRET_ACCESS_KEY"):
session_kwargs["aws_secret_access_key"] = settings.AWS_SECRET_ACCESS_KEY
boto3.setup_default_session(**session_kwargs)
示例4: get_client
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def get_client(
client,
profile_name,
aws_access_key_id,
aws_secret_access_key,
region=None,
):
"""Shortcut for getting an initialized instance of the boto3 client."""
boto3.setup_default_session(
profile_name=profile_name,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region,
)
return boto3.client(client)
示例5: run
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def run(self, command: str) -> None:
"""Execute one of the available commands.
Args:
command: Command in self.commands.
"""
boto3.setup_default_session(region_name=self._config.aws_region)
# Validate the configuration.
try:
if command not in {'clone_rules', 'compile_rules', 'configure', 'unit_test'}:
self._config.validate()
getattr(self, command)() # Command validation already happened in the ArgumentParser.
except InvalidConfigError as error:
sys.exit('ERROR: {}\nPlease run "python3 manage.py configure"'.format(error))
except TestFailureError as error:
sys.exit('TEST FAILED: {}'.format(error))
示例6: setup_aws_client
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def setup_aws_client(config):
role_arn = "arn:aws:iam::{}:role/{}".format(config['account_id'].replace('-', ''),
config['role_name'])
session = Session()
fetcher = AssumeRoleCredentialFetcher(
session.create_client,
session.get_credentials(),
role_arn,
extra_args={
'DurationSeconds': 3600,
'RoleSessionName': 'TapS3CSV',
'ExternalId': config['external_id']
},
cache=JSONFileCache()
)
refreshable_session = Session()
refreshable_session.register_component(
'credential_provider',
CredentialResolver([AssumeRoleProvider(fetcher)])
)
LOGGER.info("Attempting to assume_role on RoleArn: %s", role_arn)
boto3.setup_default_session(botocore_session=refreshable_session)
示例7: setup_aws_client
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def setup_aws_client(cls, config):
aws_access_key_id = config['aws_access_key_id']
aws_secret_access_key = config['aws_secret_access_key']
LOGGER.info('Attempting to create AWS session')
boto3.setup_default_session(aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key)
示例8: __iter__
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def __iter__(self):
# over-ride the __iter__ to get the ec2 resource object for the current code (backwards compatibility)
# method 1 for ec2
# ec2_it = self.ec2_resource.instances.all()
# return ec2_it
# boto3 ec2 and cloudwatch data
ec2_resource_all = {}
import boto3
# TODO cannot use directly use the iterator exposed in "ec2_it"
# because it would return the dataframes from Cloudwatch,
# whereas in the cloudwatch data fetch here, the data gets cached to redis.
# Once the redshift.iterator can cache to redis, then the cloudwatch part here
# can also be dropped, as well as using the "ec2_it" iterator directly
# for ec2_dict in self.ec2_it:
for ec2_dict, ec2_id, ec2_launchtime, _ in super().__iter__():
if ec2_dict['Region'] not in ec2_resource_all.keys():
boto3.setup_default_session(region_name = ec2_dict['Region'])
ec2_resource_all[ec2_dict['Region']] = boto3.resource('ec2')
ec2_resource_single = ec2_resource_all[ec2_dict['Region']]
ec2_l = ec2_resource_single.instances.filter(InstanceIds=[ec2_dict['InstanceId']])
ec2_l = list(ec2_l)
if len(ec2_l)==0:
continue # not found
# yield first entry
ec2_obj = ec2_l[0]
ec2_obj.region_name = ec2_dict['Region']
yield ec2_dict, ec2_id, ec2_launchtime, ec2_obj
示例9: set_resource
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def set_resource(self, region_name):
"""
Easy-to-mock function since moto mock of cloudwatch is giving pagination error
"""
boto3.setup_default_session(region_name = region_name)
self.cloudwatch_resource = boto3.resource('cloudwatch')
示例10: get
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def get(self):
# get cloudtrail ec2 type changes for all instances
logger.debug("Downloading cloudtrail data (from %i regions)"%len(self.region_include))
df_2 = []
import boto3
# add some spaces for aligning the progress bars
desc="Cloudtrail events in all regions"
desc = "%-50s"%desc
iter_wrap = self.region_include
iter_wrap = self.tqdmman(iter_wrap, desc=desc, total=len(self.region_include))
for region_name in iter_wrap:
boto3.setup_default_session(region_name = region_name)
df_1 = super().get()
df_1['Region'] = region_name # bugfix, field name was "region" (lower-case)
df_2.append(df_1.reset_index())
# concatenate
df_3 = pd.concat(df_2, axis=0, sort=False)
# check if empty
if df_3.shape[0]==0:
return df_3
# sort again
df_3 = df_3.set_index(["Region", "ServiceName", "ResourceName", "EventTime"]).sort_index()
return df_3
示例11: test_iterateCore_exists
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def test_iterateCore_exists(mocker):
# mock the get regions part
mockreturn = lambda service: ['us-east-1']
mockee = 'boto3.session.Session.get_available_regions'
mocker.patch(mockee, side_effect=mockreturn)
# undo some region settings from before
import boto3
boto3.setup_default_session(region_name='us-east-1')
# create mock redshift
import boto3
ec2_client = boto3.resource('ec2')
ec2_client.create_instances(
MinCount = 1,
MaxCount = 1,
InstanceType='t2.medium'
)
# test
rpi = Ec2Iterator()
rpi.region_include=['us-east-1']
x = list(rpi.iterate_core())
assert len(x) == 1
# cannot name function "test_iterator" because the filename is as such
# pytest .../test_iterator.py -k 'test_iterator' would run all tests, not just this one
示例12: test_iterateCore_exists
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def test_iterateCore_exists(mocker):
# mock the get regions part
mockreturn = lambda service: ['us-east-1']
mockee = 'boto3.session.Session.get_available_regions'
mocker.patch(mockee, side_effect=mockreturn)
# undo some region settings from before
import boto3
boto3.setup_default_session(region_name='us-east-1')
# create mock redshift
import boto3
redshift_client = boto3.client('redshift')
redshift_client.create_cluster(
ClusterIdentifier='abc',
NodeType='abc',
MasterUsername='abc',
MasterUserPassword='abc'
)
# test
rpi = RedshiftPerformanceIterator()
rpi.region_include = ['us-east-1']
x = list(rpi.iterate_core())
assert len(x) == 1
# cannot name function "test_iterator" because the filename is as such
# pytest .../test_iterator.py -k 'test_iterator' would run all tests, not just this one
示例13: test_default_session
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def test_default_session():
boto3.setup_default_session(region_name="us-east-1")
assert wr._utils.ensure_session().region_name == "us-east-1"
boto3.setup_default_session(region_name="us-east-2")
assert wr._utils.ensure_session().region_name == "us-east-2"
boto3.setup_default_session(region_name="us-west-1")
assert wr._utils.ensure_session().region_name == "us-west-1"
boto3.setup_default_session(region_name="us-west-2")
assert wr._utils.ensure_session().region_name == "us-west-2"
示例14: __init__
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def __init__(self):
self.default_wait_secs = 5
self.verbose = bool(int(os.getenv('VERBOSE', 0)))
self.profile = os.getenv('AWS_PROFILE')
try:
self.region = os.getenv('AWS_DEFAULT_REGION')
assert self.region
except:
try:
self.region = os.getenv('AWS_REGION')
assert self.region
except:
self.region = 'us-east-1'
self.session = boto3.session.Session()
boto3.setup_default_session()
if self.profile:
print(
'profile={} region={}'.format(
self.profile,
self.region
),
file=sys.stderr
)
self.session = boto3.session.Session(profile_name=self.profile)
boto3.setup_default_session(profile_name=self.profile)
示例15: main
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import setup_default_session [as 别名]
def main():
args = parse_args()
try:
boto3.setup_default_session(profile_name=args.profile, region_name=args.region)
conn = boto3.client('ec2')
except ProfileNotFound as e:
print(e)
sys.exit(1)
# Retrieve a list of instances that match the filters
reservations = conn.describe_instances(Filters=get_filters(args))
if len(reservations['Reservations']) == 0:
print('No instances matching criteria')
sys.exit(1)
instance_dns_names = [
instance['PublicDnsName']
for reservation
in reservations['Reservations']
for instance
in reservation['Instances']]
if args.all_matching_instances:
pass
else:
# Pick a random instance from the results
instance_dns_names = [instance_dns_names[random.randrange(0, len(instance_dns_names))]]
if args.command:
remote_command = ' '.join(args.command)
else:
remote_command = ''
for dns_name in instance_dns_names:
if args.ssh_user:
dns_name = '%s@%s' % (args.ssh_user, dns_name)
ssh_cmd = 'ssh %s %s %s' % (args.ssh_args, dns_name, remote_command)
os.system(ssh_cmd)