本文整理汇总了Python中boto3.resource方法的典型用法代码示例。如果您正苦于以下问题:Python boto3.resource方法的具体用法?Python boto3.resource怎么用?Python boto3.resource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto3
的用法示例。
在下文中一共展示了boto3.resource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def __init__(self, aws_access_key_id, aws_secret_access_key,
aws_region, in_queue=None, out_queue=None):
"""Set up SQS connections.
:param aws_access_key_id: AWS access key ID.
:param aws_secret_access_key: AWS secret access key.
:param aws_region: AWS region string.
:param in_queue: Optional input queue name.
:param out_queue: Optional output queue name.
"""
self.in_queue = None
self.out_queue = None
if in_queue:
resource = boto3.resource('sqs', region_name=aws_region,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key)
self.in_queue = resource.get_queue_by_name(QueueName=in_queue)
if out_queue:
client = boto3.client('sqs', region_name=aws_region,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key)
self.out_queue = client.get_queue_url(QueueName=out_queue)['QueueUrl']
示例2: audit_2_3
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def audit_2_3(self):
"""2.3 Ensure the S3 bucket CloudTrail logs to is not publicly accessible (Scored)"""
raise NotImplementedError()
import boto3
s3 = boto3.session.Session(region_name="us-east-1").resource("s3")
# s3 = boto3.resource("s3")
# for trail in self.trails:
# for grant in s3.Bucket(trail["S3BucketName"]).Acl().grants:
# print(s3.Bucket(trail["S3BucketName"]).Policy().policy)
for bucket in s3.buckets.all():
print(bucket)
try:
print(" Policy:", bucket.Policy().policy)
except Exception:
pass
for grant in bucket.Acl().grants:
try:
print(" Grant:", grant)
except Exception:
pass
示例3: assert_alarm
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def assert_alarm(self, name, pattern, remediate=False):
logs = clients.logs
sns = resources.sns
alarm_ok = False
for trail in self.trails:
log_group_name = ARN(trail["CloudWatchLogsLogGroupArn"]).resource.split(":")[1]
for metric_filter in logs.describe_metric_filters(logGroupName=log_group_name)["metricFilters"]:
if metric_filter["filterPattern"] == pattern:
for alarm in self.alarms:
try:
self.assertEqual(alarm.metric_name, metric_filter["metricTransformations"][0]["metricName"])
self.assertGreater(len(list(sns.Topic(alarm.alarm_actions[0]).subscriptions.all())), 0)
alarm_ok = True
except Exception:
pass
if remediate and not alarm_ok:
self.ensure_alarm(name=name,
pattern=pattern,
log_group_name=log_group_name)
alarm_ok = True
self.assertTrue(alarm_ok)
示例4: load_configurations
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def load_configurations(bucket_name, key_name):
logging.getLogger().debug('[load_configurations] Start')
try:
s3 = boto3.resource('s3')
file_obj = s3.Object(bucket_name, key_name)
file_content = file_obj.get()['Body'].read()
global config
config = json.loads(file_content)
except Exception as e:
logging.getLogger().error("[load_configurations] \tError to read config file")
raise e
logging.getLogger().debug('[load_configurations] End')
示例5: delete_ami
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def delete_ami(ami_id):
ensure_ami_exists(ami_id)
print('Deleting ami: {ami_id}'.format(ami_id=ami_id))
ec2 = boto3.resource('ec2')
image = ec2.Image(ami_id)
# retrieve the mappings before deregistering the image
mappings = image.block_device_mappings
print('Got these mappings: {mappings}'.format(mappings=mappings))
# first we deregister the image
image.deregister()
print('Image {ami_id} deregistered'.format(ami_id=ami_id))
snapshot_ids = [block_device_mapping['Ebs']['SnapshotId'] for block_device_mapping in mappings]
print('Got snapshots {snapshot_ids}'.format(snapshot_ids=snapshot_ids))
for snapshot_id in snapshot_ids:
ec2.Snapshot(snapshot_id).delete()
print('Deleted snaphots: {snapshot_ids}'.format(snapshot_ids=snapshot_ids))
示例6: handle_request
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def handle_request(self):
"""
Handles the custom resource request from cloudformation
:return:
"""
start = datetime.now()
self._logger.info("Cloudformation request is {}", safe_json(self._event, indent=2))
try:
result = CustomResource.handle_request(self)
return safe_dict({
"result": result,
"datetime": datetime.now().isoformat(),
"running-time": (datetime.now() - start).total_seconds()
})
except Exception as ex:
self._logger.error(ERR_HANDLING_SETUP_REQUEST, ex, full_stack())
raise ex
finally:
self._logger.flush()
示例7: _create_request
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def _create_request(self):
"""
Handles create request from cloudformation custom resource
:return:
"""
try:
self._setup()
self.physical_resource_id = self.__class__.__name__.lower()
if allow_send_metrics():
self._send_create_metrics()
return True
except Exception as ex:
self.response["Reason"] = str(ex)
return False
示例8: _delete_request
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def _delete_request(self):
"""
Handles delete request from cloudformation custom resource
:return:
"""
try:
self.delete_templates()
self.delete_external_task_config_stacks()
if allow_send_metrics():
self._send_delete_metrics()
return True
except Exception as ex:
self.response["Reason"] = str(ex)
return False
示例9: updateVersions
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def updateVersions(region="us-east-1", table="credential-store"):
'''
do a full-table scan of the credential-store,
and update the version format of every credential if it is an integer
'''
dynamodb = boto3.resource('dynamodb', region_name=region)
secrets = dynamodb.Table(table)
response = secrets.scan(ProjectionExpression="#N, version, #K, contents, hmac",
ExpressionAttributeNames={"#N": "name", "#K": "key"})
items = response["Items"]
for old_item in items:
if isInt(old_item['version']):
new_item = copy.copy(old_item)
new_item['version'] = credstash.paddedInt(new_item['version'])
if new_item['version'] != old_item['version']:
secrets.put_item(Item=new_item)
secrets.delete_item(Key={'name': old_item['name'], 'version': old_item['version']})
else:
print "Skipping item: %s, %s" % (old_item['name'], old_item['version'])
示例10: save_directory
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def save_directory(self, source_dir, target_name):
import boto3
# Stolen from:
# https://github.com/boto/boto3/issues/358#issuecomment-346093506
target_name = target_name or os.path.basename(source_dir)
all_files = []
for root, dirs, files in os.walk(source_dir):
all_files += [os.path.join(root, f) for f in files]
s3_resource = boto3.resource("s3")
for filename in all_files:
file_location = s3_join(
self.dir, target_name, os.path.relpath(filename, source_dir)
)
s3_resource.Object(self.bucket, file_location).put(
Body=open(filename, "rb")
)
示例11: _delete_lambda_role
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def _delete_lambda_role(self, name):
iam = boto3.resource('iam')
role_name = self._role_name(name)
policy_name = self._policy_name(name)
role_policy = iam.RolePolicy(role_name, policy_name)
role = iam.Role(self._role_name(name))
# HACK: This 'if thing in things.all()' biz seems like
# a very inefficient way to check for resource
# existence...
if role_policy in role.policies.all():
role_policy.delete()
if role in iam.roles.all():
role.delete()
示例12: s3_get
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def s3_get(url: str, temp_file: IO) -> None:
"""Pull a file directly from S3."""
s3_resource = boto3.resource("s3")
bucket_name, s3_path = split_s3_path(url)
s3_resource.Bucket(bucket_name).download_fileobj(s3_path, temp_file)
示例13: s3_etag
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def s3_etag(url: str) -> Optional[str]:
"""Check ETag on S3 object."""
s3_resource = boto3.resource("s3")
bucket_name, s3_path = split_s3_path(url)
s3_object = s3_resource.Object(bucket_name, s3_path)
return s3_object.e_tag
示例14: generate_app_log_parser_conf_file
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def generate_app_log_parser_conf_file(stack_name, error_threshold, block_period, app_access_log_bucket, overwrite):
logging.getLogger().debug("[generate_app_log_parser_conf_file] Start")
local_file = '/tmp/' + stack_name + '-app_log_conf_LOCAL.json'
remote_file = stack_name + '-app_log_conf.json'
default_conf = {
'general': {
'errorThreshold': error_threshold,
'blockPeriod': block_period,
'errorCodes': ['400', '401', '403', '404', '405']
},
'uriList': {
}
}
if not overwrite:
try:
s3 = boto3.resource('s3')
file_obj = s3.Object(app_access_log_bucket, remote_file)
file_content = file_obj.get()['Body'].read()
remote_conf = json.loads(file_content)
if 'general' in remote_conf and 'errorCodes' in remote_conf['general']:
default_conf['general']['errorCodes'] = remote_conf['general']['errorCodes']
if 'uriList' in remote_conf:
default_conf['uriList'] = remote_conf['uriList']
except Exception as e:
logging.getLogger().debug("[generate_app_log_parser_conf_file] \tFailed to merge existing conf file data.")
logging.getLogger().debug(e)
with open(local_file, 'w') as outfile:
json.dump(default_conf, outfile)
s3_client = boto3.client('s3')
s3_client.upload_file(local_file, app_access_log_bucket, remote_file, ExtraArgs={'ContentType': "application/json"})
logging.getLogger().debug("[generate_app_log_parser_conf_file] End")
示例15: __init__
# 需要导入模块: import boto3 [as 别名]
# 或者: from boto3 import resource [as 别名]
def __init__(self, queue_name, bucket_name, function_name, cron_schedule='rate(1 day)'):
self._queue_name = queue_name
self._bucket_name = bucket_name
self._function_name = function_name
self._cron_schedule = cron_schedule,
self._s3 = boto.resource('s3')
self._sqs = boto.resource('sqs')
self._lambda_client = boto.client('lambda')
self._iam_client = boto.client('iam')
self._queue = None
self._bucket = None