def parse_s3_url(url, bucket_name_property="Bucket", object_key_property="Key", version_property=None): if isinstance(url, six.string_types) \ and url.startswith("s3://"): # Python < 2.7.10 don't parse query parameters from URI with custom # scheme such as s3://blah/blah. As a workaround, remove scheme # altogether to trigger the parser "s3://foo/bar?v=1" =>"//foo/bar?v=1" parsed = urlparse.urlparse(url[3:]) query = urlparse.parse_qs(parsed.query) if parsed.netloc and parsed.path: result = dict() result[bucket_name_property] = parsed.netloc result[object_key_property] = parsed.path.lstrip('/') # If there is a query string that has a single versionId field, # set the object version and return if version_property is not None \ and 'versionId' in query \ and len(query['versionId']) == 1: result[version_property] = query['versionId'][0] return result raise ValueError("URL given to the parse method is not a valid S3 url " "{0}".format(url))
def assert_url_correct(self, response, expected_endpoint='sts.us-east-1.amazonaws.com', expected_signing_region='us-east-1', has_session_token=False): url = self._get_url(response) url_components = urlparse.urlparse(url) self.assertEqual(url_components.netloc, expected_endpoint) parsed_qs = urlparse.parse_qs(url_components.query) self.assertIn( expected_signing_region, parsed_qs['X-Amz-Credential'][0]) if has_session_token: self.assertEqual( [self.session_token], parsed_qs['X-Amz-Security-Token']) else: self.assertNotIn('X-Amz-Security-Token', parsed_qs) self.assertIn(self.access_key, parsed_qs['X-Amz-Credential'][0]) self.assertIn('x-k8s-aws-id', parsed_qs['X-Amz-SignedHeaders'][0])