全部產品
Search
文件中心

Lindorm:基於S3 Java API的應用開發

更新時間:Jul 06, 2024

本文介紹通過S3 Java API串連並使用Lindorm的具體操作和使用樣本。

前提條件

  • 已安裝Java環境,要求安裝JDK 1.8及以上版本。

  • 已擷取Lindorm寬表引擎的S3相容地址。如何擷取,請參見查看串連地址

  • 已將用戶端IP地址添加至Lindorm白名單。如何添加,請參見設定白名單

操作步驟

  1. 安裝S3 Java SDK。開啟Eclipse用戶端,建立一個Project並在pom.xml中配置Maven依賴,具體內容如下:

    • 安裝Java SDK 1.x Maven依賴

      <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>1.11.655</version>
      </dependency>
    • 安裝Java SDK 2.x Maven依賴

      <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>aws-sdk-java</artifactId>
        <version>2.17.32</version>
      </dependency>
  2. 在Project中輸入下述代碼串連並訪問Lindorm寬表引擎。

Java SDK 1.x程式碼範例

  • 建立串連

    String s3Endpoint = "http://ld-bp17j28j2y7pm****-proxy-blob.lindorm.rds.aliyuncs.com:9053"; //Lindorm寬表引擎的S3相容地址
    String bucketName = "testbucket";
    
    // 建立串連
    AmazonS3 client = AmazonS3ClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(s3Endpoint, null))
            .withPathStyleAccessEnabled(true)
            .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("AK", "SK")))
            .build();
    說明

    建立串連時,請將樣本中的“AK”和“SK”欄位替換為寬表引擎的使用者名稱和密碼。建議將密碼寫入環境變數或設定檔中,避免採用硬式編碼形式。

  • Bucket操作

    // 建立bucket
    Bucket bucket = client.createBucket(bucketName);
    
    // 擷取bucket是否存在
    HeadBucketResult result = client.headBucket(new HeadBucketRequest(bucketName));
    
    // 列舉所有bucket
    List<Bucket> buckets = client.listBuckets(new ListBucketsRequest());
    說明

    如果您需要建立新使用者並為其賦予Bucket的操作許可權,請參見建立使用者S3協議許可權管理

  • Object操作

    String content = "content";
    // 上傳object
    client.putObject(bucketName, key_name, content);
    
    // 讀取object
    S3Object object = client.getObject(bucketName, keyName);
    
    // 列舉bucket內所有object
    // list v1
    ObjectListing objects = client.listObjects(bucketName);
    // list v2
    ListObjectsV2Result results = client.listObjectsV2(bucketName);
    
    // 刪除object
    client.deleteObject(bucketName, keyName);
    
    // 大量刪除object
    client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keyName));
  • Multipart Upload操作

    // 要上傳的檔案
    File file = new File(filePath);
    long contentLength = file.length();
    long partSize = 5 * 1024 * 1024; // 設定分區大小為5MB
    
    List<PartETag> partETags = new ArrayList<PartETag>();
    
    // 初始化分區上傳
    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, keyName);
    InitiateMultipartUploadResult initResponse = client.initiateMultipartUpload(initRequest);
    
    // 上傳分區
    long filePosition = 0;
    for (int i = 1; filePosition < contentLength; i++) {
        partSize = Math.min(partSize, (contentLength - filePosition));
    
        // 分區上傳請求
        UploadPartRequest uploadRequest = new UploadPartRequest()
            .withBucketName(bucketName)
            .withKey(keyName)
            .withUploadId(initResponse.getUploadId())
            .withPartNumber(i)
            .withFileOffset(filePosition)
            .withFile(file)
            .withPartSize(partSize);
    
        UploadPartResult uploadResult = client.uploadPart(uploadRequest);
        partETags.add(uploadResult.getPartETag());
    
        filePosition += partSize;
    }
    
    // 完成分區上傳,object可見
    CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, keyName,
                                                                                    initResponse.getUploadId(), partETags);
    client.completeMultipartUpload(compRequest);

Java SDK 2.x程式碼範例

  • 建立串連

    String s3Endpoint = "http://ld-bp17j28j2y7pm****-proxy-blob.lindorm.rds.aliyuncs.com:9053"; //Lindorm寬表引擎的S3相容地址
    String bucketName = "testbucket";
    
    // 建立串連
    AwsBasicCredentials creds = AwsBasicCredentials.create("AK", "SK");
    
    S3Client client = S3Client.builder()
        .serviceConfiguration(b -> b.checksumValidationEnabled(false))
        .region(Region.AP_EAST_1)
        .credentialsProvider(StaticCredentialsProvider.create(creds))
        .endpointOverride(new URI(s3Endpoint))
        .build();
    
    // 關閉串連
    client.close();
    說明

    建立串連時,請將樣本中的“AK”和“SK”欄位替換為寬表引擎的使用者名稱和密碼。建議將密碼寫入環境變數或設定檔中,避免採用硬式編碼形式。

  • Bucket操作

    // 建立bucket
    S3Waiter s3Waiter = client.waiter();
    CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
        .bucket(bucketName)
        .build();
    
    // 擷取bucket是否存在
    client.createBucket(bucketRequest);
    HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder()
        .bucket(bucketName)
        .build();
    
    WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
    waiterResponse.matched().response().ifPresent(System.out::println);
    說明

    如果您需要建立新使用者並為其賦予Bucket的操作許可權,請參見建立使用者S3協議許可權管理

  • Object操作

    // 寫object
    PutObjectRequest putOb = PutObjectRequest.builder()
            .bucket(bucketName)
            .key(keyName)
            .build();
    
    PutObjectResponse response = client.putObject(putOb,
            RequestBody.fromString("content"));
    
    // 讀object
    GetObjectRequest objectRequest = GetObjectRequest
            .builder()
            .key(keyName)
            .bucket(bucketName)
            .build();
    
    ResponseBytes<GetObjectResponse> objectBytes = client.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
    
    // 列舉object
    ListObjectsRequest listObjects = ListObjectsRequest
                        .builder()
                        .bucket(bucketName)
                        .build();
    
    ListObjectsResponse res = client.listObjects(listObjects);
    List<S3Object> objects = res.contents();
  • Multipart Upload操作

    // 初始化分區上傳
    CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder()
        .bucket(bucketName)
        .key(keyName)
        .build();
    
    CreateMultipartUploadResponse response = client.createMultipartUpload(createMultipartUploadRequest);
    String uploadId = response.uploadId();
    System.out.println(uploadId);
    
    // 上傳分區1
    UploadPartRequest uploadPartRequest1 = UploadPartRequest.builder().bucket(bucketName).key(keyName)
        .uploadId(uploadId)
        .partNumber(1).build();
    String etag1 = client.uploadPart(uploadPartRequest1, RequestBody.fromString("content1")).eTag();
    CompletedPart part1 = CompletedPart.builder().partNumber(1).eTag(etag1).build();
    
    // 上傳分區2
    UploadPartRequest uploadPartRequest2 = UploadPartRequest.builder().bucket(bucketName).key(keyName)
        .uploadId(uploadId)
        .partNumber(2).build();
    String etag2 = client.uploadPart(uploadPartRequest2, RequestBody.fromString("content2")).eTag();
    CompletedPart part2 = CompletedPart.builder().partNumber(2).eTag(etag2).build();
    
    // 完成分區上傳,object可見
    CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder()
        .parts(part1, part2)
        .build();
    
    CompleteMultipartUploadRequest completeMultipartUploadRequest =
        CompleteMultipartUploadRequest.builder()
        .bucket(bucketName)
        .key(keyName)
        .uploadId(uploadId)
        .multipartUpload(completedMultipartUpload)
        .build();
    
    client.completeMultipartUpload(completeMultipartUploadRequest);