Skip to content

Commit

Permalink
ref: factor out s3.object_exists method
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Jan 2, 2024
1 parent 3469162 commit 474c0ad
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
0.5.5
- ref: factor out s3.object_exists method
- ref: factor out s3.compute_checksum method
0.5.4
- enh: cache presigned URLs with 10% tolerance
Expand Down
23 changes: 15 additions & 8 deletions dcor_shared/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ def make_object_public(bucket_name, object_name, missing_ok=False):
)


def object_exists(bucket_name, object_name):
"""Check whether an object exists"""
s3_client, _, _ = get_s3()
try:
s3_client.head_object(Bucket=bucket_name, Key=object_name)
except (s3_client.exceptions.NoSuchKey,
s3_client.exceptions.ClientError):
obj_exists = False
else:
obj_exists = True
return obj_exists


@functools.lru_cache()
def require_bucket(bucket_name):
"""Create an S3 bucket if it does not exist yet
Expand Down Expand Up @@ -302,14 +315,8 @@ def upload_file(bucket_name, object_name, path, sha256, private=True,

perform_upload = True
if not override:
try:
s3_client.head_object(Bucket=bucket_name, Key=object_name)
except (s3_client.exceptions.NoSuchKey,
s3_client.exceptions.ClientError):
object_exists = False
else:
object_exists = True
perform_upload = not object_exists
perform_upload = not object_exists(bucket_name=bucket_name,
object_name=object_name)

if perform_upload:
s3_bucket.upload_file(Filename=str(path),
Expand Down
21 changes: 21 additions & 0 deletions tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ def test_make_object_public_no_such_key(tmp_path):
missing_ok=False)


def test_object_exists():
path = data_path / "calibration_beads_47.rtdc"
bucket_name = f"test-circle-{uuid.uuid4()}"
rid = str(uuid.uuid4())
object_name = f"resource/{rid[:3]}/{rid[3:6]}/{rid[6:]}"
s3.upload_file(
bucket_name=bucket_name,
object_name=object_name,
path=path,
sha256=sha256sum(path),
private=True)

assert s3.object_exists(bucket_name=bucket_name,
object_name=object_name)
# sanity checks
assert not s3.object_exists(bucket_name=bucket_name,
object_name=f"peter/pan-{uuid.uuid4()}")
assert not s3.object_exists(bucket_name=f"hansgunter-{uuid.uuid4()}",
object_name=object_name)


def test_presigned_url(tmp_path):
path = data_path / "calibration_beads_47.rtdc"
bucket_name = f"test-circle-{uuid.uuid4()}"
Expand Down

0 comments on commit 474c0ad

Please sign in to comment.