Skip to content

Commit a202fa3

Browse files
bbtfrliyang
andauthored
fix empty s3 file name issue (#536)
Co-authored-by: liyang <liyang@msh.team>
1 parent 121282d commit a202fa3

2 files changed

Lines changed: 21 additions & 25 deletions

File tree

megfile/cli.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,16 @@ def cli(debug, log_level):
7070
"""
7171
options["debug"] = debug
7272
options["log_level"] = log_level or ("DEBUG" if debug else "INFO")
73+
if not debug:
74+
signal.signal(signal.SIGINT, signal.SIG_DFL)
7375
set_log_level(options["log_level"])
7476

7577

7678
def safe_cli(): # pragma: no cover
77-
debug = options.get("debug", False)
78-
if not debug:
79-
signal.signal(signal.SIGINT, signal.SIG_DFL)
8079
try:
8180
cli()
8281
except Exception as e:
83-
if debug:
82+
if options.get("debug", False):
8483
raise
8584
else:
8685
click.echo(f"\n[{type(e).__name__}] {e}", err=True)

megfile/s3_path.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,8 @@ def create_generator(_s3_pathname) -> Iterator[FileEntry]:
605605
for content in resp.get("Contents", []):
606606
path = s3_path_join(f"{protocol}://", bucket, content["Key"])
607607
if not search_dir and pattern.match(path):
608+
if path.endswith("/"):
609+
continue
608610
yield FileEntry(S3Path(path).name, path, _make_stat(content))
609611
dirname = os.path.dirname(path)
610612
while dirname not in dirnames and dirname != top_dir:
@@ -1960,6 +1962,7 @@ def create_generator() -> Iterator[FileEntry]:
19601962
)
19611963

19621964
prefix = _become_prefix(key)
1965+
protocol = self._protocol_with_profile
19631966
client = self._client
19641967

19651968
def suppress_error_callback(e):
@@ -1970,13 +1973,13 @@ def suppress_error_callback(e):
19701973
with raise_s3_error(self.path_with_protocol, suppress_error_callback):
19711974
for resp in _list_objects_recursive(client, bucket, prefix):
19721975
for content in resp.get("Contents", []):
1973-
full_path = s3_path_join(
1974-
f"{self._protocol_with_profile}://", bucket, content["Key"]
1975-
)
1976+
if content["Key"].endswith("/"):
1977+
continue
1978+
path = s3_path_join(f"{protocol}://", bucket, content["Key"])
19761979

19771980
if followlinks:
19781981
try:
1979-
origin_path = self.from_path(full_path).readlink()
1982+
origin_path = self.from_path(path).readlink()
19801983
yield FileEntry(
19811984
origin_path.name,
19821985
origin_path.path_with_protocol,
@@ -1986,9 +1989,7 @@ def suppress_error_callback(e):
19861989
except S3NotALinkError:
19871990
pass
19881991

1989-
yield FileEntry(
1990-
S3Path(full_path).name, full_path, _make_stat(content)
1991-
)
1992+
yield FileEntry(S3Path(path).name, path, _make_stat(content))
19921993

19931994
return _create_missing_ok_generator(
19941995
create_generator(),
@@ -2016,17 +2017,15 @@ def scandir(self) -> ContextIterator:
20162017
# we need to wrap the iterator in another function
20172018
def create_generator() -> Iterator[FileEntry]:
20182019
prefix = _become_prefix(key)
2020+
protocol = self._protocol_with_profile
20192021
client = self._client
20202022

2021-
def generate_s3_path(protocol: str, bucket: str, key: str) -> str:
2022-
return "%s://%s/%s" % (protocol, bucket, key)
2023-
20242023
if not bucket and not key: # list buckets
20252024
response = client.list_buckets()
20262025
for content in response["Buckets"]:
20272026
yield FileEntry(
20282027
content["Name"],
2029-
f"{self._protocol_with_profile}://{content['Name']}",
2028+
f"{protocol}://{content['Name']}",
20302029
StatResult(
20312030
ctime=content["CreationDate"].timestamp(),
20322031
isdir=True,
@@ -2039,21 +2038,17 @@ def generate_s3_path(protocol: str, bucket: str, key: str) -> str:
20392038
for common_prefix in resp.get("CommonPrefixes", []):
20402039
yield FileEntry(
20412040
common_prefix["Prefix"][len(prefix) : -1],
2042-
generate_s3_path(
2043-
self._protocol_with_profile,
2044-
bucket,
2045-
common_prefix["Prefix"],
2046-
),
2041+
f"{protocol}://{bucket}/{common_prefix['Prefix']}",
20472042
StatResult(isdir=True, extra=common_prefix),
20482043
)
20492044
for content in resp.get("Contents", []):
2050-
src_url = generate_s3_path(
2051-
self._protocol_with_profile, bucket, content["Key"]
2052-
)
2045+
if content["Key"].endswith("/"):
2046+
continue
2047+
path = f"{protocol}://{bucket}/{content['Key']}"
20532048
yield FileEntry( # pytype: disable=wrong-arg-types
20542049
content["Key"][len(prefix) :],
2055-
src_url,
2056-
_make_stat_without_metadata(content, self.from_path(src_url)),
2050+
path,
2051+
_make_stat_without_metadata(content, self.from_path(path)),
20572052
)
20582053

20592054
def missing_ok_generator():
@@ -2220,6 +2215,8 @@ def walk(
22202215
for common_prefix in resp.get("CommonPrefixes", []):
22212216
dirs.append(common_prefix["Prefix"][:-1])
22222217
for content in resp.get("Contents", []):
2218+
if content["Key"].endswith("/"):
2219+
continue
22232220
files.append(content["Key"])
22242221

22252222
dirs = sorted(dirs)

0 commit comments

Comments
 (0)