Skip to content
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

Defer S3 Client instantiation until needed #2079

Merged
merged 3 commits into from
Apr 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions luigi/contrib/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,35 @@ class S3Client(FileSystem):
boto-powered S3 client.
"""

_s3 = None

def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
**kwargs):
from boto.s3.key import Key
options = self._get_s3_config()
options.update(kwargs)
if aws_access_key_id:
options['aws_access_key_id'] = aws_access_key_id
if aws_secret_access_key:
options['aws_secret_access_key'] = aws_secret_access_key

self.Key = Key
self._options = options

@property
def s3(self):
# only import boto when needed to allow top-lvl s3 module import
import boto
import boto.s3.connection
from boto.s3.key import Key

options = self._get_s3_config()
options.update(kwargs)
options = dict(self._options)

if self._s3:
return self._s3

aws_access_key_id = options.get('aws_access_key_id')
aws_secret_access_key = options.get('aws_secret_access_key')

# Removing key args would break backwards compability
role_arn = options.get('aws_role_arn')
role_session_name = options.get('aws_role_session_name')
Expand All @@ -97,22 +117,18 @@ def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
aws_access_key_id = assumed_role.credentials.access_key
aws_session_token = assumed_role.credentials.session_token

else:
if not aws_access_key_id:
aws_access_key_id = options.get('aws_access_key_id')

if not aws_secret_access_key:
aws_secret_access_key = options.get('aws_secret_access_key')

for key in ['aws_access_key_id', 'aws_secret_access_key', 'aws_role_session_name', 'aws_role_arn']:
if key in options:
options.pop(key)

self.s3 = boto.s3.connection.S3Connection(aws_access_key_id,
aws_secret_access_key,
security_token=aws_session_token,
**options)
self.Key = Key
self._s3 = boto.s3.connection.S3Connection(aws_access_key_id,
aws_secret_access_key,
security_token=aws_session_token,
**options)
return self._s3

@s3.setter
def s3(self, value):
self._s3 = value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can as well remove the setter, since the s3 member is not assigned to anywhere in the code (as far as I can see). With this change, I am okay with the changes, but I am not a core developer and not using the s3 stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to leave the setter in because it makes it easier to test out / mock interactions here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that is fine with me. Personally I keep my good-to-have-in-the-future code in my local repo only.


def exists(self, path):
"""
Expand Down