|
| 1 | +import sys |
| 2 | +import asyncio |
| 3 | +import logging |
| 4 | +import argparse |
| 5 | +import aiofiles |
| 6 | +import yarl |
| 7 | + |
| 8 | + |
| 9 | +from .. import settings |
| 10 | +from ..common import utils |
| 11 | +from ..lib.storage import commonfs |
| 12 | +from .client import DirectIO |
| 13 | +from ..exceptions.direct import StreamError, DirectIOError |
| 14 | +from ..exceptions.transport import TLSError |
| 15 | + |
| 16 | + |
| 17 | +logging.basicConfig(format="%(asctime)s [%(levelname)s] %(name)s: %(message)s", level=logging.ERROR) |
| 18 | + |
| 19 | + |
| 20 | +logger = logging.getLogger('cterasdk.direct') |
| 21 | + |
| 22 | + |
| 23 | +def validate_endpoint(endpoint): |
| 24 | + baseurl, port = yarl.URL(endpoint), 443 |
| 25 | + logger.debug('Validating connection to host: %s on port: %s', baseurl.host, port) |
| 26 | + utils.tcp_connect(baseurl.host, port, timeout=5) |
| 27 | + return f'{baseurl}' |
| 28 | + |
| 29 | + |
| 30 | +def validate_directory_and_filename(path): |
| 31 | + directory, filename = commonfs.generate_file_destination(path) |
| 32 | + assert all([directory, filename]), f'Error: Could not resolve file path: {path}' |
| 33 | + |
| 34 | + |
| 35 | +async def download_from_object_storage(options, file_id, path): |
| 36 | + async with DirectIO(**options) as client: |
| 37 | + streamer = await client.streamer(file_id) |
| 38 | + try: |
| 39 | + async with aiofiles.open(path, 'wb') as fd: |
| 40 | + async for block in streamer.start(): |
| 41 | + await fd.seek(block.offset) |
| 42 | + await fd.write(block.data) |
| 43 | + except StreamError as e: |
| 44 | + print(f'Download failed. Cause: {e.__cause__}', file=sys.stderr) |
| 45 | + |
| 46 | + |
| 47 | +def download_object(): |
| 48 | + parser = argparse.ArgumentParser( |
| 49 | + description="Download a file from CTERA Portal via CTERA Direct I/O." |
| 50 | + ) |
| 51 | + |
| 52 | + arguments = [ |
| 53 | + ("--endpoint", "-e", {"type": str, "required": True, "help": "CTERA Portal (e.g. corp.acme.ctera.com)"}), |
| 54 | + ("--path", "-p", {"type": str, "required": True, "help": "File path (e.g. ./download.zip)"}), |
| 55 | + ("--access", "-a", {"type": str, "help": "Access Key (optional)"}), |
| 56 | + ("--secret", "-s", {"type": str, "help": "Secret Key (optional)"}), |
| 57 | + ("--bearer", "-b", {"type": str, "help": "Bearer token (optional)"}), |
| 58 | + ("--file-id", "-f", {"required": True, "type": int, "help": "File ID (numeric)"}), |
| 59 | + ("--no-verify-ssl", "-k", {"action": "store_true", "help": "Disable SSL verification"}), |
| 60 | + ("--debug", "-d", {"action": "store_true", "help": "Enable debug logging"}), |
| 61 | + ] |
| 62 | + |
| 63 | + for lopt, sopt, options in arguments: |
| 64 | + parser.add_argument(lopt, sopt, **options) |
| 65 | + |
| 66 | + args = parser.parse_args() |
| 67 | + |
| 68 | + try: |
| 69 | + |
| 70 | + if args.debug: |
| 71 | + logger.setLevel(logging.DEBUG) |
| 72 | + |
| 73 | + options = { |
| 74 | + 'baseurl': validate_endpoint(args.endpoint), |
| 75 | + 'access_key_id': args.access, |
| 76 | + 'secret_access_key': args.secret, |
| 77 | + 'bearer': args.bearer |
| 78 | + } |
| 79 | + |
| 80 | + validate_directory_and_filename(args.path) |
| 81 | + |
| 82 | + settings.io.direct.api.settings.connector.ssl = not args.no_verify_ssl |
| 83 | + settings.io.direct.storage.settings.connector.ssl = not args.no_verify_ssl |
| 84 | + |
| 85 | + asyncio.run(download_from_object_storage(options, args.file_id, args.path)) |
| 86 | + except ConnectionError: |
| 87 | + print(f'Error: Could not establish connection to host: {args.endpoint}:443', file=sys.stderr) |
| 88 | + except (AssertionError, TLSError, DirectIOError) as e: |
| 89 | + print(e, file=sys.stderr) |
0 commit comments