Tencent COS Uploader
Overview
Use Tencent COS Python SDK to upload a local file and return presigned URLs for browser preview and file download.
Prerequisites
- Install dependency:
pip install cos-python-sdk-v5
- Prepare COS credentials (env vars preferred):
COS_SECRET_IDCOS_SECRET_KEYCOS_REGIONCOS_BUCKETCOS_SESSION_TOKEN(optional, for temporary credentials)
Workflow
- Confirm required inputs:
region(from--regionor env)bucket(from--bucketor env)--filelocal file path--keyobject key in bucket (default is filename)
- Run the script:
python3 scripts/cos_upload_and_presign.py \
--region ap-guangzhou \
--bucket my-bucket-1250000000 \
--file /absolute/path/to/file.pdf \
--key reports/2026/file.pdf \
--expires 3600
- Return JSON output containing:
view_url: GET presigned URL for viewingdownload_url: presigned URL withattachmentdispositionupload_result: SDK upload response
Script Parameters
--regionCOS region, optional ifCOS_REGIONexists--bucketbucket name with appid suffix, optional ifCOS_BUCKETexists--filelocal file path to upload, required--keyobject key in bucket, optional--expiresURL expiration seconds, default3600--secret-idoptional ifCOS_SECRET_IDexists--secret-keyoptional ifCOS_SECRET_KEYexists--session-tokenoptional ifCOS_SESSION_TOKENexists--schemehttpsorhttp, defaulthttps--download-filenamecustom filename for download prompt (optional)
Notes
- Keep
expiresas short as practical to reduce link leakage risk. download_urlis generated viaget_presigned_download_url;view_urlis generated viaget_presigned_url(Method='GET').- If
--keyis omitted, the script uses the source filename as object key.
