AWS Doc SDK ExamplesWord AWS SDK 리포지토리에는 더 많은 GitHub 예제가 있습니다.
기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
Amazon S3 Control using AWS SDKs 코드 예제
다음 코드 예제에서는 Amazon S3 Control을 AWS 소프트웨어 개발 키트(SDK)와 함께 사용하는 방법을 보여줍니다.
기본 사항은 서비스 내에서 필수 작업을 수행하는 방법을 보여주는 코드 예제입니다.
작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.
시작
다음 코드 예제에서는 'Amazon S3 컨트롤'을 사용하여 시작하는 방법을 보여줍니다.
- Java
-
- Java 2.x용 SDK
-
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.retry.RetryMode;
import software.amazon.awssdk.core.retry.RetryPolicy;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3control.S3ControlAsyncClient;
import software.amazon.awssdk.services.s3control.model.JobListDescriptor;
import software.amazon.awssdk.services.s3control.model.JobStatus;
import software.amazon.awssdk.services.s3control.model.ListJobsRequest;
import software.amazon.awssdk.services.s3control.paginators.ListJobsPublisher;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
/**
* Before running this example:
* <p/>
* The SDK must be able to authenticate AWS requests on your behalf. If you have not configured
* authentication for SDKs and tools,see https://docs.aws.amazon.com/sdkref/latest/guide/access.html in the AWS SDKs and Tools Reference Guide.
* <p/>
* You must have a runtime environment configured with the Java SDK.
* See https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html in the Developer Guide if this is not set up.
*/
public class HelloS3Batch {
private static S3ControlAsyncClient asyncClient;
public static void main(String[] args) {
S3BatchActions actions = new S3BatchActions();
String accountId = actions.getAccountId();
try {
listBatchJobsAsync(accountId)
.exceptionally(ex -> {
System.err.println("List batch jobs failed: " + ex.getMessage());
return null;
})
.join();
} catch (CompletionException ex) {
System.err.println("Failed to list batch jobs: " + ex.getMessage());
}
}
/**
* Retrieves the asynchronous S3 Control client instance.
* <p>
* This method creates and returns a singleton instance of the {@link S3ControlAsyncClient}. If the instance
* has not been created yet, it will be initialized with the following configuration:
* <ul>
* <li>Maximum concurrency: 100</li>
* <li>Connection timeout: 60 seconds</li>
* <li>Read timeout: 60 seconds</li>
* <li>Write timeout: 60 seconds</li>
* <li>API call timeout: 2 minutes</li>
* <li>API call attempt timeout: 90 seconds</li>
* <li>Retry policy: 3 retries</li>
* <li>Region: US_EAST_1</li>
* <li>Credentials provider: {@link EnvironmentVariableCredentialsProvider}</li>
* </ul>
*
* @return the asynchronous S3 Control client instance
*/
private static S3ControlAsyncClient getAsyncClient() {
if (asyncClient == null) {
SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder()
.maxConcurrency(100)
.connectionTimeout(Duration.ofSeconds(60))
.readTimeout(Duration.ofSeconds(60))
.writeTimeout(Duration.ofSeconds(60))
.build();
ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder()
.apiCallTimeout(Duration.ofMinutes(2))
.apiCallAttemptTimeout(Duration.ofSeconds(90))
.retryStrategy(RetryMode.STANDARD)
.build();
asyncClient = S3ControlAsyncClient.builder()
.region(Region.US_EAST_1)
.httpClient(httpClient)
.overrideConfiguration(overrideConfig)
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.build();
}
return asyncClient;
}
/**
* Asynchronously lists batch jobs that have completed for the specified account.
*
* @param accountId the ID of the account to list jobs for
* @return a CompletableFuture that completes when the job listing operation is finished
*/
public static CompletableFuture<Void> listBatchJobsAsync(String accountId) {
ListJobsRequest jobsRequest = ListJobsRequest.builder()
.jobStatuses(JobStatus.COMPLETE)
.accountId(accountId)
.maxResults(10)
.build();
ListJobsPublisher publisher = getAsyncClient().listJobsPaginator(jobsRequest);
return publisher.subscribe(response -> {
List<JobListDescriptor> jobs = response.jobs();
for (JobListDescriptor job : jobs) {
System.out.println("The job id is " + job.jobId());
System.out.println("The job priority is " + job.priority());
}
}).thenAccept(response -> {
System.out.println("Listing batch jobs completed");
}).exceptionally(ex -> {
System.err.println("Failed to list batch jobs: " + ex.getMessage());
throw new RuntimeException(ex);
});
}