Skip to content
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

s3 fake directory fix #206

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cloudpathlib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, local_cache_dir: Optional[Union[str, os.PathLike]] = None):

def __del__(self) -> None:
# make sure temp is cleaned up if we created it
if self._cache_tmp_dir is not None:
if hasattr(self, "_cache_tmp_dir") and self._cache_tmp_dir is not None:
self._cache_tmp_dir.cleanup()

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion cloudpathlib/cloudpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def __init__(self, cloud_path: Union[str, "CloudPath"], client: Optional["Client

def __del__(self):
# make sure that file handle to local path is closed
if self._handle is not None:
if hasattr(self, "_handle") and self._handle is not None:
self._handle.close()

@property
Expand Down
3 changes: 2 additions & 1 deletion cloudpathlib/s3/s3client.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ def _list_dir(self, cloud_path: S3Path, recursive=False) -> Iterable[S3Path]:

# files in the directory
for result_key in result.get("Contents", []):
yield self.CloudPath(f"s3://{cloud_path.bucket}/{result_key.get('Key')}")
if result_key.get("Size") > 0:
yield self.CloudPath(f"s3://{cloud_path.bucket}/{result_key.get('Key')}")

def _move_file(self, src: S3Path, dst: S3Path, remove_src: bool = True) -> S3Path:
# just a touch, so "REPLACE" metadata
Expand Down
1 change: 1 addition & 0 deletions tests/assets/dir_0/file0_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample data of file0_0.txt
1 change: 1 addition & 0 deletions tests/assets/dir_0/file0_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample data of file0_1.txt
1 change: 1 addition & 0 deletions tests/assets/dir_0/file0_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample data of file0_2.txt
1 change: 1 addition & 0 deletions tests/assets/dir_1/dir_1_0/file_1_0_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample data of file_1_0_0.txt
1 change: 1 addition & 0 deletions tests/assets/dir_1/file_1_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample data of file_1_0.txt
5 changes: 4 additions & 1 deletion tests/mock_clients/mock_s3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import collections
from datetime import datetime
from pathlib import Path, PurePosixPath
Expand Down Expand Up @@ -205,6 +206,8 @@ def paginate(self, Bucket=None, Prefix="", Delimiter=None):
{"Prefix": str(_.relative_to(self.root).as_posix())} for _ in page if _.is_dir()
]
files = [
{"Key": str(_.relative_to(self.root).as_posix())} for _ in page if _.is_file()
{"Key": str(_.relative_to(self.root).as_posix()), "Size": os.stat(_).st_size}
for _ in page
if _.is_file()
]
yield {"CommonPrefixes": dirs, "Contents": files}