MinIO - 61. MinIO 서버와 함께 Java용 AWS SDK를 사용하는 방법

목록

How to use AWS SDK for Java with MinIO Server

MinIO 서버와 함께 Java용 AWS SDK를 사용하는 방법

aws-sdk-java is the official AWS SDK for the Java programming language.

aws-sdk-java는 Java 프로그래밍 언어용 공식 AWS SDK입니다.

In this recipe we will learn how to use aws-sdk-java with MinIO server.

이 레시피에서는 MinIO 서버와 함께 aws-sdk-java를 사용하는 방법을 알아봅니다.

1. Prerequisites

1. 전제조건

Install MinIO Server from here.

여기에서 MinIO 서버를 설치하세요.

2. Setup dependencies

2. 종속성 설정

You can either download and install the aws-java-sdk using the AWS Java SDK documentation or use Apache Maven to get the needed dependencies.

AWS Java SDK 설명서를 사용하여 aws-java-sdk를 다운로드하고 설치하거나 Apache Maven을 사용하여 필요한 종속성을 얻을 수 있습니다.

UTF-8 1.11.106 com.amazonaws aws-java-sdk-bom ${aws.sdk.version} pom import com.amazonaws aws-java-sdk-s3 org.apache.maven.plugins maven-compiler-plugin 1.8 1.8

3. Example

3. 예

Replace aws-java-sdk-1.11.213/samples/AmazonS3/S3Sample.java with code below and update Endpoint, BasicAWSCredentials, bucketName, uploadFileName and keyName with your local setup.

aws-java-sdk-1.11.213/samples/AmazonS3/S3Sample.java를 아래 코드로 바꾸고 Endpoint, BasicAWSCredentials, bucketName, uploadFileName 및 keyName을 로컬 설정으로 업데이트합니다.

Example below shows upload and download object operations on MinIO server using aws-sdk-java.

아래 예에서는 aws-sdk-java를 사용하여 MinIO 서버에서 객체 업로드 및 다운로드 작업을 보여줍니다.

import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.ClientConfiguration; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.model.S3Object; public class S3Sample { private static String bucketName = "testbucket"; private static String keyName = "hosts"; private static String uploadFileName = "/etc/hosts"; public static void main(String[] args) throws IOException { AWSCredentials credentials = new BasicAWSCredentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY"); ClientConfiguration clientConfiguration = new ClientConfiguration(); clientConfiguration.setSignerOverride("AWSS3V4SignerType"); AmazonS3 s3Client = AmazonS3ClientBuilder .standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:9000", Regions.US_EAST_1.name())) .withPathStyleAccessEnabled(true) .withClientConfiguration(clientConfiguration) .withCredentials(new AWSStaticCredentialsProvider(credentials)) .build(); try {

System.out.println("Uploading a new object to S3 from a file\n");

System.out.println("file\n에서 S3에 새 객체 업로드");

File file = new File(uploadFileName);

// Upload file

// 파일 업로드

s3Client.putObject(new PutObjectRequest(bucketName, keyName, file));

// Download file

// 파일 다운로드

GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName, keyName); S3Object objectPortion = s3Client.getObject(rangeObjectRequest); [@E System.out.println("Printing bytes retrieved:"); @E] [@K System.out.println("검색된 바이트를 인쇄하는 중:"); @K] displayTextInputStream(objectPortion.getObjectContent()); } catch (AmazonServiceException ase) { [@E System.out.println("Caught an AmazonServiceException, which " + "means your request made it " @E] [@K System.out.println("AmazonServiceException을 발견했습니다. 이는 " + "요청이 이루어졌음을 의미합니다. " @K]

+ "to Amazon S3, but was rejected with an error response" + " for some reason.");

+ "Amazon S3로 보내졌지만 오류 응답과 함께 거부되었습니다." + "어떤 이유로든.");

System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { [@E System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " @E] [@K System.out.println("AmazonClientException을 발견했습니다. 이는 클라이언트가 시도하는 동안 " + "내부 오류가 발생했음을 의미합니다." @K] + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } } private static void displayTextInputStream(InputStream input) throws IOException {

// Read one text line at a time and display.

// 한 번에 한 줄씩 읽어서 표시합니다.

BufferedReader reader = new BufferedReader(new InputStreamReader(input)); while (true) { String line = reader.readLine(); if (line == null) break; System.out.println(" " + line); } System.out.println(); } }

4. Run the Program

4. 프로그램 실행

ant Buildfile: /home/ubuntu/aws-java-sdk-1.11.213/samples/AmazonS3/build.xml

run:

실행:

[java] Uploading a new object to S3 from a file

[java] 파일에서 S3에 새 객체 업로드

[java] Printing bytes retrieved:

[java] 검색된 인쇄 바이트:

127.0.0.1 localhost127.0.1.1 minio

# The following lines are desirable for IPv6 capable hosts

# 다음 줄은 IPv6 지원 호스트에 적합합니다

::1 localhost ip6-localhost ip6-loopbackff02::1 ip6-allnodesff02::2 ip6-allrouters

BUILD SUCCESSFUL

성공적인 빌드

Total time: 3 seconds

총 시간: 3 초

5. Explore Further

5. 더 자세히 살펴보세요

MinIO Java Library for Amazon S3

Amazon S3용 MinIO Java 라이브러리