如何使用AWS SDK for Java操作Minio Server

aws-sdk-java是Java语言版本的官方AWS SDK。本文我们将学习如何使用aws-sdk-java操作Minio Server。

1. 前提条件

这里下载并安装Minio Server。

2. 设置依赖

你可以参考AWS Java SDK文档下载并安装aws-java-sdk,或者使用Apache Maven来获得相应的依赖。

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <aws.sdk.version>1.11.106</aws.sdk.version>
  4. </properties>
  5. <dependencyManagement>
  6. <dependencies>
  7. <dependency>
  8. <groupId>com.amazonaws</groupId>
  9. <artifactId>aws-java-sdk-bom</artifactId>
  10. <version>${aws.sdk.version}</version>
  11. <type>pom</type>
  12. <scope>import</scope>
  13. </dependency>
  14. </dependencies>
  15. </dependencyManagement>
  16. <dependencies>
  17. <dependency>
  18. <groupId>com.amazonaws</groupId>
  19. <artifactId>aws-java-sdk-s3</artifactId>
  20. </dependency>
  21. </dependencies>
  22. <build>
  23. <plugins>
  24. <plugin>
  25. <groupId>org.apache.maven.plugins</groupId>
  26. <artifactId>maven-compiler-plugin</artifactId>
  27. <configuration>
  28. <source>1.8</source>
  29. <target>1.8</target>
  30. </configuration>
  31. </plugin>
  32. </plugins>
  33. </build>

3. 示例

使用下面的代码替换aws-java-sdk-1.11.213/samples/AmazonS3/S3Sample.java,并且修改EndpointBasicAWSCredentialsbucketNameuploadFileNamekeyName成你的本地配置。

下面的示例描述的是如何使用asw-sdk-java来对Minio Server进行上传和下载操作。

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import com.amazonaws.AmazonClientException;
  7. import com.amazonaws.AmazonServiceException;
  8. import com.amazonaws.ClientConfiguration;
  9. import com.amazonaws.auth.AWSCredentials;
  10. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  11. import com.amazonaws.auth.BasicAWSCredentials;
  12. import com.amazonaws.client.builder.AwsClientBuilder;
  13. import com.amazonaws.regions.Regions;
  14. import com.amazonaws.services.s3.AmazonS3;
  15. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  16. import com.amazonaws.services.s3.model.GetObjectRequest;
  17. import com.amazonaws.services.s3.model.PutObjectRequest;
  18. import com.amazonaws.services.s3.model.S3Object;
  19. publicclass S3Sample {
  20. privatestaticString bucketName ="testbucket";
  21. privatestaticString keyName ="hosts";
  22. privatestaticString uploadFileName ="/etc/hosts";
  23. publicstaticvoid main(String[] args)throwsIOException{
  24. AWSCredentials credentials =newBasicAWSCredentials("YOUR-ACCESSKEYID","OUR-SECREYTACCESSKEY");
  25. ClientConfiguration clientConfiguration =newClientConfiguration();
  26. clientConfiguration.setSignerOverride("AWSS3V4SignerType");
  27. AmazonS3 s3Client =AmazonS3ClientBuilder
  28. .standard()
  29. .withEndpointConfiguration(newAwsClientBuilder.EndpointConfiguration("http://localhost:9000",Regions.US_EAST_1.name()))
  30. .withPathStyleAccessEnabled(true)
  31. .withClientConfiguration(clientConfiguration)
  32. .withCredentials(newAWSStaticCredentialsProvider(credentials))
  33. .build();
  34. try{
  35. System.out.println("Uploading a new object to S3 from a file\n");
  36. File file =newFile(uploadFileName);
  37. // 上传文件
  38. s3Client.putObject(newPutObjectRequest(bucketName, keyName, file));
  39. // 下载文件
  40. GetObjectRequest rangeObjectRequest =newGetObjectRequest(bucketName, keyName);
  41. S3Object objectPortion = s3Client.getObject(rangeObjectRequest);
  42. System.out.println("Printing bytes retrieved:");
  43. displayTextInputStream(objectPortion.getObjectContent());
  44. }catch(AmazonServiceException ase){
  45. System.out.println("Caught an AmazonServiceException, which "+"means your request made it "
  46. +"to Amazon S3, but was rejected with an error response"+" for some reason.");
  47. System.out.println("Error Message: "+ ase.getMessage());
  48. System.out.println("HTTP Status Code: "+ ase.getStatusCode());
  49. System.out.println("AWS Error Code: "+ ase.getErrorCode());
  50. System.out.println("Error Type: "+ ase.getErrorType());
  51. System.out.println("Request ID: "+ ase.getRequestId());
  52. }catch(AmazonClientException ace){
  53. System.out.println("Caught an AmazonClientException, which "+"means the client encountered "+"an internal error while trying to "
  54. +"communicate with S3, "+"such as not being able to access the network.");
  55. System.out.println("Error Message: "+ ace.getMessage());
  56. }
  57. }
  58. privatestaticvoid displayTextInputStream(InputStream input)throwsIOException{
  59. // 按行读取并打印。
  60. BufferedReader reader =newBufferedReader(newInputStreamReader(input));
  61. while(true){
  62. String line = reader.readLine();
  63. if(line ==null)
  64. break;
  65. System.out.println(" "+ line);
  66. }
  67. System.out.println();
  68. }
  69. }

4. 运行程序

  1. ant
  2. Buildfile:/home/ubuntu/aws-java-sdk-1.11.213/samples/AmazonS3/build.xml
  3. run:
  4. [java]Uploading a newobject to S3 from a file
  5. [java]
  6. [java]Printing bytes retrieved:
  7. [java]127.0.0.1 localhost
  8. [java]127.0.1.1 minio
  9. [java]
  10. [java]# 对于支持IPv6的主机,会有以下几行输出。
  11. [java]::1 localhost ip6-localhost ip6-loopback
  12. [java] ff02::1 ip6-allnodes
  13. [java] ff02::2 ip6-allrouters
  14. [java]
  15. BUILD SUCCESSFUL
  16. Total time:3 seconds

5. 了解更多

原文: https://docs.minio.io/cn/how-to-use-aws-sdk-for-java-with-minio-server.html