java操作AWS S3一些坑记录

1,aws sdk jar版本不一致问题

一开始我在pom.xml中只配置了如下aws-java-sdk-s3



com.amazonaws
aws-java-sdk-s3
1.11.506

 

然后eclipse工程中如下截图,可以看到这3个jar版本不一致

 

然后我直接执行下面的代码put 字符串和上传文件

 

 1 package com.hp.nova.common.tasksync.awss3;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 import com.amazonaws.AmazonServiceException;
 7 import com.amazonaws.SdkClientException;
 8 import com.amazonaws.auth.AWSCredentials;
 9 import com.amazonaws.auth.AWSStaticCredentialsProvider;
10 import com.amazonaws.auth.BasicAWSCredentials;
11 import com.amazonaws.auth.profile.ProfileCredentialsProvider;
12 import com.amazonaws.services.s3.AmazonS3;
13 import com.amazonaws.services.s3.AmazonS3ClientBuilder;
14 import com.amazonaws.services.s3.model.ObjectMetadata;
15 import com.amazonaws.services.s3.model.PutObjectRequest;
16 
17 public class UploadObject {
18 
19     public static void main(String[] args) throws IOException {
20         String clientRegion = "cn-northwest-1";
21         String bucketName = "nova-test";
22         String stringObjKeyName = "novatestkey";
23         String fileObjKeyName = "test.txt";
24         String fileName = "C:/nova_work_document/test.txt";
25 
26         try {
27             AWSCredentials awsCredentials = new BasicAWSCredentials("Access key ID", "Secret access key");
28             AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
29                     .withRegion(clientRegion)
30                     .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
31                     .build();
32         
33             // Upload a text string as a new object.
34             s3Client.putObject(bucketName, stringObjKeyName, "Uploaded String Object");
35             
36             // Upload a file as a new object with ContentType and title specified.
37             PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName));
38             ObjectMetadata metadata = new ObjectMetadata();
39             metadata.setContentType("plain/text");
40             metadata.addUserMetadata("x-amz-meta-title", "someTitle");
41             request.setMetadata(metadata);
42             s3Client.putObject(request);
43         }
44         catch(AmazonServiceException e) {
45             // The call was transmitted successfully, but Amazon S3 couldn't process 
46             // it, so it returned an error response.
47             e.printStackTrace();
48         }
49         catch(SdkClientException e) {
50             // Amazon S3 couldn't be contacted for a response, or the client
51             // couldn't parse the response from Amazon S3.
52             e.printStackTrace();
53         }
54     }
55 }

 

 

 

就会报错如下

Exception in thread "main" java.lang.NoSuchFieldError: SIGNING_REGION
at com.amazonaws.services.s3.AmazonS3Client.createRequest(AmazonS3Client.java:4654)
at com.amazonaws.services.s3.AmazonS3Client.createRequest(AmazonS3Client.java:4630)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1706)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:3819)
at com.hp.nova.common.tasksync.awss3.UploadObject111.main(UploadObject111.java:34)

 

解决方法:

在pom.xml中把如下都配置上



com.amazonaws
aws-java-sdk-s3
1.11.506



com.amazonaws
aws-java-sdk-kms
1.11.506



com.amazonaws
aws-java-sdk-core
1.11.506



com.amazonaws
jmespath-java
1.11.506

 

 jar包的情况如下图

 java操作AWS S3一些坑记录_第1张图片

 

 

然后执行就可以成功了

转载于:https://www.cnblogs.com/xiaohanlin/p/10451074.html

你可能感兴趣的:(java操作AWS S3一些坑记录)