Use Tencent COS Python SDK to upload a local file and return presigned URLs for browser preview and file download.
pip install cos-python-sdk-v5
COS_SECRET_IDCOS_SECRET_KEYCOS_REGIONCOS_BUCKETCOS_SESSION_TOKEN (optional, for temporary credentials)region (from --region or env)bucket (from --bucket or env)--file local file path--key object key in bucket (default is filename)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
view_url: GET presigned URL for viewingdownload_url: presigned URL with attachment dispositionupload_result: SDK upload response--region COS region, optional if COS_REGION exists--bucket bucket name with appid suffix, optional if COS_BUCKET exists--file local file path to upload, required--key object key in bucket, optional--expires URL expiration seconds, default 3600--secret-id optional if COS_SECRET_ID exists--secret-key optional if COS_SECRET_KEY exists--session-token optional if COS_SESSION_TOKEN exists--scheme https or http, default https--download-filename custom filename for download prompt (optional)expires as short as practical to reduce link leakage risk.download_url is generated via get_presigned_download_url; view_url is generated via get_presigned_url(Method='GET').--key is omitted, the script uses the source filename as object key.共 1 个版本