Best Python code snippet using localstack_python
audit_s3.py
Source:audit_s3.py  
...64        try:65            return S3.get_bucket_location(Bucket=self.bucket_name())['LocationConstraint']66        except KeyError:67            return68    def get_object_lock_configuration(self):69        try:70            return S3.get_object_lock_configuration(Bucket=self.bucket_name())['ObjectLockConfiguration']['ObjectLockEnabled']71        except KeyError:72            return 'Disabled'73        except botocore.exceptions.ClientError:74            return 'Access Denied'75    def get_enforce_ssl(self):76        try:77            for statement in S3.get_bucket_policy(Bucket=self.bucket_name())['Statement']:78                if statement['Effect'] == 'Deny':79                    if statement['Condition']['Bool']['aws:SecureTransport'] == 'false':80                        return True81        except (KeyError, botocore.exceptions.ClientError) as error:82            print('get_public_access_configuration raised error: ', error)83            return False84    def get_public_access_configuration(self):85        try:86            return S3.get_public_access_block(Bucket=self.bucket_name()).get('PublicAccessBlockConfiguration', {})87        except botocore.exceptions.ClientError as error:88            print('get_public_access_configuration raised error: ', error)89            return {90                key: False for key in (91                    'BlockPublicAcls', 'BlockPublicPolicy',92                    'IgnorePublicAcls', 'RestrictPublicBuckets'93                )94            }95    def get_bucket_logging(self):96        return 'LoggingEnabled' in S3.get_bucket_logging(Bucket=self.bucket_name())97    def get_bucket_notification_configuration(self):98        result = {99            'LambdaFunctionNotifications': None,100            'SQSQueueNotifications': None,101            'SNSTopicNotifications': None,102            'EventBridgeNotifications': None,103        }104        try:105            for key, configuration in S3.get_bucket_notification_configuration(Bucket=self.bucket_name()).items():106                if key == 'TopicConfigurations':107                    result['SNSTopicNotifications'] = ','.join(topic['TopicArn'] for topic in configuration)108                if key == 'LambdaFunctionConfigurations':109                    result['LambdaFunctionNotifications'] = ','.join(function['LambdaFunctionArn'] for function in configuration)110                if key == 'QueueConfigurations':111                    result['SQSQueueNotifications'] = ','.join(queue['QueueArn'] for queue in configuration)112                if key == 'EventBridgeConfiguration':113                    result['EventBridgeNotifications'] = ','.join(event['EventBridgeArn'] for event in configuration)114        except (KeyError, botocore.exceptions.ClientError) as error:115            print('get_bucket_notification_configuration raised error: ', error)116        return result117    def get_tags(self):118        try:119            return {120                tag['Key']: tag['Value']121                for tag in S3.get_bucket_tagging(Bucket=self.bucket_name()).get('TagSet')122            }123        except (KeyError, TypeError):124            return {}125    def to_dict(self):126        return {127            "ResourceName": self.bucket_name(),128            "SizeInBytes" : str(self.get_size()),129            "SizeInGiB" : str(self.get_size_in_gib()),130            "LastModified" : self.get_last_modified_date(),131            "NumberOfObjects" : str(self.get_number_of_objects()),132            "Encryption": self.encryption.get('SSEAlgorithm'),133            'KmsKeyId': self.encryption.get("KMSMasterKeyID"),134            'Versioning': self.versioning.get('Status'),135            'MFADelete': self.versioning.get('MFADelete'),136            'DateAudited': str(datetime.datetime.now()),137            'Location': self.get_bucket_location(),138            'EnforceSSL': self.get_enforce_ssl(),139            'ObjectLockConfiguration': self.get_object_lock_configuration(),140            'AccessLogging': self.get_bucket_logging(),141            **self.get_bucket_notification_configuration(),142            **self.get_public_access_configuration(),143            **self.get_tags(),144        }145def now():146    return datetime.datetime.now(datetime.timezone.utc)147def region():148    return os.environ.get('AWS_REGION', 'us-east-1')149def endpoint_url(service):150    return151    return f"https://{service}.{region()}.amazonaws.com"152def create_client(service):153    return SESSION.client(...s3.py
Source:s3.py  
...20        response = s3_client.get_object_legal_hold(Bucket=bucket, Key=key, VersionId=version_id)21    else:22        response = s3_client.get_object_legal_hold(Bucket=bucket, Key=key)23    return response24def get_object_lock_configuration(s3_client, bucket):25    response = s3_client.get_object_lock_configuration(Bucket=bucket)26    return response27def get_object_retention(s3_client, bucket, key, version_id=None):28    if version_id:29        response = s3_client.get_object_retention(Bucket=bucket, Key=key, VersionId=version_id)30    else:31        response = s3_client.get_object_retention(Bucket=bucket, Key=key)32    return response33test_json_bytes = bytes(json.dumps({"test": "json"}).encode('UTF-8'))34def put_object(s3_client, bucket, key, object_bytes=test_json_bytes):35    response = s3_client.put_object(Body=object_bytes, Bucket=bucket, Key=key)36    return response37def get_object(s3_client, bucket, key, version_id=None):38    if version_id:39        response = s3_client.get_object(Bucket=bucket, Key=key, VersionId=version_id)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
