[英]AmazonS3Client will not connect
我試圖按照這些鏈接的說明連接到我的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;
}
}
*********嘗試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)原因:java.lang.NoClassDefFoundError:無法初始化類com.amazonaws.services.s3.AmazonS3Client。(AmazonS3Client.java:384)上的com.amazonaws.ClientConfiguration :135)
早些時候,我嘗試遵循一個創建ClientConfiguration對象並將協議設置為HTTP的演示。 但是我在調用new ClientConfiguration();
遇到了一個問題new ClientConfiguration();
構造函數拋出NullPointerException。 我在這里錯過了一些要求嗎?
這是完全奇怪的,因為aws-java-sdk-s3
顯式依賴aws-java-sdk-core
(請參閱pom.xml
)。 這里有些魚。
對我來說,原來是apache httpclient
版本的沖突(我的一個POM中的版本比一個Amazon庫所使用的版本更舊)。
我從其他人那里聽說過類似的沖突,例如jackson
。
因此,對於在這種情況下的任何人,我建議您在Eclipse中打開POM.xml
時簽出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添加到您的類路徑中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.