Maven 项目 "AmazonS3ClientBuilder() has private access in com.amazonaws.services.s3.AmazonS3ClientBuilder"

Maven project "AmazonS3ClientBuilder() has private access in com.amazonaws.services.s3.AmazonS3ClientBuilder"

我有一个文件,很早就得到了标题中提到的错误:

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import java.io.File;

public static void main(String[] args) throws Exception {
    createAndPopulateSimpleBucket();
}

public static void createAndPopulateSimpleBucket() throws Exception {
    AmazonS3 s3client = new AmazonS3ClientBuilder().standard().build();
}

当我调用 new AmazonS3ClientBuilder() 时,标题中出现错误。

我是 Maven 的新手,我 认为 我的 pom 设置正确。这是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>AWSJavaHelloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-bom</artifactId>
                <version>1.11.715</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
        </dependency>

    </dependencies>
</project>

我猜我的 pom 有问题,但我不知道是什么。当我从 AWS SDK 中包含 everything(而不是只真正包含 S3 依赖项)时,我 得到同样的错误。

那么,有人能找出问题所在吗?

AmazonS3ClientBuilderdocumentation 声明此 class 不公开 public 无参数构造函数。这意味着您不能从 class.

调用 new AmazonS3ClientBuilder()

幸运的是,这个class提供了两个static工厂方法;其中一种方法 AmazonS3ClientBuilder#defaultClient, creates an AmazonS3 instance (the client), and the other method, AmazonS3ClientBuilder#standard 创建了构建器的实例。

了解这一点后,您可以使用以下代码段之一替换您的代码:

public static void createAndPopulateSimpleBucket() throws Exception {
    AmazonS3 s3client = AmazonS3ClientBuilder.defaultClient();
}

或:

public static void createAndPopulateSimpleBucket() throws Exception {
    AmazonS3 s3client = AmazonS3ClientBuilder.standard().build();
}