由于项目需要采用minio快速搭建了一个分布式s3存储系统,为了考虑以后迁移到ceph的可能性,后端api放弃了minio的官方api而采用Amazon提供的官方s3 api,由于minio是兼容s3协议的存储系统,因此理论上可以采用aws sdk进行调用。
下载java sdk的sample代码:
git clone https://github.com/awslabs/aws-java-sample.git
执行mvn package将自动下载依赖jar包和编译
期间将遇到错误 [Maven] Can't build because of Source option 1.5 is no longer supported,解决方式是:
将
添加到pod.xml文件
修改sample代码,将endpoint指定到内部搭建的s3地址,并且指定access_key和secret_key:
+ //AmazonS3 s3 = new AmazonS3Client();
+ AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials("admin", "password"));
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
s3.setRegion(usWest2);
+ s3.setEndpoint("http://192.168.101.200:31001");
mvn package编译
mvn exec:java执行
执行期间将报
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter的Exception,
请在pod.xml中添加
依赖
mvn exec:java执行
可以看到创建bucket和list bucket成功,但是往bucket中upload数据的时候,报
java.lang.NullPointerException异常,这是由于ETag和eTag大小写导致的兼容问题,在最新的sdk版本中有修复。
修改pod.xml中aws-sdk的version至最新版本:
再次编译,记录运行结果:
lyh@lyh:~/Project/aws-java-sample$ mvn exec:java
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/usr/share/maven/lib/guice.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building aws-java-sample 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ aws-java-sample >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ aws-java-sample <<<
[INFO]
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ aws-java-sample ---
===========================================
Getting Started with Amazon S3
===========================================
Creating bucket my-first-s3-bucket-49530254-b8e0-49ab-80f5-ac5d285e2cea
Listing buckets
- dddd
- my-first
- my-first-s3-bucket-06f0d306-8671-4bf6-bc90-6132431e461d
- my-first-s3-bucket-30a8e103-1f69-4656-85e2-2f2fcbd3948e
- my-first-s3-bucket-49530254-b8e0-49ab-80f5-ac5d285e2cea
Uploading a new object to S3 from a file
Downloading an object
Content-Type: text/plain
abcdefghijklmnopqrstuvwxyz
01234567890112345678901234
!@#$%^&*()-=[]{};':',.<>/?
01234567890112345678901234
abcdefghijklmnopqrstuvwxyz
Listing objects
- MyObjectKey (size = 135)
Deleting an object
Deleting bucket my-first-s3-bucket-49530254-b8e0-49ab-80f5-ac5d285e2cea