How to use put_bucket_website method in localstack

Best Python code snippet using localstack_python

test_s3.py

Source:test_s3.py Github

copy

Full Screen

1import gzip2from unittest import TestCase3from unittest.mock import call4from botocore.exceptions import EndpointConnectionError, ClientError5import click6import pytest7from lily_delivery.dependencies import S38class S3TestCase(TestCase):9 @pytest.fixture(autouse=True)10 def initfixtures(self, mocker, tmpdir):11 self.mocker = mocker12 self.tmpdir = tmpdir13 def setUp(self):14 self.s3 = S3(15 access_key_id='3489348',16 secret_access_key='382938',17 region_name='east-eu',18 bucket_name='my_bucket')19 #20 # IS_VALID21 #22 def test_is_valid(self):23 self.mocker.patch.object(24 self.s3.client,25 'list_objects'26 ).return_value = {'ResponseMetadata': {'HTTPStatusCode': 200}}27 assert self.s3.is_valid() is True28 def test_is_valid__invalid(self):29 self.mocker.patch.object(30 self.s3.client,31 'list_objects'32 ).side_effect = [33 ClientError(34 operation_name='list_objects',35 error_response={'ResponseMetadata': {'HTTPStatusCode': 403}}),36 ClientError(37 operation_name='list_objects',38 error_response={'ResponseMetadata': {'HTTPStatusCode': 404}}),39 ]40 assert self.s3.is_valid() is False41 assert self.s3.is_valid() is False42 def test_is_valid__client_or_connection_problem(self):43 self.mocker.patch.object(44 self.s3.client,45 'list_objects'46 ).side_effect = [47 ClientError(48 operation_name='list_objects',49 error_response={'ResponseMetadata': {'HTTPStatusCode': 500}}),50 EndpointConnectionError(endpoint_url='/some/url'),51 ]52 with pytest.raises(click.ClickException) as e:53 self.s3.is_valid()54 assert e.value.message == 'faced problems when connecting to AWS S3'55 with pytest.raises(click.ClickException) as e:56 self.s3.is_valid()57 assert e.value.message == 'could not connect to the bucket specified'58 #59 # CHECK_IF_KEY_EXISTS60 #61 def test_check_if_key_exists__exists(self):62 get_object = self.mocker.patch.object(self.s3.client, 'get_object')63 get_object.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200}}64 assert self.s3.check_if_key_exists('index.html') is True65 assert get_object.call_args_list == [66 call(Bucket='my_bucket', Key='index.html'),67 ]68 def test_check_if_key_exists__does_not_exist(self):69 self.mocker.patch.object(70 self.s3.client,71 'get_object'72 ).side_effect = ClientError(73 operation_name='GET_OBJECT',74 error_response={'ResponseMetadata': {'HTTPStatusCode': 404}})75 assert self.s3.check_if_key_exists('index.html') is False76 def test_check_if_key_exists__connection_problem(self):77 self.mocker.patch.object(78 self.s3.client,79 'get_object'80 ).side_effect = [81 ClientError(82 operation_name='GET_OBJECT',83 error_response={'ResponseMetadata': {'HTTPStatusCode': 403}}),84 EndpointConnectionError(endpoint_url='/some/url'),85 ]86 with pytest.raises(click.ClickException) as e:87 self.s3.check_if_key_exists('index.html')88 assert e.value.message == 'faced problems when connecting to AWS S3'89 with pytest.raises(click.ClickException) as e:90 self.s3.check_if_key_exists('index.html')91 assert e.value.message == 'could not connect to bucket specified'92 #93 # UPLOAD_DIR94 #95 def test_upload_dir__makes_the_right_calls(self):96 put_object = self.mocker.patch.object(self.s3.client, 'put_object')97 build_dir = self.tmpdir.mkdir('build')98 build_dir.join('index.html').write('<html>')99 build_dir.join('logo.png').write('abc')100 build_dir.mkdir('assets').join('asset.gif').write('gif.it')101 self.s3.upload_dir(str(build_dir), {'cache-control': 'forever'})102 assert put_object.call_args_list == [103 call(104 ACL='public-read',105 Body=gzip.compress(b'<html>'),106 Bucket='my_bucket',107 CacheControl='forever',108 ContentEncoding='gzip',109 ContentType='text/html',110 Key='index.html'),111 call(112 ACL='public-read',113 Body=gzip.compress(b'abc'),114 Bucket='my_bucket',115 CacheControl='forever',116 ContentEncoding='gzip',117 ContentType='image/png',118 Key='logo.png'),119 call(120 ACL='public-read',121 Body=gzip.compress(b'gif.it'),122 Bucket='my_bucket',123 CacheControl='forever',124 ContentEncoding='gzip',125 ContentType='image/gif',126 Key='assets/asset.gif'),127 ]128 #129 # UPDATE_WEBSITE_INDEX130 #131 def test_update_website_index(self):132 put_bucket_website = self.mocker.patch.object(133 self.s3.client, 'put_bucket_website')134 put_bucket_website.return_value = (135 {'ResponseMetadata': {'HTTPStatusCode': 200}})136 self.s3.update_website_index('index-1.5.6.html')137 assert put_bucket_website.call_args_list == [138 call(139 Bucket='my_bucket',140 WebsiteConfiguration={141 'IndexDocument': {'Suffix': 'index-1.5.6.html'},142 }),143 ]144 def test_update_website_index__error(self):145 put_bucket_website = self.mocker.patch.object(146 self.s3.client, 'put_bucket_website')147 put_bucket_website.side_effect = ClientError(148 operation_name='put_bucket_website',149 error_response={'ResponseMetadata': {'HTTPStatusCode': 403}})150 with pytest.raises(click.ClickException) as e:151 self.s3.update_website_index('index-1.5.6.html')...

Full Screen

Full Screen

bucket_website.py

Source:bucket_website.py Github

copy

Full Screen

...9token = None # 使用临时密钥需要传入Token,默认为空,可不填10config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象11client = CosS3Client(config)12# 设置存储桶静态网站13def put_bucket_website():14 #.cssg-snippet-body-start:[put-bucket-website]15 response = client.put_bucket_website(16 Bucket='bucket',17 WebsiteConfiguration={18 'IndexDocument': {19 'Suffix': 'string'20 },21 'ErrorDocument': {22 'Key': 'string'23 },24 'RedirectAllRequestsTo': {25 'Protocol': 'http'|'https'26 },27 'RoutingRules': [28 {29 'Condition': {30 'HttpErrorCodeReturnedEquals': 'string',31 'KeyPrefixEquals': 'string'32 },33 'Redirect': {34 'HttpRedirectCode': 'string',35 'Protocol': 'http'|'https',36 'ReplaceKeyPrefixWith': 'string',37 'ReplaceKeyWith': 'string'38 }39 }40 ]41 }42 )43 44 #.cssg-snippet-body-end45# 获取存储桶静态网站46def get_bucket_website():47 #.cssg-snippet-body-start:[get-bucket-website]48 response = client.get_bucket_website(49 Bucket='examplebucket-1250000000'50 )51 52 #.cssg-snippet-body-end53# 删除存储桶静态网站54def delete_bucket_website():55 #.cssg-snippet-body-start:[delete-bucket-website]56 response = client.delete_bucket_website(57 Bucket='examplebucket-1250000000'58 )59 60 #.cssg-snippet-body-end61#.cssg-methods-pragma62# 设置存储桶静态网站63put_bucket_website()64# 获取存储桶静态网站65get_bucket_website()66# 删除存储桶静态网站67delete_bucket_website()...

Full Screen

Full Screen

sample_bucket_website.py

Source:sample_bucket_website.py Github

copy

Full Screen

...15website_configuration = {16 'IndexDocument': {'Suffix': 'myindex.html'},17 'ErrorDocument': {'Key': 'myerror.html'}18 }19if Oss.put_bucket_website(config, bucket_name, website_configuration):20 print("put bucket_website sucess!")21else:22 print("put bucket_website failed!")23#delete_bucket_website24if Oss.delete_bucket_website(config, bucket_name):25 print("delete bucket_website sucess!")26else:...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful