LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
un-2316-gcs-azure-s3-fix (#1272)
Kirtiman Mishraf1f0559ac54a
diff --git a/backend/backend/exceptions.py b/backend/backend/exceptions.pyindex 1461ba550..8159771c1 100644--- a/backend/backend/exceptions.py+++ b/backend/backend/exceptions.py@@ -21,7 +21,10 @@ class UnstractBaseException(APIException):if core_err and core_err.user_message:detail = core_err.user_messageif detail and "Name or service not known" in str(detail):- detail = "Failed to establish a new connection: " "Name or service not known"+ detail = (+ "Failed to establish a new connection. \n"+ "Error: \n```\nName or service not known\n```"+ )super().__init__(detail=detail, **kwargs)self._core_err = core_errdiff --git a/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/azure_cloud_storage.py b/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/azure_cloud_storage.pyindex 973162e9f..c91e234d6 100644--- a/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/azure_cloud_storage.py+++ b/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/azure_cloud_storage.py@@ -5,7 +5,10 @@ from typing import Anyimport azure.core.exceptions as AzureExceptionfrom adlfs import AzureBlobFileSystem-from unstract.connectors.exceptions import AzureHttpError, ConnectorError+from unstract.connectors.exceptions import AzureHttpError+from unstract.connectors.filesystems.azure_cloud_storage.exceptions import (+ parse_azure_error,+)from unstract.connectors.filesystems.unstract_file_system import UnstractFileSystemfrom unstract.filesystem import FileStorageType, FileSystem@@ -21,6 +24,7 @@ class AzureCloudStorageFS(UnstractFileSystem):super().__init__("AzureCloudStorageFS")account_name = settings.get("account_name", "")access_key = settings.get("access_key", "")+ self.bucket = settings.get("bucket", "")self.azure_fs = AzureBlobFileSystem(account_name=account_name, credential=access_key)@@ -88,13 +92,13 @@ class AzureCloudStorageFS(UnstractFileSystem):def test_credentials(self) -> bool:"""To test credentials for Azure Cloud Storage."""try:- is_dir = bool(self.get_fsspec_fs().isdir(""))- if not is_dir:- raise RuntimeError("Could not access root directory.")+ self.get_fsspec_fs().info(self.bucket)except Exception as e:- raise ConnectorError(+ logger.error(f"Error from Azure Cloud Storage while testing connection: {str(e)}"- ) from e+ )+ err = parse_azure_error(e)+ raise err from ereturn Truedef upload_file_to_storage(self, source_path: str, destination_path: str) -> None:diff --git a/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.py b/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.pynew file mode 100644index 000000000..ded2f9ce0--- /dev/null+++ b/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.py@@ -0,0 +1,47 @@+import azure.core.exceptions as AzureException++from unstract.connectors.exceptions import AzureHttpError, ConnectorError+++def parse_azure_error(e: Exception) -> ConnectorError:+ """Parses the exception from Azure Cloud Storage.++ Helps parse the Azure Cloud Storage error and wraps it with our+ custom exception object to contain a user friendly message.++ Args:+ e (Exception): Error from Azure Cloud Storage++ Returns:+ ConnectorError: Unstract's ConnectorError object+ """+ if isinstance(e, ConnectorError):+ return e++ error_message = "Error from Azure Cloud Storage while testing connection. "++ if isinstance(e, AzureException.ClientAuthenticationError):+ client_error = e.message if hasattr(e, "message") else str(e)+ error_message += (+ f"Authentication failed. Please check your connection credentials. \n"+ f"Error: \n```\n{client_error}\n```"+ )+ return ConnectorError(error_message)+ elif isinstance(e, AzureException.ServiceRequestError):+ client_error = e.message if hasattr(e, "message") else str(e)+ error_message += (+ f"Failed to connect to Azure service. \n" f"Error: \n```\n{client_error}\n```"+ )+ return ConnectorError(error_message)+ elif isinstance(e, AzureException.HttpResponseError):+ client_error = e.message if hasattr(e, "message") else str(e)+ error_message += (+ f"Azure service returned an error response. \n"+ f"Error: \n```\n{client_error}\n```"+ )+ return AzureHttpError(error_message)+ else:+ error_message += (+ f"Error from Azure Cloud Storage. \n" f"Error: \n```\n{str(e)}\n```"+ )+ return ConnectorError(error_message)diff --git a/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/static/json_schema.json b/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/static/json_schema.jsonindex 07c940285..110588874 100644--- a/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/static/json_schema.json+++ b/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/static/json_schema.json@@ -4,7 +4,8 @@"required": ["connectorName","account_name",- "access_key"+ "access_key",+ "bucket"],"properties": {"connectorName": {@@ -15,15 +16,18 @@"account_name": {"type": "string","title": "Account Name",- "description": "Account name of azure cloud storage",- "default":""+ "description": "Account name of azure cloud storage"},"access_key": {"type": "string","title": "Access Key","format": "password",- "description": "Access key of the corresponding account",- "default":""+ "description": "Access key of the corresponding account"+ },+ "bucket": {+ "type": "string",+ "title": "Bucket Name",+ "description": "Name of the bucket to be restricted to."}}}diff --git a/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/google_cloud_storage.py b/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/google_cloud_storage.pyindex 1ed850fda..3f4fee644 100644--- a/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/google_cloud_storage.py+++ b/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/google_cloud_storage.py@@ -14,11 +14,27 @@ logger = logging.getLogger(__name__)class GoogleCloudStorageFS(UnstractFileSystem):def __init__(self, settings: dict[str, Any]):+ """Initializing gcs++ Args:+ settings (dict[str, Any]): A json dict containing json connection string+ Raises:+ ConnectorError: Error raised when connection initialization fails+ """super().__init__("GoogleCloudStorage")- self.bucket = settings.get("bucket", "")project_id = settings.get("project_id", "")- json_credentials = json.loads(settings.get("json_credentials", "{}"))- self.gcs_fs = GCSFileSystem(token=json_credentials, project=project_id)+ json_credentials_str = settings.get("json_credentials", "{}")+ try:+ json_credentials = json.loads(json_credentials_str)+ self.gcs_fs = GCSFileSystem(token=json_credentials, project=project_id)+ except json.JSONDecodeError as e:+ logger.error(f"Failed to parse JSON credentials: {str(e)}")+ error_msg = (+ "Failed to connect to Google Cloud Storage. \n"+ "GCS credentials are not in proper JSON format. \n"+ f"Error: \n```\n{str(e)}\n```"+ )+ raise ConnectorError(error_msg) from e@staticmethoddef get_id() -> str:@@ -79,13 +95,20 @@ class GoogleCloudStorageFS(UnstractFileSystem):return Nonedef test_credentials(self) -> bool:- """To test credentials for Google Cloud Storage."""+ """Test Google Cloud Storage credentials by accessing the root path info.++ Raises:+ ConnectorError: connector-error++ Returns:+ boolean: true if test-connection is successful+ """try:- is_dir = bool(self.get_fsspec_fs().isdir(self.bucket))- if not is_dir:- raise RuntimeError("Could not access root directory.")+ self.get_fsspec_fs().info("/")except Exception as e:- raise ConnectorError(- f"Error from Google Cloud Storage while testing connection: {str(e)}"- ) from e+ error_msg = (+ "Error from Google Cloud Storage while testing connection. \n"+ f"Error: \n```\n{str(e)}\n```"+ )+ raise ConnectorError(error_msg) from ereturn Truediff --git a/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/static/json_schema.json b/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/static/json_schema.jsonindex 0f025fd0b..88781c889 100644--- a/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/static/json_schema.json+++ b/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/static/json_schema.json@@ -23,11 +23,6 @@"type": "string","title": "Project ID","description": "Name of the project ID."- },- "bucket": {- "type": "string",- "title": "Bucket Name",- "description": "Name of the bucket to be restricted to."}}}diff --git a/unstract/connectors/src/unstract/connectors/filesystems/minio/static/json_schema.json b/unstract/connectors/src/unstract/connectors/filesystems/minio/static/json_schema.jsonindex 5dd29f68e..e357f4805 100644--- a/unstract/connectors/src/unstract/connectors/filesystems/minio/static/json_schema.json+++ b/unstract/connectors/src/unstract/connectors/filesystems/minio/static/json_schema.json@@ -6,7 +6,8 @@"connectorName","key","secret",- "endpoint_url"+ "endpoint_url",+ "region_name"],"properties": {"connectorName": {@@ -35,7 +36,8 @@"region_name": {"type": "string","title": "Region Name",- "description": "Region name of AWS S3 account."+ "default": "ap-south",+ "description": "Region of the AWS S3 account. For Minio, leave it blank"}}}