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

Torch Object Storage

Copy page
Download PDF
Vector bucket
PutVectorBucketPolicy(TOS Vectors Node.js SDK)
Copy page
Download PDF
PutVectorBucketPolicy(TOS Vectors Node.js SDK)
putVectorBucketPolicy interface sets the IAM Policy of a vector bucket. This article explains how to complete this operation using the putVectorBucketPolicy interface of the TOS Vectors Node.js SDK.​
Notes​
  • To prevent key leakage, this sample code reads the Access Key ID and Secret Access Key from environment variables. Therefore, before using the SDK, you need to configure long-term access credentials. For detailed instructions, see Configure long-term access credentials.​
  • TOS Vectors Node.js SDK supports using temporary access credentials; it also supports configuring timeout mechanisms, retry policies, and other options. You can flexibly adjust these settings according to business needs to enhance application security. For details, see Common configuration scenarios.​
Permissions required​
The main account has all permissions by default, but IAM users have no permissions by default. If you need a sub-account to perform the putVectorBucketPolicy operation, the main account must grant the following permissions to the sub-account through IAM policy and bucket policy:​
tosvectors:PutVectorBucketPolicy​
Code example​
Set the IAM policy for the vector bucket​
​
// Set the IAM policy for the vector bucket​
​
import {​
TosVectorsClient,​
TosClientError,​
TosServerError,​
} from "@volcengine/tos-sdk";​
​
// Retrieve authentication information and configuration from environment variables​
const client = new TosVectorsClient({​
accessKeyId: process.env["TOS_ACCESS_KEY"] as string, // TOS_ACCESS_KEY: your Access Key ID​
accessKeySecret: process.env["TOS_SECRET_KEY"] as string, // TOS_SECRET_KEY: your Secret Key​
region: 'Provide your region',​
endpoint: 'Provide your vector endpoint',​
});​
​
const vectorBucketName = 'Provide your vector bucket name';​
const accountId = process.env["TOS_ACCOUNT_ID"];​
​
/**​
* Handle errors that may occur during SDK calls.​
*@paramerror the error object caught​
*/​
function handleError(error: Error) {​
if (error instanceof TosClientError) {​
// Client error, usually due to parameter issues or network problems​
console.log("Client Error:");​
console.log(" Message:", error.message);​
if (error.stack) {​
console.log(" Stack:", error.stack);​
}​
} else if (error instanceof TosServerError) {​
// Server error, indicating the request was received but failed to process​
console.log("Server Error:");​
console.log(" Request ID:", error.requestId);​
console.log(" Status Code:", error.statusCode);​
console.log(" Code:", error.code);​
console.log(" Message:", error.message);​
console.log(" Headers:", error.headers);​
} else {​
// Other unknown errors​
console.log("Unexpected Error:");​
console.log(" Message:", error.message);​
}​
}​
​
async function main() {​
try {​
// Construct IAM Policy document (JSON string)​
const simplePolicy = JSON.stringify({​
Version: "2012-10-17",​
Statement: [​
{​
Effect: "Allow",​
Principal: [accountId],​
Action: "tosvectors:GetVectorBucket",​
Resource: `trn:tosvectors:${process.env["TOS_REGION"]}:${accountId}:bucket/${vectorBucketName}`,​
},​
],​
});​
​
// 2. Call the corresponding API method to set the IAM Policy for the vector bucket.​
const result = await client.putVectorBucketPolicy({​
vectorBucketName,​
accountID: accountId,​
policy: simplePolicy,​
});​
​
// 3. Process and print key information from the returned result.​
console.log("Request Succeeded:");​
console.log(" Request ID:", result.requestId);​
console.log(" Status Code:", result.statusCode);​
console.log(" Headers:", result.headers);​
console.log(" Result Data:", result.data);​
} catch (error: any) {​
handleError(error);​
}​
}​
​
main();​
​
​
Related documentation​
For API documentation on PutVectorBucketPolicy, see PutVectorBucketPolicy.​
​
Last updated: 2026.02.06 18:01:44