|
| 1 | +# ---------------------------------------------------------------------- |
| 2 | +# | |
| 3 | +# | S3BrowserDataStore.py |
| 4 | +# | |
| 5 | +# | David Brownell <db@DavidBrownell.com> |
| 6 | +# | 2025-02-23 14:08:12 |
| 7 | +# | |
| 8 | +# ---------------------------------------------------------------------- |
| 9 | +# | |
| 10 | +# | Copyright David Brownell 2025 |
| 11 | +# | Distributed under the MIT License. |
| 12 | +# | |
| 13 | +# ---------------------------------------------------------------------- |
| 14 | +"""Contains the S3BrowserDataStore object""" |
| 15 | + |
| 16 | +from pathlib import Path |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +from dbrownell_Common.Streams.DoneManager import DoneManager # type: ignore[import-untyped] |
| 20 | +from dbrownell_Common import SubprocessEx # type: ignore[import-untyped] |
| 21 | +from dbrownell_Common.Types import override # type: ignore[import-untyped] |
| 22 | + |
| 23 | +from FileBackup.DataStore.Interfaces.BulkStorageDataStore import BulkStorageDataStore |
| 24 | + |
| 25 | + |
| 26 | +# ---------------------------------------------------------------------- |
| 27 | +class S3BrowserDataStore(BulkStorageDataStore): |
| 28 | + """Data store that uses the S3 Browser application (https://s3browser.com/)""" |
| 29 | + |
| 30 | + # ---------------------------------------------------------------------- |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + account_name: str, |
| 34 | + bucket_name: str, |
| 35 | + s3_dir: Optional[Path], |
| 36 | + ) -> None: |
| 37 | + super(S3BrowserDataStore, self).__init__() |
| 38 | + |
| 39 | + self.account_name = account_name |
| 40 | + self.bucket_name = bucket_name |
| 41 | + |
| 42 | + self._s3_dir = self.bucket_name / (s3_dir or Path()) |
| 43 | + |
| 44 | + self._validated_command_line = False |
| 45 | + |
| 46 | + # ---------------------------------------------------------------------- |
| 47 | + @override |
| 48 | + def ExecuteInParallel(self) -> bool: |
| 49 | + return False |
| 50 | + |
| 51 | + # ---------------------------------------------------------------------- |
| 52 | + @override |
| 53 | + def Upload( |
| 54 | + self, |
| 55 | + dm: DoneManager, |
| 56 | + local_path: Path, |
| 57 | + ) -> None: |
| 58 | + if self._validated_command_line is False: |
| 59 | + with dm.Nested( |
| 60 | + "Validating S3 Browser on the command line...", |
| 61 | + suffix="\n", |
| 62 | + ) as check_dm: |
| 63 | + result = SubprocessEx.Run("s3browser-cli license show") |
| 64 | + |
| 65 | + check_dm.WriteVerbose(result.output) |
| 66 | + |
| 67 | + if result.returncode != 0: |
| 68 | + check_dm.WriteError( |
| 69 | + "S3 Browser is not available; please make sure it exists in the path and run the script again.\n" |
| 70 | + ) |
| 71 | + return |
| 72 | + |
| 73 | + self._validated_command_line = True |
| 74 | + |
| 75 | + with dm.Nested("Uploading via S3 Browser...") as upload_dm: |
| 76 | + command_line = f's3browser-cli file upload "{self.account_name}" "{local_path / "*"}" "{self._s3_dir.as_posix()}"' |
| 77 | + |
| 78 | + upload_dm.WriteVerbose(f"Command Line: {command_line}\n\n") |
| 79 | + |
| 80 | + with upload_dm.YieldStream() as stream: |
| 81 | + upload_dm.result = SubprocessEx.Stream(command_line, stream) |
0 commit comments