Skip to content

Commit

Permalink
Verify file contents upload response (#1339)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozangoktan authored Sep 12, 2023
1 parent 7961af2 commit 9794839
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [6.24.0] - 2023-09-12
### Fixed
- Bugfix for `FilesAPI.upload` and `FilesAPI.upload_bytes` not raising an error on file contents upload failure. Now `CogniteFileUploadError` is raised based on upload response.

## [6.23.0] - 2023-09-08
### Added
- Supporting for deleting constraints and indexes on containers.
Expand Down
9 changes: 8 additions & 1 deletion cognite/client/_api/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
LabelFilter,
TimestampRange,
)
from cognite.client.exceptions import CogniteFileUploadError
from cognite.client.utils._auxiliary import find_duplicates
from cognite.client.utils._identifier import Identifier, IdentifierSequence
from cognite.client.utils._validation import process_asset_subtree_ids, process_data_set_ids
Expand Down Expand Up @@ -570,9 +571,15 @@ def upload_bytes(
returned_file_metadata = res.json()
upload_url = returned_file_metadata["uploadUrl"]
headers = {"Content-Type": file_metadata.mime_type}
self._http_client_with_retry.request(
upload_response = self._http_client_with_retry.request(
"PUT", upload_url, data=content, timeout=self._config.file_transfer_timeout, headers=headers
)
if not upload_response.ok:
raise CogniteFileUploadError(
message=upload_response.text,
code=upload_response.status_code,
)

return FileMetadata._load(returned_file_metadata)

def retrieve_download_urls(
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "6.23.0"
__version__ = "6.24.0"
__api_subversion__ = "V20220125"
13 changes: 13 additions & 0 deletions cognite/client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ class CogniteReadTimeout(CogniteException):
pass


class CogniteFileUploadError(CogniteException):
def __init__(
self,
message: str,
code: int,
) -> None:
self.message = message
self.code = code

def __str__(self) -> str:
return f"{self.message} | code: {self.code}"


class CogniteMultiException(CogniteException):
def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "cognite-sdk"

version = "6.23.0"
version = "6.24.0"

description = "Cognite Python SDK"
readme = "README.md"
Expand Down

0 comments on commit 9794839

Please sign in to comment.