def test_get_contents_from_url_returns_string_content(self, resource_mock): body_mock = Mock(spec=StreamingBody) body_mock.read.return_value = b'Foo' resource_mock.return_value.Object.return_value.get.return_value = { "Body": body_mock } result = S3().get_contents_from_url('s3://my-bucket/my/key/file.json') self.assertEqual("Foo", result)
def _s3_get_file(url): """ Load file from s3 :param url: str :return: str(utf-8) """ try: return S3().get_contents_from_url(url) except Exception as e: raise CfnSphereException("Could not load file from {0}: {1}".format(url, e))
def _s3_get_file(url): s3 = S3() try: if url.lower().endswith(".json"): return json.loads(s3.get_contents_from_url(url)) if url.lower().endswith(".yml") or url.lower().endswith(".yaml"): return yaml.load(s3.get_contents_from_url(url)) raise TemplateErrorException( "{0} has an unknown file type. Please provide an url with [.json|.yml|.yaml] extension" ) except Exception as e: raise TemplateErrorException( "Could not load file from {0}: {1}".format(url, e))
def test_parse_url_properly_parses_s3_url(self): (protocol, bucket_name, key_name) = S3._parse_url('s3://my-bucket/my/key/file.json') self.assertEqual('s3', protocol) self.assertEqual('my-bucket', bucket_name) self.assertEqual('my/key/file.json', key_name)