TOS PyTorch Connector requires valid access credentials in order to authenticate requests when interacting with TOS. This topic explains how to configure the connector's authentication information and parameters.
To securely access TOS, you must provide valid access credentials: your access key ID (AK) and secret access key (SK). Regarding TOS PyTorch Connector, we recommend passing the credentials as parameters in the code explicitly. Create a CredentialProvider(ak=ak, sk=sk) object and pass it directly via the cred parameter when instantiating TosMapDataset, TosIterableDataset, or TosCheckpoint. This approach offers strong code portability and a clear credential scope, and it does not pollute the global environment.
The easiest and recommended way is to pass the access credentials directly to TOS PyTorch Connector via CredentialProvider.
Your AK and SK are your security credentials for accessing TOS. Keep them safe.
- To obtain them: Log in to the Volcano Engine console. Then, go to IAM > Access Key Management to create and manage your AK/SK pair.
- Authorization: For data security, strictly adhere to the principle of least privilege. Assign specific permission policies to your AK and SK. If your application only needs to read training data, grant it the read-only permission (TosReadOnlyAccess). This effectively prevents risks of data tampering or deletion caused by key leakage.
After getting your AK and SK, you can pass them through the cred parameter when initializing TOS PyTorch Connector classes.
The table below explains the basic configuration parameters.
TOS PyTorch Connector offers out-of-the-box datasets and checkpoints. With simple configuration, you can load datasets and access checkpoints. Replace the parameters, such as region, endpoint, AK, and SK, with your actual values.
from tostorchconnector import CredentialProvider
from tostorchconnector import TosMapDataset
return item.key, item.size
dataset_uri = 'tos://bucket/prefix'
endpoint = 'http://tos-cn-beijing.ivolces.com'
ak = '<your-access-key>'
sk = '<your-secret-key>'
with TosMapDataset.from_prefix(dataset_uri,
region=region, endpoint=endpoint,
cred=CredentialProvider(
dataset_len = len(dataset)
from tostorchconnector import TosCheckpoint, CredentialProvider
checkpoint_uri = 'tos://bucket/key'
endpoint = 'http://tos-cn-beijing.ivolces.com'
ak = '<your-access-key>'
sk = '<your-secret-key>'
with TosCheckpoint(region, endpoint, cred=CredentialProvider(ak, sk)) as checkpoint:
with checkpoint.writer(checkpoint_uri) as writer:
with checkpoint.reader(checkpoint_uri) as reader:
assert data == b'helloworld'