Repository navigation
Add example instructions on using Lambda - #27
Closed
chriskuehl wants to merge 1 commit into
Closed
chriskuehl wants to merge 1 commit into
chriskuehl wants to merge 1 commit into
Conversation
asottile
reviewed
Jun 9, 2018
| ) | ||
|
|
||
|
|
||
| def _sync_bucket(localdir, bucket_name): |
Collaborator
There was a problem hiding this comment.
I was looking at how aws s3 sync works and it basically does a (mtime, size) comparison against the remote objects -- going to try and implement something like that as well :)
Collaborator
|
Also probably want to note something about debouncing here. Uploading 100 packages causes the lambda to fire 100 times (I've noticed that for anything above ~100 packages ~25% of the invocations fail due to list bucket rate limiting) Some ideas for debouncing:
|
Collaborator
|
The upload timestamp check worked for me! then code that's roughly like this: def _iso_8601(dt: datetime.datetime) -> str:
return dt.isoformat() + 'Z'
def _parse_dt(s: str) -> datetime.datetime:
return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%fZ')
def _get_sync_dt() -> datetime.datetime:
s3 = boto3.client('s3')
try:
resp = s3.get_object(Bucket=SYNC_KEY.bucket, Key=SYNC_KEY.prefix)
except ClientError:
return datetime.datetime.min
else:
return _parse_dt(resp['Body'].read().decode().strip())
def _set_sync_dt(dt: datetime.datetime) -> None:
now = _iso_8601(dt).encode()
s3 = boto3.client('s3')
s3.put_object(
Body=now, Bucket=SYNC_KEY.bucket, Key=SYNC_KEY.prefix,
ServerSideEncryption='AES256',
)
def main(event: Dict[str, Any], context: Any) -> int:
event_dt = _parse_dt(event['Records'][0]['eventTime'])
sync_dt = _get_sync_dt()
now_dt = datetime.datetime.utcnow()
if sync_dt > event_dt:
print(f'skipping (synced at {sync_dt}, event at {event_dt})')
return 0
# (snip...) creation and sync here
_set_sync_dt(now_dt)
return 0
# Strictly for testing
if __name__ == '__main__':
now = _iso_8601(datetime.datetime.utcnow())
exit(main({'Records': [{'eventTime': now}]}, None)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #23
Still a little WIP, but I think most of the content is here.
I intentionally kept the lambda bits in a separate directory not distributed with the main package, as I don't want dumb-pypi to grow a dependency on boto or anything like that.
The code here is pretty simple... most of the complication is configuring all the permissions for AWS.
Testing this out, it is pretty cool to drop a package into the bucket and a couple seconds later see the updated index :)