{"id":5162,"library":"cos-python-sdk-v5","title":"Tencent Cloud Object Storage (COS) SDK for Python v5","description":"The `cos-python-sdk-v5` is the official Tencent Cloud COS Python SDK. It provides an interface for interacting with Tencent Cloud Object Storage service, supporting a wide range of operations like bucket and object management. The library currently supports Python 2.6, 2.7, and Python 3.x. It is actively maintained with regular updates.","status":"active","version":"1.9.41","language":"en","source_language":"en","source_url":"https://github.com/tencentyun/cos-python-sdk-v5","tags":["tencent cloud","object storage","cos","sdk","cloud storage"],"install":[{"cmd":"pip install -U cos-python-sdk-v5","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Supported Python versions for the SDK.","package":"Python 2.7, 3.4+","optional":false}],"imports":[{"symbol":"CosConfig","correct":"from qcloud_cos import CosConfig"},{"symbol":"CosS3Client","correct":"from qcloud_cos import CosS3Client"},{"symbol":"CosServiceError","correct":"from qcloud_cos import CosServiceError"},{"symbol":"CosClientError","correct":"from qcloud_cos import CosClientError"}],"quickstart":{"code":"import os\nimport logging\nfrom qcloud_cos import CosConfig, CosS3Client, CosServiceError\n\nlogging.basicConfig(level=logging.INFO, stream=os.sys.stdout)\n\nsecret_id = os.environ.get('TENCENTCLOUD_SECRET_ID', '')\nsecret_key = os.environ.get('TENCENTCLOUD_SECRET_KEY', '')\nregion = os.environ.get('TENCENTCLOUD_REGION', 'ap-beijing') # Example region\ntoken = None  # Use None for permanent keys, provide if using temporary keys\n\nif not secret_id or not secret_key:\n    print(\"Please set TENCENTCLOUD_SECRET_ID and TENCENTCLOUD_SECRET_KEY environment variables.\")\nelse:\n    try:\n        config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)\n        client = CosS3Client(config)\n\n        # Example: List buckets\n        response = client.list_buckets()\n        print(\"Successfully listed buckets:\")\n        for bucket_info in response.get('Buckets', []):\n            print(f\"  Bucket Name: {bucket_info.get('Name')}, Creation Date: {bucket_info.get('CreationDate')}\")\n\n    except CosServiceError as e:\n        print(f\"COS Service Error: {e.get_status_code()} - {e.get_error_code()} - {e.get_error_msg()}\")\n    except CosClientError as e:\n        print(f\"COS Client Error: {e}\")\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to initialize the COS client and list existing buckets. It retrieves credentials from environment variables (recommended for security) and includes basic error handling."},"warnings":[{"fix":"Uninstall `cos-python-sdk` and install `cos-python-sdk-v5`. Update import paths and client initialization as per v5 documentation.","message":"The older `cos-python-sdk` (without '-v5') is deprecated and uses an outdated JSON API. Users must upgrade to `cos-python-sdk-v5`, which uses the XML API.","severity":"breaking","affected_versions":"<1.0.0 (for old SDK)"},{"fix":"Remove `Appid` from `CosConfig` initialization. When specifying a bucket, ensure it's in the format `BucketName-Appid`.","message":"The `Appid` parameter has been removed from `CosConfig`. The Appid should now be appended to the `Bucket` name when making calls (e.g., `examplebucket-1250000000`).","severity":"gotcha","affected_versions":"All v5 versions"},{"fix":"Initialize `CosS3Client` once and reuse the instance throughout your application's lifecycle for operations within the same region.","message":"Avoid creating a new `CosS3Client` instance for each operation. It is recommended to create only one instance per region and reuse it for multiple operations (e.g., looping uploads/downloads) to prevent excessive connections and threads.","severity":"gotcha","affected_versions":"All v5 versions"},{"fix":"Configure sub-account IAM permissions with the least privilege. Store `SecretId` and `SecretKey` in environment variables (`TENCENTCLOUD_SECRET_ID`, `TENCENTCLOUD_SECRET_KEY`) or a secure configuration management system.","message":"For security, it is highly recommended to use sub-account keys and environment variables to call the SDK, following the principle of least privilege. If permanent secret keys are used, restrict their permissions to the minimum necessary.","severity":"gotcha","affected_versions":"All v5 versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}