-
Notifications
You must be signed in to change notification settings - Fork 31
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
MM-60769: load from s3 #1629
Merged
Merged
MM-60769: load from s3 #1629
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import os | ||
|
||
import boto3 | ||
import pytest | ||
|
||
# Safeguard for moto tests | ||
os.environ['AWS_ACCESS_KEY_ID'] = 'test-key' | ||
os.environ['AWS_SECRET_ACCESS_KEY'] = 'test-secret' | ||
|
||
|
||
@pytest.fixture | ||
def s3_boto(): | ||
"""Create an S3 boto3 client and return the client object""" | ||
return boto3.client('s3', region_name='us-east-1') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from moto import mock_aws | ||
|
||
from utils.packets.aws import bucket_exists, object_iter | ||
|
||
|
||
@mock_aws | ||
def test_bucket_exists(s3_boto): | ||
# GIVEN: 2 buckets exist | ||
s3_boto.create_bucket(Bucket='test-bucket-1') | ||
s3_boto.create_bucket(Bucket='test-bucket-2') | ||
|
||
# THEN: expect existing buckets to be found | ||
assert bucket_exists('test-bucket-1') is True | ||
assert bucket_exists('test-bucket-2') is True | ||
|
||
# THEN: expect non-existing bucket to be not found | ||
assert bucket_exists('test-bucket-1-1') is False | ||
assert bucket_exists('not-exists') is False | ||
|
||
|
||
@mock_aws | ||
def test_object_iter_with_prefix(s3_boto): | ||
# GIVEN: a bucket with 3 objects exists | ||
bucket_name = 'bucket-with-files' | ||
s3_boto.create_bucket(Bucket=bucket_name) | ||
s3_boto.put_object(Bucket=bucket_name, Key='prefix/2024/09/05/Mattermost/1.txt', Body=b'1') | ||
s3_boto.put_object(Bucket=bucket_name, Key='prefix/2024/10/04/Mattermost/1.txt', Body=b'2') | ||
s3_boto.put_object(Bucket=bucket_name, Key='prefix/2024/10/04/Mattermost/2.txt', Body=b'3') | ||
|
||
# WHEN: iterating over objects with prefix | ||
objects = list(object_iter(bucket_name, 'prefix/2024/10')) | ||
|
||
# THEN: expect two objects to have been loaded | ||
assert len(objects) == 2 | ||
assert objects[0][0] == 'prefix/2024/10/04/Mattermost/1.txt' | ||
assert objects[0][1].read() == b'2' | ||
|
||
assert objects[1][0] == 'prefix/2024/10/04/Mattermost/2.txt' | ||
assert objects[1][1].read() == b'3' | ||
|
||
|
||
@mock_aws | ||
def test_object_iter_without_prefix(s3_boto): | ||
# GIVEN: a bucket with 3 objects exists | ||
bucket_name = 'bucket-with-files' | ||
s3_boto.create_bucket(Bucket=bucket_name) | ||
s3_boto.put_object(Bucket=bucket_name, Key='prefix/2024/09/05/Mattermost/1.txt', Body=b'1') | ||
s3_boto.put_object(Bucket=bucket_name, Key='prefix/2024/10/04/Mattermost/1.txt', Body=b'2') | ||
s3_boto.put_object(Bucket=bucket_name, Key='prefix/2024/10/04/Mattermost/2.txt', Body=b'3') | ||
|
||
# WHEN: iterating over objects with prefix | ||
objects = list(object_iter(bucket_name, '')) | ||
|
||
# THEN: expect two objects to have been loaded | ||
assert len(objects) == 3 | ||
assert objects[0][0] == 'prefix/2024/09/05/Mattermost/1.txt' | ||
assert objects[0][1].read() == b'1' | ||
|
||
assert objects[1][0] == 'prefix/2024/10/04/Mattermost/1.txt' | ||
assert objects[1][1].read() == b'2' | ||
|
||
assert objects[2][0] == 'prefix/2024/10/04/Mattermost/2.txt' | ||
assert objects[2][1].read() == b'3' |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from io import BytesIO | ||
from typing import Generator, Tuple | ||
|
||
import boto3 | ||
from botocore.exceptions import ClientError | ||
|
||
s3 = boto3.client('s3') | ||
|
||
|
||
def bucket_exists(bucket: str) -> bool: | ||
""" | ||
Check if a bucket exists. | ||
""" | ||
try: | ||
s3.head_bucket(Bucket=bucket) | ||
return True | ||
except ClientError: | ||
return False | ||
|
||
|
||
def object_iter(bucket: str, prefix: str) -> Generator[Tuple[str, BytesIO], None, None]: | ||
""" | ||
Iterates over all items from a bucket with a given prefix, return . | ||
|
||
:bucket: The bucket to search in. | ||
:prefix: The prefix to search for. | ||
""" | ||
paginator = s3.get_paginator('list_objects_v2') | ||
|
||
# Iterate | ||
for result in paginator.paginate(Bucket=bucket, Prefix=prefix): | ||
# Download each file individually | ||
for key in result.get('Contents', []): | ||
yield key['Key'], BytesIO(s3.get_object(Bucket=bucket, Key=key['Key'])["Body"].read()) |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume there is no need to add tests for
ingest_support_packets_from_s3
(only the table name changes).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's calling the same function as
ingest_surveys_from_s3
. The individual components have been tested. We can add an end-to-end test if you prefer. However, since there might be changes depending on the structure of the S3 bucket, I prefer to wait for them before adding the extra data.