raw*_*215 1 java amazon-s3 amazon-web-services
我正在尝试连接到我的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/credentials)方法和显式秘密和访问密钥(新的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;
}
}
Run Code Online (Sandbox Code Playgroud)
*********代码尝试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());
}
Run Code Online (Sandbox Code Playgroud)
它永远不会超越这AmazonS3 s3client = new AmazonS3Client(Utils.getAWSCredentials());条线.我已经验证了BasicAWSCredentials对象包含正确的字段值.基于此信息可能会出现阻止S3客户端连接的问题?
**编辑**
我在生成的堆栈跟踪中发现了这看起来像有用的信息:
org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java:745)引起:java.lang.NoClassDefFoundError:无法初始化类com.amazonaws.ClientConfiguration at com.amazonaws.services.s3.AmazonS3Client.(AmazonS3Client.java:384)at gearup.actions.CateringController.assignAttachments(CateringController.java:176)在gearup.actions.CateringController.update(CateringController.java) :135)
之前我尝试过跟随创建ClientConfiguration对象并将协议设置为HTTP的演示.但是我遇到了一个调用new ClientConfiguration();构造函数抛出NullPointerException的问题.我在这里错过了一些要求吗?
这完全是奇怪的,因为aws-java-sdk-s3明确依赖aws-java-sdk-core(见pom.xml).这里有点可疑.
对我来说,事实证明这是一个apache httpclient版本的冲突(我的一个POM中的旧版本比一个亚马逊图书馆使用的版本更旧).
我从其他人那里听到类似的冲突,例如jackson.
所以对于这种情况下的任何人,我建议您在Eclipse中Dependency hierarchy打开一个POM.xml(或使用mvn dependency:tree.请参阅此处了解更多信息).
另外,请检查AWS抛出的第一条错误消息.它似乎并没有作为所有进一步堆栈跟踪的原因链接,这只会告诉你类似的东西java.lang.NoClassDefFoundError: Could not initialize class com.amazonaws.http.AmazonHttpClient.
看起来您的项目缺少某些依赖项.
您清楚地aws-java-sdk-s3在项目中配置了jar,因为它正在解析AmazonS3Client,但这个jar也依赖于aws-java-sdk-core.您需要将核心jar添加到类路径中.
| 归档时间: |
|
| 查看次数: |
9054 次 |
| 最近记录: |