AWS EFS(Elastic File System)是一种高性能、可伸缩的云端文件存储服务。AWS EFS Access Point是一种代理访问机制,它允许不同的用户或应用程序只访问共享文件系统中的特定目录。使用AWS SDK可以在AWS EFS访问点上挂载共享目录。
以下是使用AWS SDK Java 2.0 API将AWS EFS访问点挂载到EC2实例上的示例代码:
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.efs.EfsClient;
import software.amazon.awssdk.services.efs.model.AccessPointNotFoundException;
import software.amazon.awssdk.services.efs.model.CreateMountTargetRequest;
import software.amazon.awssdk.services.efs.model.CreateMountTargetResponse;
import software.amazon.awssdk.services.efs.model.MountTargetConflictException;
import software.amazon.awssdk.services.efs.model.MountTargetNotFoundException;
import software.amazon.awssdk.services.efs.model.SecurityGroupLimitExceededException;
import software.amazon.awssdk.services.efs.model.SecurityGroupNotFoundException;
import software.amazon.awssdk.services.efs.model.SubnetNotFoundException;
import software.amazon.awssdk.services.efs.model.Tag;
import software.amazon.awssdk.services.efs.model.TagNotFoundException;
public class EfsClientExample {
public static void main(String[] args) {
String efsAccessPointId = "fsap-0123456789abcdef";
String subnetId = "subnet-0123456789abcdef";
String securityGroupId = "sg-0123456789abcdef";
String mountTargetIp = "10.0.1.10";
String mountTargetFileSystemId = "fs-0123456789abcdef";
EfsClient efsClient = EfsClient.builder()
.region(Region.US_EAST_1)
.credentialsProvider(DefaultCredentialsProvider.create())
.build();
CreateMountTargetRequest request = CreateMountTargetRequest.builder()
.accessPointId(efsAccessPointId)
.fileSystemId(mountTargetFileSystemId)
.subnetId(subnetId)
.securityGroups(securityGroupId)
.ipAddress(mountTargetIp)
.build();
try {
CreateMountTargetResponse response = efsClient.createMountTarget(request);
System.out.println("Successfully created mount target");
System.out.println(response.mountTargetId());
} catch (AccessPointNotFoundException | MountTargetConflictException |
MountTargetNotFoundException | SecurityGroupLimitExceededException