Volc TOS (Tinder Object Storage) SDK
Actively maintained. Last release 2 months ago. A safe choice.
pip install tosVolcengine TOS SDK for Python
The TOS Python SDK enables Python developers to easily work with TOS(Tinder Object Storage) service in the volcengine. This document will show developers some basic examples about TOS bucket and object operation.
Install
Requirements
- Python3
Quick Start
This section introduces how to create a bucket, upload/download/delete an object in TOS service through our SDK.
Create a TOS Client
You can interact with TOS service after initiating a TOSClient instance. The accesskey and secretkey of your account, endpoint and region are required as params.
import tos
ak = "your access key"
sk = "your secret key"
endpoint = "your endpoint"
region = "your region"
bucket_name = "your bucket name"
object_key = "your object key"
client = tos.TosClientV2(ak, sk, endpoint, region)
Use VKE IRSA
After enabling VKE IRSA and associating an IAM role with the workload's
ServiceAccount, pod-identity-webhook injects VOLCENGINE_OIDC_ROLE_TRN,
VOLCENGINE_OIDC_TOKEN_FILE, and a read-only OIDC token file into the Pod.
The application can then run without a long-lived AK/SK:
import tos
client = tos.TosClientV2(
endpoint="https://tos-cn-beijing.volces.com",
region="cn-beijing",
credentials_provider=tos.OIDCCredentialsProvider(),
)
The provider exchanges the projected token through STS AssumeRoleWithOIDC
and refreshes temporary credentials before they expire. Without the webhook,
pass role_trn and oidc_token_file explicitly.
Use clients with fork
After a fork, the child rebuilds the SDK's default HTTP connection pools, bucket-type cache and DNS refresh state. OIDC credentials are fetched again on first use in the child.
Other credential providers and application callbacks need their own fork-safety guarantees.
Create a bucket
The bucket is a kind of unique namespace in TOS, which is a container to store data. This example shows you how to create a bucket.
import tos
ak = "your access key"
sk = "your secret key"
endpoint = "your endpoint"
region = "your region"
bucket_name = "your bucket name"
client = tos.TosClientV2(ak, sk, endpoint, region)
client.create_bucket(bucket_name)
Put Object
You can put your file as an object into your own bucket.
# call put_object to upload you data to the TOS
client.put_object(bucket_name, object_key, content="123")
assert resp.status_code == 200