Best Python code snippet using localstack_python
bucket_website.py
Source:bucket_website.py
...28error_file = 'error.html'29# 以ä¸ä»£ç å±ç¤ºåªè®¾ç½®ä¸»é¡µä¸404页é¢çéæç½ç«æ管30bucket.put_bucket_website(BucketWebsite(index_file, error_file))31# è·åwebsiteé
ç½®32result = bucket.get_bucket_website()33print('get_bucket_website without redirect:')34print('result index_file:', result.index_file)35print('result error_file:', result.error_file)36bucket.delete_bucket_website()37# 以ä¸ä»£ç å±ç¤ºéååæºçç½ç«æ管é
ç½®ï¼éç¨ä¸»å¤æ¨¡å¼æè
å¤ç«ç¹æ¨¡å¼38# 设置å¹é
è§å39include_header1= ConditionInlcudeHeader('host', 'test.oss-cn-beijing-internal.aliyuncs.com')40include_header2 = ConditionInlcudeHeader('host', 'test.oss-cn-shenzhen-internal.aliyuncs.com')41condition1 = Condition(key_prefix_equals='key1', 42 http_err_code_return_equals=404, include_header_list=[include_header1, include_header2])43condition2 = Condition(key_prefix_equals='key2', 44 http_err_code_return_equals=404, include_header_list=[include_header1, include_header2])45# 设置跳转è§åï¼46mirror_headers_set_1 = MirrorHeadersSet("myheader-key5","myheader-value5")47mirror_headers_set_2 = MirrorHeadersSet("myheader-key6","myheader-value6")48set_list = [mirror_headers_set_1, mirror_headers_set_2]49pass_list = ['myheader-key1', 'myheader-key2']50remove_list = ['myheader-key3', 'myheader-key4']51mirror_header = RedirectMirrorHeaders(pass_all=True, pass_list=pass_list, remove_list=remove_list, set_list=set_list)52# 使ç¨ä¸»å¤æºç«æ¨¡å¼, 使ç¨mirror_url_slaveï¼mirror_url_probeåæ°53redirect1 = Redirect(redirect_type=REDIRECT_TYPE_MIRROR, pass_query_string=False, mirror_url='http://www.test.com/', 54 mirror_url_slave='http://www.slave.com/', mirror_url_probe='http://www.test.com/index.html', mirror_pass_query_string=False, 55 mirror_follow_redirect=True, mirror_check_md5=True, mirror_headers=mirror_header)56# ä¸æå®å¤ç«57redirect2 = Redirect(redirect_type=REDIRECT_TYPE_MIRROR, mirror_url='http://www.test.com/',58 mirror_pass_query_string=True, mirror_follow_redirect=True, mirror_check_md5=False)59# å¯ä»¥è®¾ç½®ä¸æ¡æå¤æ¡ï¼æ¬ç¤ºä¾å±ç¤ºè®¾ç½®å¤æ¡60rule1 = RoutingRule(rule_num=1, condition=condition1, redirect=redirect1)61rule2 = RoutingRule(rule_num=2, condition=condition2, redirect=redirect2)62website_set = BucketWebsite(index_file, error_file, [rule1, rule2])63bucket.put_bucket_website(website_set)64# è·åwebsiteé
ç½®65website_get = bucket.get_bucket_website()66print('get_bucket_website mirror type:')67print('indext_file:', website_get.index_file)68print('error_file:', website_get.error_file)69print('rule sum:', len(website_get.rules))70bucket.delete_bucket_website() 71# 以ä¸ä»£ç å±ç¤ºé¿éäºCDN跳转以åå¤é¨è·³è½¬æè
å
é¨è·³è½¬ç设置72include_header1= ConditionInlcudeHeader('host', 'test.oss-cn-beijing-internal.aliyuncs.com')73include_header2 = ConditionInlcudeHeader('host', 'test.oss-cn-shenzhen-internal.aliyuncs.com')74condition1 = Condition(key_prefix_equals='key3', 75 http_err_code_return_equals=404, include_header_list=[include_header1, include_header2])76condition2 = Condition(key_prefix_equals='key4', 77 http_err_code_return_equals=404, include_header_list=[include_header1, include_header2])78condition3 = Condition(key_prefix_equals='key5',79 http_err_code_return_equals=404, include_header_list=[include_header1, include_header2])80# AliCDN 81redirect1 = Redirect(redirect_type=REDIRECT_TYPE_ALICDN, pass_query_string=True, 82 replace_key_with='${key}.suffix', proto='http', http_redirect_code=302)83# External84redirect2 = Redirect(redirect_type=REDIRECT_TYPE_EXTERNAL, pass_query_string=False, replace_key_prefix_with='abc', 85 proto='https', host_name='oss.aliyuncs.com', http_redirect_code=302)86# Internal87redirect3 = Redirect(redirect_type=REDIRECT_TYPE_INTERNAL, pass_query_string=False, replace_key_with='${key}.suffix')88# å¯ä»¥è®¾ç½®ä¸æ¡æå¤æ¡è§åï¼æ¬ç¤ºä¾å±ç¤ºè®¾ç½®å¤æ¡89rule1 = RoutingRule(rule_num=1, condition=condition1, redirect=redirect1)90rule2 = RoutingRule(rule_num=2, condition=condition2, redirect=redirect2)91rule3 = RoutingRule(rule_num=3, condition=condition3, redirect=redirect3)92website_set = BucketWebsite(index_file, error_file, [rule1, rule2, rule3])93bucket.put_bucket_website(website_set)94# è·åwebsiteé
ç½®95website_get = bucket.get_bucket_website()96print('get_bucket_website other type:')97print('indext_file:', website_get.index_file)98print('error_file:', website_get.error_file)99print('rule sum:', len(website_get.rules))100for rule in website_get.rules:101 print('rule_num:{}, redirect_type:{}'.format(rule.rule_num, rule.redirect.redirect_type))...
test_main.py
Source:test_main.py
...13 except ClientError:14 return False15def get_website_information(bucket_name):16 try:17 response = s3_client.get_bucket_website(Bucket=bucket_name)18 return True, response['RedirectAllRequestsTo']['HostName']19 except ClientError:20 return False, None21def get_encryption_information(bucket_name):22 try:23 s3_client.get_bucket_encryption(Bucket=bucket_name)24 return True25 except ClientError:26 return False27class TestingMain(unittest.TestCase):28 def test_format_two_decimal_points(self):29 self.assertEqual('123.00', format_two_decimal_points(123))30 self.assertEqual('12345.68', format_two_decimal_points(12345.6789))31 def test_get_public_access_information(self):...
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!!