Upload a file to the API via STS (preferred) or direct upload fallback. Args: file_path: Path to the file to upload. Returns: Dict with either ``{"object": {"bucket": ..., "key": ...}}`` (STS) or ``{"file_token": ...}`` (legacy).
(self, file_path: str)
| 437 | } |
| 438 | |
| 439 | async def upload_file(self, file_path: str) -> Dict[str, Any]: |
| 440 | """ |
| 441 | Upload a file to the API via STS (preferred) or direct upload fallback. |
| 442 | |
| 443 | Args: |
| 444 | file_path: Path to the file to upload. |
| 445 | |
| 446 | Returns: |
| 447 | Dict with either ``{"object": {"bucket": ..., "key": ...}}`` (STS) |
| 448 | or ``{"file_token": ...}`` (legacy). |
| 449 | |
| 450 | Raises: |
| 451 | TripoRequestError: If the upload fails. |
| 452 | FileNotFoundError: If the file doesn't exist. |
| 453 | """ |
| 454 | if not os.path.exists(file_path): |
| 455 | raise FileNotFoundError(f"File not found: {file_path}") |
| 456 | |
| 457 | ext = os.path.splitext(file_path)[1].lower() |
| 458 | sts_format = self._EXT_TO_STS_FORMAT.get(ext, "jpeg") |
| 459 | |
| 460 | try: |
| 461 | import boto3 |
| 462 | response = await self._impl._request( |
| 463 | 'POST', "/upload/sts/token", json_data={"format": sts_format} |
| 464 | ) |
| 465 | data = response["data"] |
| 466 | s3_client = boto3.client( |
| 467 | 's3', |
| 468 | endpoint_url='https://' + data["s3_host"], |
| 469 | aws_access_key_id=data["sts_ak"], |
| 470 | aws_secret_access_key=data["sts_sk"], |
| 471 | aws_session_token=data["session_token"], |
| 472 | ) |
| 473 | s3_client.upload_file(file_path, data["resource_bucket"], data["resource_uri"]) |
| 474 | return { |
| 475 | "object": { |
| 476 | "bucket": data["resource_bucket"], |
| 477 | "key": data["resource_uri"], |
| 478 | } |
| 479 | } |
| 480 | except ImportError: |
| 481 | file_token = await self._impl.upload_file(file_path) |
| 482 | return {"file_token": file_token} |
| 483 | |
| 484 | |
| 485 | async def _image_to_file_content(self, image: str) -> Dict[str, Any]: |