Skip to content

Commit

Permalink
add s3-endpoint-url argument to cloudformation package
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon John Lewis committed May 5, 2018
1 parent 312aebb commit 7c532ba
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
16 changes: 14 additions & 2 deletions awscli/customizations/cloudformation/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ class PackageCommand(BasicCommand):
)
},

{
'name': 's3-endpoint-url',
'help_text': (
'URL of storage service where packaged templates and artifacts'
' will be uploaded. Useful for testing and local development'
' or when uploading to a non-AWS storage service that is'
' nonetheless S3-compatible.'
)
},

{
'name': 'kms-key-id',
'help_text': (
Expand Down Expand Up @@ -117,7 +127,8 @@ def _run_main(self, parsed_args, parsed_globals):
"s3",
config=Config(signature_version='s3v4'),
region_name=parsed_globals.region,
verify=parsed_globals.verify_ssl)
verify=parsed_globals.verify_ssl,
endpoint_url=parsed_args.s3_endpoint_url)

template_path = parsed_args.template_file
if not os.path.isfile(template_path):
Expand All @@ -131,7 +142,8 @@ def _run_main(self, parsed_args, parsed_globals):
parsed_globals.region,
parsed_args.s3_prefix,
parsed_args.kms_key_id,
parsed_args.force_upload)
parsed_args.force_upload,
endpoint_url=parsed_args.s3_endpoint_url)

output_file = parsed_args.output_template_file
use_json = parsed_args.use_json
Expand Down
13 changes: 9 additions & 4 deletions awscli/customizations/s3uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ def __init__(self, s3_client,
prefix=None,
kms_key_id=None,
force_upload=False,
transfer_manager=None):
transfer_manager=None,
endpoint_url=None):
self.bucket_name = bucket_name
self.prefix = prefix
self.kms_key_id = kms_key_id or None
self.force_upload = force_upload
self.s3 = s3_client
self.region = region
self.endpoint_url = endpoint_url

self.transfer_manager = transfer_manager
if not transfer_manager:
Expand Down Expand Up @@ -177,9 +179,12 @@ def to_path_style_s3_url(self, key, version=None):
This link describes the format of Path Style URLs
http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html#access-bucket-intro
"""
base = "https://s3.amazonaws.com"
if self.region and self.region != "us-east-1":
base = "https://s3-{0}.amazonaws.com".format(self.region)
if self.endpoint_url is None:
base = "https://s3.amazonaws.com"
if self.region and self.region != "us-east-1":
base = "https://s3-{0}.amazonaws.com".format(self.region)
else:
base = self.endpoint_url

result = "{0}/{1}/{2}".format(base, self.bucket_name, key)
if version:
Expand Down
2 changes: 2 additions & 0 deletions awscli/paramfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
'cloudformation.set-stack-policy.stack-policy-url',
# aws cloudformation package --template-file
'custom.package.template-file',
# aws cloudformation package --s3-endpoint-url
'custom.package.s3-endpoint-url',
# aws cloudformation deploy --template-file
'custom.deploy.template-file',

Expand Down
1 change: 1 addition & 0 deletions tests/unit/customizations/cloudformation/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def setUp(self):
self.parsed_args = FakeArgs(template_file='./foo',
s3_bucket="s3bucket",
s3_prefix="s3prefix",
s3_endpoint_url="http://localhost",
kms_key_id="kmskeyid",
output_template_file="./oputput",
use_json=False,
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/customizations/test_s3uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,25 @@ def test_to_path_style_s3_url_other_regions(self):
result,
"https://s3-{0}.amazonaws.com/{1}/{2}".format(
region, self.bucket_name, key))

def test_to_path_style_s3_url_custom_endpoint_url(self):
key = "path/to/file"
version = "someversion"
region = "us-west-2"
endpoint_url = "http://localhost"

s3uploader = S3Uploader(
self.s3client, self.bucket_name, region, endpoint_url=endpoint_url)
result = s3uploader.to_path_style_s3_url(key, version)
self.assertEqual(
result,
"{0}/{1}/{2}?versionId={3}".format(
endpoint_url, self.bucket_name, key, version))

# Without versionId, that query parameter should be omitted
s3uploader = S3Uploader(self.s3client, self.bucket_name, region, endpoint_url=endpoint_url)
result = s3uploader.to_path_style_s3_url(key)
self.assertEqual(
result,
"{0}/{1}/{2}".format(
endpoint_url, self.bucket_name, key))

0 comments on commit 7c532ba

Please sign in to comment.