Skip to content

Commit

Permalink
enh: make sha256 optional in s3.upload_file
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Mar 4, 2024
1 parent 03b6323 commit 397fdd0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.8.1
- enh: make sha256 optional in `s3.upload_file`
0.8.0
- feat: introduce `get_resource_dc_config`, `get_resource_info`, and
`s3cc.get_s3_dc_handle_basin_based`
Expand Down
9 changes: 7 additions & 2 deletions dcor_shared/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import botocore.exceptions

from .ckan import get_ckan_config_option
from .data import sha256sum


def compute_checksum(bucket_name, object_name, max_size=None):
Expand Down Expand Up @@ -367,7 +368,7 @@ def require_bucket(bucket_name):
return s3_bucket


def upload_file(bucket_name, object_name, path, sha256, private=True,
def upload_file(bucket_name, object_name, path, sha256=None, private=True,
override=False):
"""Upload a file to a bucket
Expand All @@ -380,7 +381,8 @@ def upload_file(bucket_name, object_name, path, sha256, private=True,
path: str or pathlib.Path
Local path of the file to be uploaded
sha256: str
SHA256 checksum of the file to be uploaded
SHA256 checksum of the file to be uploaded, will be computed
if not provided
private: bool
Whether the object should remain private. If set to False,
a tag "public:true" is added to the object which is picket up
Expand All @@ -393,6 +395,9 @@ def upload_file(bucket_name, object_name, path, sha256, private=True,
s3_url: str
URL to the S3 object
"""
if not sha256:
sha256 = sha256sum(path)

path_size = pathlib.Path(path).stat().st_size
s3_client, _, _ = get_s3()
s3_bucket = require_bucket(bucket_name)
Expand Down
4 changes: 2 additions & 2 deletions dcor_shared/s3cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .ckan import (
get_ckan_config_option, get_resource_dc_config, get_resource_info
)
from .data import sha256sum

from . import s3


Expand Down Expand Up @@ -246,7 +246,7 @@ def upload_artifact(
bucket_name=bucket_name,
object_name=f"{artifact}/{rid[:3]}/{rid[3:6]}/{rid[6:]}",
path=path_artifact,
sha256=sha256 or sha256sum(path_artifact),
sha256=sha256,
private=private,
override=override)
return s3_url

0 comments on commit 397fdd0

Please sign in to comment.