Skip to content

Commit

Permalink
fix: deploy error on not supplying s3 bucket (#1534)
Browse files Browse the repository at this point in the history
  • Loading branch information
sriram-mv committed Nov 23, 2019
1 parent 31c7d35 commit f0edb60
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
11 changes: 10 additions & 1 deletion samcli/commands/package/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ class NoSuchBucketError(UserException):
def __init__(self, **kwargs):
self.kwargs = kwargs

message_fmt = "\n S3 Bucket does not exist."
message_fmt = "\nS3 Bucket does not exist."

super(NoSuchBucketError, self).__init__(message=message_fmt.format(**self.kwargs))


class BucketNotSpecifiedError(UserException):
def __init__(self, **kwargs):
self.kwargs = kwargs

message_fmt = "\nS3 Bucket not specified, use --s3-bucket to specify a bucket name"

super(BucketNotSpecifiedError, self).__init__(message=message_fmt.format(**self.kwargs))
4 changes: 3 additions & 1 deletion samcli/lib/package/s3_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from boto3.s3 import transfer

from samcli.commands.package.exceptions import NoSuchBucketError
from samcli.commands.package.exceptions import NoSuchBucketError, BucketNotSpecifiedError

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -134,6 +134,8 @@ def file_exists(self, remote_path):

try:
# Find the object that matches this ETag
if not self.bucket_name:
raise BucketNotSpecifiedError()
self.s3.head_object(Bucket=self.bucket_name, Key=remote_path)
return True
except botocore.exceptions.ClientError:
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/lib/package/test_s3_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pathlib import Path
from botocore.exceptions import ClientError

from samcli.commands.package.exceptions import NoSuchBucketError
from samcli.commands.package.exceptions import NoSuchBucketError, BucketNotSpecifiedError
from samcli.lib.package.s3_uploader import S3Uploader


Expand Down Expand Up @@ -144,6 +144,20 @@ def test_s3_upload(self):
s3_url = s3_uploader.upload(f.name, remote_path)
self.assertEqual(s3_url, "s3://{0}/{1}/{2}".format(self.bucket_name, self.prefix, remote_path))

def test_s3_upload_no_bucket(self):
s3_uploader = S3Uploader(
s3_client=self.s3,
bucket_name=None,
prefix=self.prefix,
kms_key_id=self.kms_key_id,
force_upload=self.force_upload,
)
s3_uploader.artifact_metadata = {"a": "b"}
remote_path = Path.joinpath(Path(os.getcwd()), Path("tmp"))
with self.assertRaises(BucketNotSpecifiedError):
with tempfile.NamedTemporaryFile(mode="w", delete=False) as f:
s3_uploader.upload(f.name, remote_path)

def test_s3_upload_with_dedup(self):
s3_uploader = S3Uploader(
s3_client=self.s3,
Expand Down

0 comments on commit f0edb60

Please sign in to comment.