Best Python code snippet using localstack_python
s3_listener.py
Source:s3_listener.py
...924 @staticmethod925 def is_s3_copy_request(headers, path):926 return "x-amz-copy-source" in headers or "x-amz-copy-source" in path927 @staticmethod928 def is_create_multipart_request(query):929 return query.startswith("uploads")930 @staticmethod931 def is_multipart_upload(query):932 return query.startswith("uploadId")933 @staticmethod934 def get_201_response(key, bucket_name):935 return """936 <PostResponse>937 <Location>{protocol}://{host}/{encoded_key}</Location>938 <Bucket>{bucket}</Bucket>939 <Key>{key}</Key>940 <ETag>{etag}</ETag>941 </PostResponse>942 """.format(943 protocol=get_service_protocol(),944 host=config.HOSTNAME_EXTERNAL,945 encoded_key=urlparse.quote(key, safe=""),946 key=key,947 bucket=bucket_name,948 etag="d41d8cd98f00b204e9800998ecf8427f",949 )950 @staticmethod951 def _update_location(content, bucket_name):952 bucket_name = normalize_bucket_name(bucket_name)953 host = config.HOSTNAME_EXTERNAL954 if ":" not in host:955 host = "%s:%s" % (host, config.PORT_S3)956 return re.sub(957 r"<Location>\s*([a-zA-Z0-9\-]+)://[^/]+/([^<]+)\s*</Location>",958 r"<Location>%s://%s/%s/\2</Location>" % (get_service_protocol(), host, bucket_name),959 content,960 flags=re.MULTILINE,961 )962 @staticmethod963 def is_query_allowable(method, query):964 # Generally if there is a query (some/path/with?query) we don't want to send notifications965 if not query:966 return True967 # Except we do want to notify on multipart and presigned url upload completion968 contains_cred = "X-Amz-Credential" in query and "X-Amz-Signature" in query969 contains_key = "AWSAccessKeyId" in query and "Signature" in query970 # nodejs sdk putObjectCommand is adding x-id=putobject in the query971 allowed_query = "x-id=" in query.lower()972 if (973 (method == "POST" and query.startswith("uploadId"))974 or contains_cred975 or contains_key976 or allowed_query977 ):978 return True979 @staticmethod980 def parse_policy_expiration_date(expiration_string):981 try:982 dt = datetime.datetime.strptime(expiration_string, POLICY_EXPIRATION_FORMAT1)983 except Exception:984 dt = datetime.datetime.strptime(expiration_string, POLICY_EXPIRATION_FORMAT2)985 # both date formats assume a UTC timezone ('Z' suffix), but it's not parsed as tzinfo into the datetime object986 dt = dt.replace(tzinfo=datetime.timezone.utc)987 return dt988 def forward_request(self, method, path, data, headers):989 # Create list of query parameteres from the url990 parsed = urlparse.urlparse("{}{}".format(config.get_edge_url(), path))991 query_params = parse_qs(parsed.query)992 path_orig = path993 path = path.replace(994 "#", "%23"995 ) # support key names containing hashes (e.g., required by Amplify)996 # extracting bucket name from the request997 parsed_path = urlparse.urlparse(path)998 bucket_name = extract_bucket_name(headers, parsed_path.path)999 if method == "PUT" and bucket_name and not re.match(BUCKET_NAME_REGEX, bucket_name):1000 if len(parsed_path.path) <= 1:1001 return error_response(1002 "Unable to extract valid bucket name. Please ensure that your AWS SDK is "1003 + "configured to use path style addressing, or send a valid "1004 + '<Bucket>.s3.localhost.localstack.cloud "Host" header',1005 "InvalidBucketName",1006 status_code=400,1007 )1008 return error_response(1009 "The specified bucket is not valid.",1010 "InvalidBucketName",1011 status_code=400,1012 )1013 # Detecting pre-sign url and checking signature1014 if any([p in query_params for p in SIGNATURE_V2_PARAMS]) or any(1015 [p in query_params for p in SIGNATURE_V4_PARAMS]1016 ):1017 response = authenticate_presign_url(1018 method=method, path=path, data=data, headers=headers1019 )1020 if response is not None:1021 return response1022 # handling s3 website hosting requests1023 if is_static_website(headers) and method == "GET":1024 return serve_static_website(headers=headers, path=path, bucket_name=bucket_name)1025 # check content md5 hash integrity if not a copy request or multipart initialization1026 if (1027 "Content-MD5" in headers1028 and not self.is_s3_copy_request(headers, path)1029 and not self.is_create_multipart_request(parsed_path.query)1030 ):1031 response = check_content_md5(data, headers)1032 if response is not None:1033 return response1034 modified_data = None1035 # TODO: For some reason, moto doesn't allow us to put a location constraint on us-east-11036 to_find1 = to_bytes("<LocationConstraint>us-east-1</LocationConstraint>")1037 to_find2 = to_bytes("<CreateBucketConfiguration")1038 if data and data.startswith(to_bytes("<")) and to_find1 in data and to_find2 in data:1039 # Note: with the latest version, <CreateBucketConfiguration> must either1040 # contain a valid <LocationConstraint>, or not be present at all in the body.1041 modified_data = b""1042 # If this request contains streaming v4 authentication signatures, strip them from the message1043 # Related isse: https://github.com/localstack/localstack/issues/98...
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!!