-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feature: allow setting the default bucket in Session #1176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,13 @@ class Session(object): # pylint: disable=too-many-public-methods | |
bucket based on a naming convention which includes the current AWS account ID. | ||
""" | ||
|
||
def __init__(self, boto_session=None, sagemaker_client=None, sagemaker_runtime_client=None): | ||
def __init__( | ||
self, | ||
boto_session=None, | ||
sagemaker_client=None, | ||
sagemaker_runtime_client=None, | ||
default_bucket=None, | ||
): | ||
"""Initialize a SageMaker ``Session``. | ||
|
||
Args: | ||
|
@@ -91,15 +97,23 @@ def __init__(self, boto_session=None, sagemaker_client=None, sagemaker_runtime_c | |
``InvokeEndpoint`` calls to Amazon SageMaker (default: None). Predictors created | ||
using this ``Session`` use this client. If not provided, one will be created using | ||
this instance's ``boto_session``. | ||
default_bucket (str): The default s3 bucket to be used by this session. | ||
Ex: "sagemaker-us-west-2" | ||
|
||
""" | ||
self._default_bucket = None | ||
|
||
# currently is used for local_code in local mode | ||
self.config = None | ||
|
||
self._initialize(boto_session, sagemaker_client, sagemaker_runtime_client) | ||
self._initialize( | ||
boto_session=boto_session, | ||
sagemaker_client=sagemaker_client, | ||
sagemaker_runtime_client=sagemaker_runtime_client, | ||
default_bucket=default_bucket, | ||
) | ||
|
||
def _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client): | ||
def _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client, default_bucket): | ||
"""Initialize this SageMaker Session. | ||
|
||
Creates or uses a boto_session, sagemaker_client and sagemaker_runtime_client. | ||
|
@@ -126,6 +140,9 @@ def _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client): | |
|
||
prepend_user_agent(self.sagemaker_runtime_client) | ||
|
||
self._default_bucket = None | ||
self._desired_default_bucket_name = default_bucket | ||
knakad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self.local_mode = False | ||
|
||
@property | ||
|
@@ -314,11 +331,14 @@ def default_bucket(self): | |
if self._default_bucket: | ||
return self._default_bucket | ||
|
||
default_bucket = self._desired_default_bucket_name | ||
region = self.boto_session.region_name | ||
account = self.boto_session.client( | ||
"sts", region_name=region, endpoint_url=sts_regional_endpoint(region) | ||
).get_caller_identity()["Account"] | ||
default_bucket = "sagemaker-{}-{}".format(region, account) | ||
|
||
if not default_bucket: | ||
account = self.boto_session.client( | ||
"sts", region_name=region, endpoint_url=sts_regional_endpoint(region) | ||
).get_caller_identity()["Account"] | ||
default_bucket = "sagemaker-{}-{}".format(region, account) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd move l. 334 to be right before the if self._desired_default_bucket_name
default_bucket = self._desired_default_bucket_name
else
... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the line closer, but I like to avoid ifelses when possible. |
||
|
||
s3 = self.boto_session.resource("s3") | ||
try: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
from __future__ import absolute_import | ||
|
||
import boto3 | ||
from botocore.config import Config | ||
|
||
from sagemaker import Session | ||
|
||
DEFAULT_REGION = "us-west-2" | ||
CUSTOM_BUCKET_NAME = "this-bucket-should-not-exist" | ||
|
||
|
||
def test_sagemaker_session_does_not_create_bucket_on_init( | ||
sagemaker_client_config, sagemaker_runtime_config, boto_config | ||
): | ||
boto_session = ( | ||
boto3.Session(**boto_config) if boto_config else boto3.Session(region_name=DEFAULT_REGION) | ||
) | ||
sagemaker_client_config.setdefault("config", Config(retries=dict(max_attempts=10))) | ||
sagemaker_client = ( | ||
boto_session.client("sagemaker", **sagemaker_client_config) | ||
if sagemaker_client_config | ||
else None | ||
) | ||
runtime_client = ( | ||
boto_session.client("sagemaker-runtime", **sagemaker_runtime_config) | ||
if sagemaker_runtime_config | ||
else None | ||
) | ||
|
||
Session( | ||
boto_session=boto_session, | ||
sagemaker_client=sagemaker_client, | ||
sagemaker_runtime_client=runtime_client, | ||
default_bucket=CUSTOM_BUCKET_NAME, | ||
) | ||
|
||
s3 = boto3.resource("s3") | ||
assert s3.Bucket(CUSTOM_BUCKET_NAME).creation_date is None |
Uh oh!
There was an error while loading. Please reload this page.