|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from veadk.auth.veauth.utils import get_credential_from_vefaas_iam |
| 18 | +from veadk.utils.logger import get_logger |
| 19 | +from veadk.utils.volcengine_sign import ve_request |
| 20 | + |
| 21 | +logger = get_logger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +def get_tts_token(region: str = "cn-beijing") -> str: |
| 25 | + logger.info("Fetching TTS token...") |
| 26 | + |
| 27 | + access_key = os.getenv("VOLCENGINE_ACCESS_KEY") |
| 28 | + secret_key = os.getenv("VOLCENGINE_SECRET_KEY") |
| 29 | + session_token = "" |
| 30 | + |
| 31 | + if not (access_key and secret_key): |
| 32 | + # try to get from vefaas iam |
| 33 | + cred = get_credential_from_vefaas_iam() |
| 34 | + access_key = cred.access_key_id |
| 35 | + secret_key = cred.secret_access_key |
| 36 | + session_token = cred.session_token |
| 37 | + |
| 38 | + res = ve_request( |
| 39 | + request_body={"ProjectName": "default", "Filter": {}}, |
| 40 | + header={"X-Security-Token": session_token}, |
| 41 | + action="ListApiKeys", |
| 42 | + ak=access_key, |
| 43 | + sk=secret_key, |
| 44 | + service="ark", |
| 45 | + version="2024-01-01", |
| 46 | + region=region, |
| 47 | + host="open.volcengineapi.com", |
| 48 | + ) |
| 49 | + try: |
| 50 | + first_api_key_id = res["Result"]["Items"][0]["Id"] |
| 51 | + except KeyError: |
| 52 | + raise ValueError(f"Failed to get ARK api key list: {res}") |
| 53 | + |
| 54 | + # get raw api key |
| 55 | + res = ve_request( |
| 56 | + request_body={"Id": first_api_key_id}, |
| 57 | + header={"X-Security-Token": session_token}, |
| 58 | + action="GetRawApiKey", |
| 59 | + ak=access_key, |
| 60 | + sk=secret_key, |
| 61 | + service="ark", |
| 62 | + version="2024-01-01", |
| 63 | + region=region, |
| 64 | + host="open.volcengineapi.com", |
| 65 | + ) |
| 66 | + try: |
| 67 | + api_key = res["Result"]["ApiKey"] |
| 68 | + logger.info("Successfully fetching ARK API Key.") |
| 69 | + return api_key |
| 70 | + except KeyError: |
| 71 | + raise ValueError(f"Failed to get ARK api key: {res}") |
0 commit comments