gpt4 book ai didi

java - AmazonS3Client 无法连接

转载 作者:行者123 更新时间:2023-11-29 09:37:36 34 4
gpt4 key购买 nike

我正在尝试连接到我的 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 操作。因此,我没有太多信息可以调试。

我已经尝试了默认凭证配置文件 (~/.aws/credentials) 方法和显式 secret 和访问 key (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) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.amazonaws.ClientConfiguration at com.amazonaws.services.s3.AmazonS3Client.(AmazonS3Client.java:384) at gearup.actions.CateringController.assignAttachments(CateringController.java:176) at gearup.actions.CateringController.update(CateringController.java:135)

早些时候,我尝试按照创建 ClientConfiguration 对象并将协议(protocol)设置为 HTTP 的演示进行操作。但是,我遇到了调用 new ClientConfiguration(); 构造函数引发 NullPointerException 的问题。我在这里缺少一些要求吗?

最佳答案

看起来您的项目缺少一些依赖项。

您显然在项目中配置了 aws-java-sdk-s3 jar,因为它正在解析 AmazonS3Client,但此 jar 还依赖于 aws-java-sdk-core。您需要将核心 jar 添加到类路径中。

关于java - AmazonS3Client 无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32192910/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com