Bucket Policy (Java SDK V2)

Updated at:

You can use a bucket policy to grant other users access to a specified vector bucket. This topic describes how to set, view, and delete a bucket policy for a vector bucket by using OSS Java SDK V2.

Note

Note: The bucket policy of a vector bucket must be managed by using the vector client ( OSSVectorsClient ) with the account ID ( accountId ) configured. OSS Java SDK V2 does not provide strongly typed interfaces for vector bucket policies yet. The following examples use the generic invokeOperation operation. In the policy, the Resource element uses the vector-specific format acs:ossvector:{region}:{accountId}:*/* , and the Action element uses vector operations (such as oss:PutVectors and oss:GetVectors ).

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles must be granted the following permissions by using a RAM policy or a bucket policy.

API

Actions

Description

PutBucketPolicy

oss:PutBucketPolicy

Sets the policy of a vector bucket.

GetBucketPolicy

oss:GetBucketPolicy

Views the policy of a vector bucket.

DeleteBucketPolicy

oss:DeleteBucketPolicy

Deletes the policy of a vector bucket.

Set a bucket policy

Set a bucket policy for a vector bucket. The following example denies a specified account from performing the PutVectors and GetVectors operations on the vector bucket.

import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.OperationInput;
import com.aliyun.sdk.service.oss2.OperationOptions;
import com.aliyun.sdk.service.oss2.OperationOutput;
import com.aliyun.sdk.service.oss2.transport.BinaryData;
import java.util.HashMap;
import java.util.Map;
import com.aliyun.sdk.service.oss2.vectors.OSSVectorsClient;

public class PutBucketPolicySample {
    public static void main(String[] args) throws Exception {
        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        try (OSSVectorsClient client = OSSVectorsClient.newBuilder()
                .region("cn-hangzhou")
                .accountId("1234567890")
                .credentialsProvider(provider)
                .build()) {
            String policy = "{\n"
                    + "  \"Version\": \"1\",\n"
                    + "  \"Statement\": [\n"
                    + "    {\n"
                    + "      \"Action\": [\"oss:PutVectors\", \"oss:GetVectors\"],\n"
                    + "      \"Effect\": \"Deny\",\n"
                    + "      \"Principal\": [\"1234567890\"],\n"
                    + "      \"Resource\": [\"acs:ossvector:cn-hangzhou:1234567890:*/*\"]\n"
                    + "    }\n"
                    + "  ]\n"
                    + "}";
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            Map<String, String> parameters = new HashMap<>();
            parameters.put("policy", "");
            OperationInput input = OperationInput.newBuilder()
                    .opName("PutBucketPolicy")
                    .method("PUT")
                    .bucket("examplebucket")
                    .parameters(parameters)
                    .headers(headers)
                    .body(BinaryData.fromString(policy))
                    .build();
            OperationOutput output = client.invokeOperation(input, OperationOptions.defaults());
            System.out.println("status code: " + output.statusCode()
                    + ", request id: " + output.headers().get("x-oss-request-id"));
        }
    }
}

View a bucket policy

View the current policy of a vector bucket. The policy content is included in the body of the returned result.

import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.OperationInput;
import com.aliyun.sdk.service.oss2.OperationOptions;
import com.aliyun.sdk.service.oss2.OperationOutput;
import com.aliyun.sdk.service.oss2.transport.BinaryData;
import java.util.HashMap;
import java.util.Map;
import com.aliyun.sdk.service.oss2.vectors.OSSVectorsClient;

public class GetBucketPolicySample {
    public static void main(String[] args) throws Exception {
        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        try (OSSVectorsClient client = OSSVectorsClient.newBuilder()
                .region("cn-hangzhou")
                .accountId("1234567890")
                .credentialsProvider(provider)
                .build()) {
            Map<String, String> parameters = new HashMap<>();
            parameters.put("policy", "");
            OperationInput input = OperationInput.newBuilder()
                    .opName("GetBucketPolicy")
                    .method("GET")
                    .bucket("examplebucket")
                    .parameters(parameters)
                    .build();
            OperationOutput output = client.invokeOperation(input, OperationOptions.defaults());
            System.out.println("status code: " + output.statusCode()
                    + ", request id: " + output.headers().get("x-oss-request-id"));
        }
    }
}

Delete a bucket policy

Delete the policy of a vector bucket.

import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.OperationInput;
import com.aliyun.sdk.service.oss2.OperationOptions;
import com.aliyun.sdk.service.oss2.OperationOutput;
import com.aliyun.sdk.service.oss2.transport.BinaryData;
import java.util.HashMap;
import java.util.Map;
import com.aliyun.sdk.service.oss2.vectors.OSSVectorsClient;

public class DeleteBucketPolicySample {
    public static void main(String[] args) throws Exception {
        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        try (OSSVectorsClient client = OSSVectorsClient.newBuilder()
                .region("cn-hangzhou")
                .accountId("1234567890")
                .credentialsProvider(provider)
                .build()) {
            Map<String, String> parameters = new HashMap<>();
            parameters.put("policy", "");
            OperationInput input = OperationInput.newBuilder()
                    .opName("DeleteBucketPolicy")
                    .method("DELETE")
                    .bucket("examplebucket")
                    .parameters(parameters)
                    .build();
            OperationOutput output = client.invokeOperation(input, OperationOptions.defaults());
            System.out.println("status code: " + output.statusCode()
                    + ", request id: " + output.headers().get("x-oss-request-id"));
        }
    }
}

Related documents

  • For the policy syntax and the values of Action and Resource for vector bucket policies, see the access control documentation for OSS Vectors.

  • For generic vector operation sample code, see InvokeOperationVectorDemo.java.