Skip to content

Commit

Permalink
Fix file store
Browse files Browse the repository at this point in the history
  • Loading branch information
tofarr committed Jan 24, 2025
1 parent f2b2093 commit f0eff02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
33 changes: 13 additions & 20 deletions openhands/storage/google_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,19 @@ def list(self, path: str) -> List[str]:
return list(blobs)

def delete(self, path: str) -> None:
# Sanitize path
if not path or path == '/':
path = ''
elif not path.endswith('/'):
# Try to delete as a single file first
try:
blob = self.bucket.blob(path)
try:
blob.delete()
return
except NotFound:
# If not found, try as a directory by appending /
path += '/'
except NotFound:
path += '/'
if path.endswith('/'):
path = path[:-1]

# Delete all blobs with the given path prefix (directory deletion)
blobs = self.bucket.list_blobs(prefix=path)
for blob in blobs:
try:
blob.delete()
except NotFound:
# Skip if blob was already deleted
continue
# Try to delete any child resources (Assume the path is a directory)
for blob in self.bucket.list_blobs(prefix=f'{path}/'):
blob.delete()

# Next try to delete item as a file
try:
blob = self.bucket.blob(path)
blob.delete()
except NotFound:
pass
26 changes: 12 additions & 14 deletions openhands/storage/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,23 @@ def list(self, path: str) -> list[str]:
return list(results)

def delete(self, path: str) -> None:
if not path or path == '/':
path = ''
try:
# First try to delete as a single file
if not path.endswith('/'):
try:
self.client.delete_object(Bucket=self.bucket, Key=path)
return
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] != 'NoSuchKey':
raise
# If not found as a file, try as a directory
path = f'{path}/'
# Sanitize path
if not path or path == '/':
path = ''
if path.endswith('/'):
path = path[:-1]

# Delete all objects with this prefix (directory deletion)
response = self.client.list_objects_v2(Bucket=self.bucket, Prefix=path)
# Try to delete any child resources (Assume the path is a directory)
response = self.client.list_objects_v2(
Bucket=self.bucket, Prefix=f'{path}/'
)
for content in response.get('Contents') or []:
self.client.delete_object(Bucket=self.bucket, Key=content['Key'])

# Next try to delete item as a file
self.client.delete_object(Bucket=self.bucket, Key=path)

except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'NoSuchBucket':
raise FileNotFoundError(
Expand Down
12 changes: 1 addition & 11 deletions tests/unit/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,7 @@ def delete_object(self, Bucket: str, Key: str) -> None:
},
'DeleteObject',
)
if Key not in self.objects_by_bucket[Bucket]:
raise botocore.exceptions.ClientError(
{
'Error': {
'Code': 'NoSuchKey',
'Message': f"The specified key '{Key}' does not exist",
}
},
'DeleteObject',
)
del self.objects_by_bucket[Bucket][Key]
self.objects_by_bucket[Bucket].pop(Key, None)


@dataclass
Expand Down

0 comments on commit f0eff02

Please sign in to comment.