java - AmazonS3Client 无法连接

标签 java amazon-web-services amazon-s3

我正在尝试连接到我的 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/

相关文章:

java - StartCalculatorActivity 类的 NoClassDefFoundError

java - 访问我的资源文件夹中的文件 Elastic Beanstalk/Spring Boot

Java - 使用分隔符不删除特殊字符

java - 在 for 循环中设置唯一的计时器

java - DynamoDBMapper 仅在对象不存在时保存

java - Hadoop S3A 文件系统,中止对象上传?

scala - 使用 scala 时的 lambda 的 AWS 凭证不起作用

regex - apache camel s3 组件 - 在前缀上指定后缀/正则表达式

java - 程序中读取不同硬币数量并打印总数的文件时出现异常

amazon-web-services - Amazon DynamoDB 获取过去 24 小时的所有项目