当前位置: 首页 > 知识库问答 >
问题:

使用java将文件上载到Amazon S3失败-sun。安全验证器。ValidatorException验证程序异常

谢骏奇
2023-03-14

我正在使用eclipse,并用java编写了一个方法将文件上载到amazon s3。已成功从main方法上载。从eclipse中,我从调试角度复制了等效的命令,并在命令提示符下运行,运行良好。我在我们的项目中添加了这部分代码并打包了jar文件。当我在jar文件中调用该函数

太阳安全验证器。ValidatorException:PKIX路径生成失败:sun。安全提供商。certpath。SunCertPathBuilderException:找不到请求的目标错误的有效证书路径。

我在eclipse中指出的所有依赖项jar文件,我在打包的jar文件中也指出了这些文件。但它仍然不起作用。

上传到amazon s3的方法

public static boolean saveFileToS3(File f, Long contentLength, String s3Name, String bucketName,String accessKey, String secretKey) throws FileNotFoundException, IllegalArgumentException, IOException, InterruptedException {

    AmazonS3 s3Client = null;
    try {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        s3Client = new AmazonS3Client(credentials);
    } catch (Exception e1) {
        e1.printStackTrace();
        return false;
    }        

    List<PartETag> partETags = new ArrayList<PartETag>();
    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, s3Name);
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(contentLength);

    initRequest.setObjectMetadata(metadata);
    initRequest.withCannedACL(CannedAccessControlList.PublicRead);
    InitiateMultipartUploadResult initResponse = null;
    try {
        initResponse = s3Client.initiateMultipartUpload(initRequest);
        if(initResponse == null){
            return false;
        }
    } catch (AmazonServiceException e) {
        e.printStackTrace();
    } catch (AmazonClientException e) {
        e.printStackTrace();
    } catch (Exception e){
        return false;
    }
    long partSize = 10485760; // Set part size to 10 MB.
    try {
        long filePosition = 0;
        for (int i = 1; filePosition < contentLength; i++) {
            partSize = Math.min(partSize, (contentLength - filePosition));

            if(initResponse == null){
                return false;
            }

            UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(bucketName).withKey(s3Name).withUploadId(initResponse.getUploadId()).withPartNumber(i).withFileOffset(filePosition).withFile(f).withPartSize(partSize);

            partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());

            filePosition += partSize;
        }
        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, s3Name, initResponse.getUploadId(), partETags);

        s3Client.completeMultipartUpload(compRequest);
        System.out.println("Upload complete.");
    } catch (AmazonClientException amazonClientException) {
        s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, s3Name, initResponse.getUploadId()));
        amazonClientException.printStackTrace();
        f.delete();
        return false;
    }
    f.delete();
    return true;
}

抛出异常

com.amazonaws.AmazonClientException: Unable to execute HTTP request: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:461)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:234)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3569)
at com.amazonaws.services.s3.AmazonS3Client.initiateMultipartUpload(AmazonS3Client.java:2619)
at com.virima.discoveryagentclient.ClientUtils.saveFileToS3(ClientUtils.java:2541)
at com.virima.discoveryagentclient.probes.ProbeService.runProbe(ProbeService.java:1392)
at com.virima.discoveryagentclient.probes.ProbeService.run(ProbeService.java:2187)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1497)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:535)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:403)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:391)
... 6 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1479)
... 23 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
... 29 more

我已经包含了Amazon s3上传功能所需的这四个罐子。aws-java-sdk-1.7.8.1.jarjackson-annotations-2.1.2.jarjackson-core-2.2.3.jarjackson-databind-2.1.2.jar

这是我用来从main方法运行的命令

"C:\Program Files\Java\jre1.8.0_51\bin\javaw.exe" -Dfile.encoding=Cp1252 -classpath C:\Workspace\bin;C:\Workspace\modules\lib\servlet-api.jar;C:\Workspace\modules\lib\rztbase.jar;C:\Workspace\modules\lib\mongo-2.10.1.jar;C:\Workspace\modules\lib\json_simple-1.1.jar;C:\Workspace\modules\lib\log4j-1.2.16.jar;C:\Workspace\modules\lib\commons-beanutils-1.8.3.jar;C:\Workspace\modules\lib\guava-14.0.1.jar;C:\Workspace\modules\lib\commons-httpclient-3.1.jar;C:\Workspace\modules\lib\jedis-2.8.0.jar;C:\Workspace\modules\lib\commons-pool2-2.4.2.jar;C:\Workspace\buildconfig\gen\out;C:\Workspace\modules\lib\cglib-nodep-2.2.3.jar;C:\Workspace\modules\lib\geocode.jar;C:\Workspace\modules\lib\json-org.jar;C:\Workspace\modules\lib\eclipselink.jar;C:\Workspace\modules\lib\dom4j-1.6.1.jar;C:\Workspace\modules\lib\poi-ooxml-schemas-3.7.jar;C:\Workspace\modules\lib\jaxb-api.jar;C:\Workspace\modules\lib\jaxb-core.jar;C:\Workspace\modules\lib\jaxb-impl.jar;C:\Workspace\modules\lib\jaxb-xjc.jar;C:\Workspace\modules\lib\jaxb-jxc.jar;C:\Workspace\modules\lib\gson-2.2.4.jar;C:\Workspace\modules\lib\velocity-1.7.jar;C:\Workspace\modules\lib\commons-lang-2.4.jar;C:\Workspace\modules\lib\poi-3.10-beta2.jar;C:\Workspace\modules\lib\mpxj.jar;C:\Workspace\modules\lib\shiro-core-1.2.3.jar;C:\Workspace\modules\lib\slf4j-api-1.7.6.jar;C:\Workspace\config\email\templates;C:\Workspace\modules\lib\shiro-ehcache-1.2.3.jar;C:\Workspace\modules\lib\ehcache-2.8.1.jar;C:\Workspace\modules\lib\hibernate-c3p0-4.3.4.Final.jar;C:\Workspace\modules\lib\rztbuild.jar;C:\Workspace\modules\lib\quartz-2.2.1.jar;C:\Workspace\modules\lib\aws-java-sdk-1.7.8.1.jar;C:\Workspace\modules\lib\jackson-annotations-2.1.2.jar;C:\Workspace\modules\lib\jackson-databind-2.1.2.jar;C:\Workspace\modules\lib\jackson-core-2.2.3.jar;C:\Workspace\modules\lib\Rhino_1_7R4.jar;C:\Workspace\modules\lib\poi-ooxml-3.9.jar;C:\Workspace\modules\lib\xmlbeans-2.3.0.jar;C:\Workspace\modules\lib\opencsv-2.3.jar;C:\Workspace\modules\lib\BASE64Decoder.jar;C:\Workspace\modules\lib\commons-net-3.3.jar;C:\Workspace\modules\lib\mybatis-3.2.8.jar;C:\Workspace\modules\lib\elasticsearch-1.7.1.jar;C:\Workspace\modules\lib\lucene-core-4.10.4.jar;C:\Workspace\modules\lib\commons-math3-3.5.jar;C:\Workspace\modules\lib\lucene\lucene-core-4.9.0.jar;C:\Workspace\modules\lib\itextpdf-5.5.6.jar;C:\Workspace\modules\lib\joda-time-2.8.jar;C:\Workspace\modules\lib\jcommon-1.0.0.jar;C:\Workspace\modules\lib\jfreechart-1.0.1.jar;C:\Workspace\modules\lib\mysql-connector-java-5.1.37-bin.jar;C:\Workspace\modules\lib\jsoup-1.8.3.jar;C:\Workspace\modules\lib\diffutils-1.3.0.jar;C:\Workspace\modules\lib\grammatica-1.5.jar;C:\Workspace\modules\lib\mibble-mibs-2.9.3.jar;C:\Workspace\modules\lib\mibble-parser-2.9.3.jar;C:\Workspace\modules\lib\snmp6_0.jar;C:\Workspace\modules\lib\concurrent-trees-2.4.0.jar;C:\Workspace\modules\lib\thumbnailator-0.4.8.jar;C:\Workspace\modules\lib\batik-all-1.7.jar;C:\Workspace\modules\lib\xml-apis-ext-1.3.04.jar;C:\Workspace\modules\lib\evo-inflector-1.0.1.jar;C:\Workspace\modules\lib\xmlsec-1.3.0.jar;C:\Workspace\modules\lib\sqlite-jdbc-3.7.2.jar com.em.core.services.impl.DiscoveryService

我的问题是,

>

  • 当我从eclipse main方法运行时,我可以上载,当我从命令提示符运行时,我可以上载。但当我在jar中打包时,同样的代码失败了,即使我也在类路径中添加了所有jar。为什么?

    如何修复此错误?

  • 共有2个答案

    韩鸿
    2023-03-14

    你能提到你用来触发main方法的命令吗?问题清楚地说明你遇到了握手异常,所以我假设在通过你的命令运行jar时证书路径无效。

    祁建业
    2023-03-14

    默认情况下,Java附带的根CA证书集非常有限,问题是您运行的系统中缺少amazon s3 CA证书

     类似资料:
    • 我有一个类,它用一个xml点击一个安全的URL(以https开头),然后在最后处理xml并返回响应。但在这样做的同时,我也遇到了一些例外<代码>javax。网ssl。例外:太阳。安全验证器。ValidatorException:PKIX路径生成失败:sun。安全供应商。certpath。SunCertPathBuilderException:找不到请求目标的有效认证路径 我在谷歌上搜索了很多,找到

    • 我想在用户发送请求时验证他或她的身份,所以我在SecurityConfig类中添加了以下内容: 我希望当我在地址字段中输入localhost:8080/submit时,页面可以重定向到localhost:8080/login。我的工作者实体具有“工作者”角色: 当我输入“localhost:8080/submit”时,我可以注册一个帐户并重定向到登录页面。但是当我输入正确的用户名和密码时,它会响应

    • 我已经在我现有的android项目上附加了firebase,并运行了它。这是一个场景:如果应用程序通过开发者模式从android studio运行,它(通过电话号码登录Firebase)运行平稳,我也能成功登录。但是,每当我构建签名的调试apk,Firebase都不允许登录。它给出了以下信息: 此应用程序未被授权使用Firebase身份验证。请验证firebase控制台中配置了正确的包名称和SHA

    • 问题内容: 我正在尝试使用pip为Python安装Scrapy软件包(以及其他)。我曾尝试做使用python 3和python 2安装,我已经安装/升级,像这样的setuptools:我曾尝试使用选项像这样:。但是当我运行时,我总是收到相同的错误消息。完整的输出是这样的: 我在Mac OS版本10.12.1上,并且正在使用python 3.6。有人知道这个问题的解决方案吗? 问题答案: 如此处所述

    • - 我运行了此脚本,但出现了此错误。我怎么做?

    • 我在尝试从应用程序发送电子邮件时收到以下消息。尽管我有<代码>邮件。为我的项目添加了jar</code>,它仍然会给我同样的错误。请帮忙。 javax.mail。AuthenticationFailedException:534-5.7.14请通过web浏览器登录,然后重试。534-5.7.14在534 5.7.14了解更多信息https://support.google.com/mail/ans