You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Vector bucket
PutVectorBucketPolicy(TOS Vectors Java SDK)
Copy page
Download PDF
PutVectorBucketPolicy(TOS Vectors Java SDK)
The putVectorBucketPolicy interface sets an access policy for a vector bucket. This article explains how to configure the policy using the putVectorBucketPolicy interface in the TOS Vectors Java SDK.​
Notes​
  • To prevent key leakage, the sample code reads Access Key ID and Secret Access Key from environment variables. Before using the SDK, configure long-term access credentials. For details, see configure long-term access credentials.​
  • The TOS Vectors Java SDK supports temporary access credentials and offers configurations for timeout, retry policies, and other mechanisms. You can adjust these based on your business needs to enhance application security. For details, see common configuration scenarios.​
Permissions required​
The main account has full permissions by default, while IAM users have no permissions by default. If you want to use a subaccount to execute the putVectorBucketPolicy operation, the main account must grant the following permissions to the subaccount via IAM policies and bucket policies:​
tosvectors:PutVectorBucketPolicy​
Code example​
Set IAM policy for vector bucket​
​
// Set the IAM Policy for the vector bucket.​
import com.volcengine.tos.TOSVectorsClientBuilder;​
import com.volcengine.tos.TOSVectors;​
import com.volcengine.tos.TosClientException;​
import com.volcengine.tos.TosServerException;​
import com.volcengine.tos.model.vectors.PutVectorBucketPolicyInput;​
import com.volcengine.tos.model.vectors.PutVectorBucketPolicyOutput;​
​
public class PutVectorBucketPolicyExample {​
public static void main(String[] args) {​
String endpoint = 'Provide your endpoint';​
String region = 'Provide your region';​
String accessKey = System.getenv("TOS_ACCESS_KEY");​
String secretKey = System.getenv("TOS_SECRET_KEY");​
String accountID = System.getenv("TOS_ACCOUNT_ID");​
​
// Fill in the vector bucket name.​
String vectorBucketName = 'Provide your vector bucket name';​
​
// Initialize client.​
TOSVectors client = new TOSVectorsClientBuilder().build(region, endpoint, accessKey, secretKey);​
​
try {​
// Create an IAM Policy that allows specified users to access the vector bucket.​
// The policy format follows the IAM Policy specification.​
String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":[\"\"],\"Action\":\"tosvectors:GetVectorBucket\",\"Resource\":\"trn:tosvectors:"+ region +":"+ accountID + ":bucket/" + vectorBucketName + "\"}]}";​
​
// Construct the request parameters for setting the vector bucket policy.​
PutVectorBucketPolicyInput input = PutVectorBucketPolicyInput.builder()​
.vectorBucketName(vectorBucketName)​
.accountId(accountID)​
.policy(policy)​
.build();​
​
// Call API to set vector bucket policy.​
PutVectorBucketPolicyOutput output = client.putVectorBucketPolicy(input);​
​
// Process the return result.​
System.out.println("Set the vector bucket IAM policy successfully");​
System.out.println("RequestID: " + output.getRequestInfo().getRequestId());​
System.out.println("StatusCode: " + output.getRequestInfo().getStatusCode());​
System.out.println("The policy has been successfully applied to the vector bucket: " + vectorBucketName);​
​
} catch (TosClientException e) {​
// Operation failed, caught a Client exception. Generally, it is due to incorrect request parameters, and in this case, the request was not sent.​
System.out.println("Operation failed");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (TosServerException e) {​
// Operation failed, server exception captured, and detailed error information returned from the server can be obtained.​
System.out.println("Operation failed");​
System.out.println("StatusCode: " + e.getStatusCode());​
System.out.println("Code: " + e.getCode());​
System.out.println("Message: " + e.getMessage());​
System.out.println("RequestID: " + e.getRequestID());​
} catch (Throwable t) {​
// As a fallback to catch other exceptions, it generally won't execute here​
System.out.println("Operation failed");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
Related documentation​
For the API documentation of PutVectorBucketPolicy, see PutVectorBucketPolicy.​
Last updated: 2026.02.06 18:01:48