日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

基于S3 Java API的應用開發

本文介紹通過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);