Skip to content

Commit 29b2615

Browse files
author
Thierry RAMORASOAVINA
committed
Update copy_from_local and copy_to_local for S3
1 parent ea20b1b commit 29b2615

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

khiops/core/internals/filesystems.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
try:
2424
import boto3
2525
import boto3.session
26+
from boto3.exceptions import S3UploadFailedError
2627
from botocore.exceptions import ClientError
2728

2829
boto3_import_error = None
@@ -254,7 +255,7 @@ def copy_from_local(uri_or_path, local_path):
254255
Raises
255256
------
256257
RuntimeError
257-
If there was a problem when removing.
258+
If there was a problem when copying.
258259
"""
259260
create_resource(uri_or_path).copy_from_local(local_path)
260261

@@ -272,7 +273,7 @@ def copy_to_local(uri_or_path, local_path):
272273
Raises
273274
------
274275
RuntimeError
275-
If there was a problem when removing.
276+
If there was a problem when copying.
276277
"""
277278
create_resource(uri_or_path).copy_to_local(local_path)
278279

@@ -668,29 +669,22 @@ def remove(self):
668669
)
669670

670671
def copy_from_local(self, local_path):
671-
response = self.s3_client.Bucket(self.uri_info.netloc).upload_file(
672-
local_path, self.uri_info.path[1:]
673-
)
674-
status_code = response["ResponseMetadata"]["HTTPStatusCode"]
675-
copy_ok = 200 <= status_code <= 299
676-
if not copy_ok:
677-
raise RuntimeError(
678-
f"S3 copy_from_local failed {self.uri} with code {status_code}: "
679-
+ json.dumps(response)
672+
try:
673+
self.s3_client.Bucket(self.uri_info.netloc).upload_file(
674+
local_path, self.uri_info.path[1:]
680675
)
676+
# normalize the raised exception
677+
except S3UploadFailedError as exc:
678+
raise RuntimeError(f"S3 copy_from_local failed {self.uri}") from exc
681679

682680
def copy_to_local(self, local_path):
683-
response = self.s3_client.Bucket(self.uri_info.netloc).download_file(
684-
local_path, self.uri_info.path[1:]
685-
)
686-
status_code = response["ResponseMetadata"]["HTTPStatusCode"]
687-
copy_ok = 200 <= status_code <= 299
688-
if not copy_ok:
689-
raise RuntimeError(
690-
f"S3 download failed {self.uri} with code {status_code}: "
691-
+ json.dumps(response)
681+
try:
682+
self.s3_client.Bucket(self.uri_info.netloc).download_file(
683+
self.uri_info.path[1:], local_path
692684
)
693-
return copy_ok
685+
# normalize the raised exception
686+
except S3UploadFailedError as exc:
687+
raise RuntimeError(f"S3 download failed {self.uri}") from exc
694688

695689
def list_dir(self):
696690
# Add an extra slash to the path to treat it as a folder

tests/test_remote_access.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ def init_remote_bucket(cls, bucket_name=None, proto=None):
8484
f"{proto}://{bucket_name}/khiops-cicd/samples/{file}",
8585
os.path.join(kh.get_samples_dir(), file),
8686
)
87+
# symetric call to ensure the upload was OK
88+
fs.copy_to_local(
89+
f"{proto}://{bucket_name}/khiops-cicd/samples/{file}", "/tmp/dummy"
90+
)
8791

8892
def results_dir_root(self):
8993
"""To be overridden by descendants if needed

0 commit comments

Comments
 (0)