Access points (Java SDK V2)

Updated at:

Create, query, list, and delete access points, and manage access point policies by using OSS Java SDK V2. Access points apply fine-grained access control to an entire bucket or to the objects under a specific prefix within a bucket.

Features

An access point is a custom access entry point that operates at the bucket level or prefix level. Each access point has its own access domain (alias), ARN, and access policy, which lets you restrict what different applications or users can access within a bucket. OSS Java SDK V2 lets you manage the following resources:

  • Access points: create ( CreateAccessPoint ), query ( GetAccessPoint ), list ( ListAccessPoints ), and delete ( DeleteAccessPoint ) them. When you create an access point, use networkOrigin to specify the network origin ( Internet or vpc ). For access points in a VPC, you can also specify vpcId .

  • Access point policies: set ( PutAccessPointPolicy ), get ( GetAccessPointPolicy ), and delete ( DeleteAccessPointPolicy ) policies at the access point level. Policy content is in JSON format.

  • Object Process access points: use CreateAccessPointForObjectProcess to create an access point that supports WriteGetObjectResponse and customizes response content.

After you create an access point, access point-level settings such as policies and Block Public Access take effect together with the bucket-level settings. For detailed rules, see the access points user guide.

Usage notes

  • Access point operations require alibabacloud-oss-v2 (OSS Java SDK V2) 0.3.1 or later.

  • The sample code in this topic uses cn-hangzhou , the region ID of the China (Hangzhou) region, as an example. If you use another region, replace it with the actual region ID.

  • The examples in this topic obtain access credentials from environment variables. Make sure that your AccessKey credentials are configured before you run the sample code.

  • In requests that are related to access points, pass the bucket name in the bucket parameter and the access point name in accessPointName . Each bucket supports a limited number of access points, and requests that exceed the limit return an error.

  • Delete the policy of an access point before you delete the access point. Deleted access points cannot be restored. You can also manage access points in the OSS console.

  • An Alibaba Cloud account can create up to 1,000 access points. An access point name must be unique within the account in a region, be 3 to 19 characters in length, contain only lowercase letters, digits, and hyphens (-), and cannot start or end with a hyphen or end with -ossalias.

  • Anonymous access is not supported for access points.

  • After an access point is created, only its policy can be modified. Basic information such as the name and alias cannot be changed.

  • You can also create and manage access points in the OSS console, by calling API operations, or by using ossutil.

Sample code

Create an access point

The following code creates an access point with the Internet network origin for a bucket:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;

public class CreateAccessPointSample {
    public static void main(String[] args) {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";
        String apName = "example-ap";

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClientBuilder clientBuilder = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region(region);

        try (OSSClient client = clientBuilder.build()) {
            // Access point with the Internet network origin. For a VPC network origin, set networkOrigin to vpc and configure vpcId.
            CreateAccessPointConfiguration config = CreateAccessPointConfiguration.newBuilder()
                    .accessPointName(apName)
                    .networkOrigin("Internet")
                    .build();

            CreateAccessPointResult result = client.createAccessPoint(CreateAccessPointRequest.newBuilder()
                    .bucket(bucket)
                    .createAccessPointConfiguration(config)
                    .build());

            System.out.printf("Create access point success, alias: %s, ARN: %s, request id: %s%n",
                    result.accessPointResult().alias(),
                    result.accessPointResult().accessPointArn(),
                    result.requestId());
        } catch (Exception e) {
            System.out.printf("error:%n%s", e);
        }
    }
}

The following code creates an access point with the vpc network origin:

            // Access point with the vpc network origin
            AccessPointVpcConfiguration vpcConfiguration = AccessPointVpcConfiguration.newBuilder()
                    .vpcId("vpc-8fzxxxxxxxxxxxxxxxx")
                    .build();

            CreateAccessPointConfiguration config = CreateAccessPointConfiguration.newBuilder()
                    .accessPointName(apName)
                    .networkOrigin("vpc")
                    .vpcConfiguration(vpcConfiguration)
                    .build();

Query access point details

The following code queries the details of a specified access point:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;

public class GetAccessPointSample {
    public static void main(String[] args) {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";
        String apName = "example-ap";

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClientBuilder clientBuilder = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region(region);

        try (OSSClient client = clientBuilder.build()) {
            GetAccessPointResult result = client.getAccessPoint(GetAccessPointRequest.newBuilder()
                    .bucket(bucket)
                    .accessPointName(apName)
                    .build());

            System.out.printf("Name: %s, Bucket: %s, Network origin: %s, request id: %s%n",
                    result.accessPointResult().accessPointName(),
                    result.accessPointResult().bucket(),
                    result.accessPointResult().networkOrigin(),
                    result.requestId());
        } catch (Exception e) {
            System.out.printf("error:%n%s", e);
        }
    }
}

List access points

The following code lists the access points of the current user within a region:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;

public class ListAccessPointsSample {
    public static void main(String[] args) {
        String region = "cn-hangzhou";

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClientBuilder clientBuilder = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region(region);

        try (OSSClient client = clientBuilder.build()) {
            ListAccessPointsResult result = client.listAccessPoints(ListAccessPointsRequest.newBuilder()
                    .build());

            System.out.printf("Status code:%d, request id:%s%n",
                    result.statusCode(), result.requestId());

            for (AccessPoint accessPoint : result.accessPointsResult().accessPoints().accessPoint()) {
                System.out.printf("Name: %s, Bucket: %s, Network Origin: %s%n",
                        accessPoint.accessPointName(), accessPoint.bucket(), accessPoint.networkOrigin());
            }
        } catch (Exception e) {
            System.out.printf("error:%n%s", e);
        }
    }
}

Manage access point policies

The following code sets an access policy for a specified access point:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
import com.aliyun.sdk.service.oss2.transport.BinaryData;

public class PutAccessPointPolicySample {
    public static void main(String[] args) {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";
        String apName = "example-ap";

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClientBuilder clientBuilder = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region(region);

        try (OSSClient client = clientBuilder.build()) {
            String policy = "{\n" +
                    "  \"Version\": \"1\",\n" +
                    "  \"Statement\": [\n" +
                    "    {\n" +
                    "      \"Action\": [\"oss:*\"],\n" +
                    "      \"Effect\": \"Allow\",\n" +
                    "      \"Principal\": [\"1234567890123456\"],\n" +
                    "      \"Resource\": [\"acs:oss:cn-hangzhou:1234567890123456:accesspoint/example-ap\"]\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}";

            PutAccessPointPolicyResult result = client.putAccessPointPolicy(PutAccessPointPolicyRequest.newBuilder()
                    .bucket(bucket)
                    .accessPointName(apName)
                    .body(BinaryData.fromString(policy))
                    .build());

            System.out.printf("Put access point policy success, request id: %s%n",
                    result.requestId());
        } catch (Exception e) {
            System.out.printf("error:%n%s", e);
        }
    }
}

To get or delete an access point policy, remove the body parameter from the preceding request and call client.getAccessPointPolicy(...) or client.deleteAccessPointPolicy(...) .

Delete an access point

The following code deletes a specified access point:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;

public class DeleteAccessPointSample {
    public static void main(String[] args) {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";
        String apName = "example-ap";

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClientBuilder clientBuilder = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region(region);

        try (OSSClient client = clientBuilder.build()) {
            DeleteAccessPointResult result = client.deleteAccessPoint(DeleteAccessPointRequest.newBuilder()
                    .bucket(bucket)
                    .accessPointName(apName)
                    .build());

            System.out.printf("Delete access point success, request id: %s%n",
                    result.requestId());
        } catch (Exception e) {
            System.out.printf("error:%n%s", e);
        }
    }
}

References