|
7 | 7 | from libcloud.storage.types import (
|
8 | 8 | ObjectError,
|
9 | 9 | ContainerDoesNotExistError,
|
10 |
| - ObjectDoesNotExistError, |
11 | 10 | InvalidContainerNameError,
|
12 | 11 | )
|
13 | 12 | from libcloud.storage.providers import get_driver
|
@@ -148,38 +147,35 @@ def upload_file(self, local_path):
|
148 | 147 | except Exception as e:
|
149 | 148 | logger.exception(f"An unexpected error occurred while uploading '{object_name}': {e}")
|
150 | 149 |
|
151 |
| - def object_exists(self, local_path): |
| 150 | + def object_exists(self, prefix): |
152 | 151 | """
|
153 |
| - Checks if an object exists in the object storage container. |
| 152 | + Checks if any object exists in the container that starts with the given prefix. |
154 | 153 |
|
155 | 154 | Parameters
|
156 | 155 | ----------
|
157 |
| - local_path : str |
158 |
| - The local file path corresponding to the object name. |
| 156 | + prefix : str |
| 157 | + The prefix to match object names against. |
159 | 158 |
|
160 | 159 | Returns
|
161 | 160 | -------
|
162 | 161 | bool
|
163 |
| - True if the object exists, False otherwise. |
| 162 | + True if at least one object with the given prefix exists, False otherwise. |
164 | 163 |
|
165 | 164 | Logs
|
166 | 165 | ----
|
167 | 166 | Logs information about the existence check or errors encountered.
|
168 | 167 | """
|
169 |
| - object_name = os.path.basename(local_path) |
170 | 168 | try:
|
171 |
| - self.driver.get_object( |
172 |
| - container_name=self._container_name, |
173 |
| - object_name=object_name |
174 |
| - ) |
175 |
| - logger.debug(f"Object '{object_name}' exists in container '{self._container_name}'.") |
176 |
| - return True |
177 |
| - except ObjectDoesNotExistError: |
178 |
| - logger.debug(f"Object '{object_name}' does not exist in container '{self._container_name}'.") |
| 169 | + objects = self.driver.list_container_objects(container=self._container_name, prefix=prefix) |
| 170 | + if objects: |
| 171 | + logger.debug(f"At least one object with prefix '{prefix}' exists in container '{self._container_name}'.") |
| 172 | + return True |
| 173 | + else: |
| 174 | + logger.debug(f"No objects with prefix '{prefix}' found in container '{self._container_name}'.") |
179 | 175 | except ContainerDoesNotExistError:
|
180 | 176 | logger.error(f"Container '{self._container_name}' does not exist in the cloud storage.")
|
181 | 177 | except InvalidContainerNameError:
|
182 | 178 | logger.error(f"Invalid container name '{self._container_name}'.")
|
183 | 179 | except Exception as e:
|
184 |
| - logger.exception(f"An unexpected error occurred while checking for object '{object_name}': {e}") |
| 180 | + logger.exception(f"An unexpected error occurred while listing objects with prefix '{prefix}': {e}") |
185 | 181 | return False
|
0 commit comments