import json
import os
import time
from urllib.parse import urlsplit
from volcengine.ApiInfo import ApiInfo
from volcengine.Credentials import Credentials
from volcengine.ServiceInfo import ServiceInfo
from volcengine.base.Service import Service
endpoint = urlsplit("https://api.tensorslab.com/v1/video/")
region = os.getenv("ASSET_REGION", "ap-southeast-1")
actions = [
"CreateAssetGroup", "GetAssetGroup", "UpdateAssetGroup", "DeleteAssetGroup",
"CreateAsset", "GetAsset", "UpdateAsset", "DeleteAsset",
"CreateVisualValidateSession", "GetVisualValidateResult",
]
sdk = Service(
ServiceInfo(endpoint.netloc, {}, Credentials("", "", "ark", region),
10, 60, scheme=endpoint.scheme),
{action: ApiInfo("POST", endpoint.path, {
"Action": action, "Version": "2024-01-01"
}, {}, {}) for action in actions},
)
sdk.set_ak(os.environ["ASSET_AK"])
sdk.set_sk(os.environ["ASSET_SK"])
def call(action, body):
response = json.loads(sdk.json(action, {}, json.dumps(body)))
if response["ResponseMetadata"].get("Error"):
raise RuntimeError(response["ResponseMetadata"]["Error"])
return response["Result"]
group_id = call("CreateAssetGroup", {
"Name": "Reference library", "GroupType": "AIGC", "ProjectName": "default"
})["Id"]
asset_id = call("CreateAsset", {
"GroupId": group_id, "URL": "https://example.com/reference.png",
"AssetType": "Image", "Name": "Reference image"
})["Id"]
# Save both IDs; replace the URL above with your accessible media URL.
for _ in range(120):
asset = call("GetAsset", {"Id": asset_id})
if asset["Status"] == "Active":
print("asset://" + asset_id)
break
if asset["Status"] == "Failed":
raise RuntimeError(asset.get("Error", asset))
time.sleep(3)
else:
raise TimeoutError("Asset is not ready; retain its ID and query again later")