|
| 1 | +import aiohttp |
| 2 | +import logging |
| 3 | + |
| 4 | +from typing import Dict, Any |
| 5 | +from ...config import config |
| 6 | +from ...consts import consts |
| 7 | + |
| 8 | +logger = logging.getLogger(consts.LOGGER_NAME) |
| 9 | + |
| 10 | + |
| 11 | +class MikuService: |
| 12 | + def __init__(self, cfg: config.Config = None): |
| 13 | + self.config = cfg |
| 14 | + self.api_key = cfg.access_key if cfg else None |
| 15 | + self.endpoint_url = cfg.endpoint_url if cfg else None |
| 16 | + |
| 17 | + def _get_auth_header(self) -> Dict[str, str]: |
| 18 | + """Generate Bearer token authorization header""" |
| 19 | + if not self.api_key: |
| 20 | + raise ValueError("QINIU_API_KEY is not configured") |
| 21 | + return { |
| 22 | + "Authorization": f"Bearer {self.api_key}" |
| 23 | + } |
| 24 | + |
| 25 | + def _build_bucket_url(self, bucket: str) -> str: |
| 26 | + """Build S3-style bucket URL""" |
| 27 | + if not self.endpoint_url: |
| 28 | + raise ValueError("QINIU_ENDPOINT_URL is not configured") |
| 29 | + |
| 30 | + # Remove protocol if present in endpoint_url |
| 31 | + endpoint = self.endpoint_url |
| 32 | + if endpoint.startswith("http://"): |
| 33 | + endpoint = endpoint[7:] |
| 34 | + elif endpoint.startswith("https://"): |
| 35 | + endpoint = endpoint[8:] |
| 36 | + |
| 37 | + # Build URL in format: https://<bucket>.<endpoint> |
| 38 | + return f"https://{bucket}.{endpoint}" |
| 39 | + |
| 40 | + def _build_stream_url(self, bucket: str, stream: str) -> str: |
| 41 | + """Build S3-style stream URL""" |
| 42 | + if not self.endpoint_url: |
| 43 | + raise ValueError("QINIU_ENDPOINT_URL is not configured") |
| 44 | + |
| 45 | + # Remove protocol if present in endpoint_url |
| 46 | + endpoint = self.endpoint_url |
| 47 | + if endpoint.startswith("http://"): |
| 48 | + endpoint = endpoint[7:] |
| 49 | + elif endpoint.startswith("https://"): |
| 50 | + endpoint = endpoint[8:] |
| 51 | + |
| 52 | + # Build URL in format: https://<bucket>.<endpoint>/<stream> |
| 53 | + return f"https://{bucket}.{endpoint}/{stream}" |
| 54 | + |
| 55 | + async def create_bucket(self, bucket: str) -> Dict[str, Any]: |
| 56 | + """ |
| 57 | + Create a bucket using S3-style API |
| 58 | +
|
| 59 | + Args: |
| 60 | + bucket: The bucket name to create |
| 61 | +
|
| 62 | + Returns: |
| 63 | + Dict containing the response status and message |
| 64 | + """ |
| 65 | + url = self._build_bucket_url(bucket) |
| 66 | + headers = self._get_auth_header() |
| 67 | + |
| 68 | + logger.info(f"Creating bucket: {bucket} at {url}") |
| 69 | + |
| 70 | + async with aiohttp.ClientSession() as session: |
| 71 | + async with session.put(url, headers=headers) as response: |
| 72 | + status = response.status |
| 73 | + text = await response.text() |
| 74 | + |
| 75 | + if status == 200 or status == 201: |
| 76 | + logger.info(f"Successfully created bucket: {bucket}") |
| 77 | + return { |
| 78 | + "status": "success", |
| 79 | + "bucket": bucket, |
| 80 | + "url": url, |
| 81 | + "message": f"Bucket '{bucket}' created successfully", |
| 82 | + "status_code": status |
| 83 | + } |
| 84 | + else: |
| 85 | + logger.error(f"Failed to create bucket: {bucket}, status: {status}, response: {text}") |
| 86 | + return { |
| 87 | + "status": "error", |
| 88 | + "bucket": bucket, |
| 89 | + "url": url, |
| 90 | + "message": f"Failed to create bucket: {text}", |
| 91 | + "status_code": status |
| 92 | + } |
| 93 | + |
| 94 | + async def create_stream(self, bucket: str, stream: str) -> Dict[str, Any]: |
| 95 | + """ |
| 96 | + Create a stream using S3-style API |
| 97 | +
|
| 98 | + Args: |
| 99 | + bucket: The bucket name |
| 100 | + stream: The stream name to create |
| 101 | +
|
| 102 | + Returns: |
| 103 | + Dict containing the response status and message |
| 104 | + """ |
| 105 | + url = self._build_stream_url(bucket, stream) |
| 106 | + headers = self._get_auth_header() |
| 107 | + |
| 108 | + logger.info(f"Creating stream: {stream} in bucket: {bucket} at {url}") |
| 109 | + |
| 110 | + async with aiohttp.ClientSession() as session: |
| 111 | + async with session.put(url, headers=headers) as response: |
| 112 | + status = response.status |
| 113 | + text = await response.text() |
| 114 | + |
| 115 | + if status == 200 or status == 201: |
| 116 | + logger.info(f"Successfully created stream: {stream} in bucket: {bucket}") |
| 117 | + return { |
| 118 | + "status": "success", |
| 119 | + "bucket": bucket, |
| 120 | + "stream": stream, |
| 121 | + "url": url, |
| 122 | + "message": f"Stream '{stream}' created successfully in bucket '{bucket}'", |
| 123 | + "status_code": status |
| 124 | + } |
| 125 | + else: |
| 126 | + logger.error(f"Failed to create stream: {stream}, status: {status}, response: {text}") |
| 127 | + return { |
| 128 | + "status": "error", |
| 129 | + "bucket": bucket, |
| 130 | + "stream": stream, |
| 131 | + "url": url, |
| 132 | + "message": f"Failed to create stream: {text}", |
| 133 | + "status_code": status |
| 134 | + } |
0 commit comments