【发布时间】:2015-11-18 12:04:09
【问题描述】:
我正在尝试连接到我的 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) 方法和显式密钥和访问密钥(new 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 在 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。我在这里遗漏了一些要求吗?
【问题讨论】:
-
抛出的异常中的错误信息是什么?
-
@Brayden 请在上面查看我的编辑
标签: java amazon-web-services amazon-s3