-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add dependencies for S3 access & testing * Add S3 utils * Add loading from S3 * Remove left-over comment
- Loading branch information
1 parent
32706fd
commit 7673294
Showing
8 changed files
with
575 additions
and
30 deletions.
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