我正在尝试连接到我的AWS S3存储桶,以便根据这些链接上传文件'说明。
http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html#credentials-specify-provider
出于某种原因,当它试图实例化AmazonS3Client对象时,它会抛出一个被吞下的异常并退出我的Struts Action。因此,我没有太多信息需要调试。
我已尝试默认凭据配置文件(〜/ .aws /凭证)方法和显式密钥和访问密钥(新的BasicAWSCredentials( access_key_id,secret_access_key)
/**
* Uses the secret key and access key to return an object for accessing AWS features
* @return BasicAWSCredentials
*/
public static BasicAWSCredentials getAWSCredentials() {
final Properties props = new Properties();
try {
props.load(Utils.class.getResourceAsStream("/somePropFile"));
BasicAWSCredentials credObj = new BasicAWSCredentials(props.getProperty("accessKey"),
props.getProperty("secretKey"));
return credObj;
} catch (IOException e) {
log.error("getAWSCredentials IOException" + e.getMessage());
return null;
}
catch (Exception e) {
log.error("getAWSCredentials Exception: " + e.getMessage());
e.printStackTrace();
return null;
}
}
*********代码尝试S3访问**********
try {
AmazonS3 s3client = new AmazonS3Client(Utils.getAWSCredentials());
//AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
String fileKey = "catering/" + catering.getId() + fileUploadsFileName.get(i);
System.out.println("Uploading a new object to S3 from a file\n");
s3client.putObject(new PutObjectRequest(
Utils.getS3BucketName(),
fileKey, file));
// Save Attachment record
Attachment newAttach = new Attachment();
newAttach.setFile_key(fileKey);
newAttach.setFilename(fileUploadsFileName.get(i));
newAttach.setFiletype(fileUploadsContentType.get(i));
newAttach = aDao.add(newAttach);
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which " +
"means your request made it " +
"to Amazon S3, but was rejected with an error response" +
" for some reason.");
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());
fileErrors.add(fileUploadsFileName.get(i));
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which " +
"means the client encountered " +
"an internal error while trying to " +
"communicate with S3, " +
"such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
fileErrors.add(fileUploadsFileName.get(i));
} catch(Exception e) {
System.out.println("Error Message: " + e.getMessage());
}
它永远不会超过AmazonS3 s3client = new AmazonS3Client(Utils.getAWSCredentials());
行。我已验证BasicAWSCredentials对象包含正确的字段值。基于此信息可能会出现阻止S3客户端连接的问题?
**编辑**
我在生成的堆栈跟踪中发现了这个看似有用的信息:
org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(Thread.java:745)引起: java.lang.NoClassDefFoundError:无法初始化类 com.amazonaws.ClientConfiguration at com.amazonaws.services.s3.AmazonS3Client。(AmazonS3Client.java:384) 在 gearup.actions.CateringController.assignAttachments(CateringController.java:176) 在 gearup.actions.CateringController.update(CateringController.java:135)
之前我尝试过跟随创建ClientConfiguration对象并将协议设置为HTTP的演示。但是我遇到了一个调用new ClientConfiguration();
构造函数抛出NullPointerException的问题。我在这里错过了一些要求吗?
答案 0 :(得分:8)
这很奇怪,因为aws-java-sdk-s3
明确依赖于asw-java-sdk-core
(请参阅pom.xml
)。这里有点腥
对我而言,事实证明这是一个apache httpclient
版本的冲突(我的一个POM中的旧版本比一个亚马逊图书馆使用的版本更多。)
我从其他人那里听到类似的冲突,例如jackson
。
对于这种情况下的任何人,我建议您在Eclipse中打开Dependency hierarchy
时查看POM.xml
(或使用mvn dependency:tree
。有关详细信息,请参阅here )。
另外,请检查AWS抛出的第一条错误消息。它似乎并未作为所有其他堆栈跟踪中的原因链接,这只会告诉您类似java.lang.NoClassDefFoundError: Could not initialize class com.amazonaws.http.AmazonHttpClient
的内容。
答案 1 :(得分:7)
看起来您的项目缺少某些依赖项。
您清楚地在项目中配置了aws-java-sdk-s3
jar,因为它解析了AmazonS3Client,但这个jar也依赖于aws-java-sdk-core
。您需要将核心jar添加到类路径中。