从Java代码创建S3批处理作业时出现400错误的请求错误

问题描述

根据doc,我正在尝试从Java代码创建一个批处理作业。 我可以从具有相同角色和lambda arn的控制台创建作业,但是从代码中得到400 Bad Request。另外,根据此doc

,我看不到任何错误消息

这是我的代码段-

JobOperation jobOperation = new JobOperation().withLambdaInvoke(new LambdaInvokeOperation()
                    .withFunctionArn("arn:aws:lambda:eu-west-1:<account_id>:function:s3BatchOperarationsPOCLambda"));

            JobManifest manifest = new JobManifest()
                    .withSpec(new JobManifestSpec().withFormat(JobManifestFormat.S3InventoryReport_CSV_20161130)
                            .withFields(new String[] { "Bucket","Key" }))
                    .withLocation(
                            new JobManifestLocation().withObjectArn("arn:aws:s3:::<bucket_name>/manifest.csv")
                                    .withETag("e55392fa1ad40a08e40b13b3c000a0aa"));

            JobReport jobReport = new JobReport().withBucket(reportBucketName).withPrefix("testreport")
                    .withFormat(JobReportFormat.Report_CSV_20180820).withEnabled(true).withReportScope("AllTasks");

            AWSS3Control s3ControlClient = AWSS3ControlClientBuilder.standard().withRegion(Regions.US_WEST_1).build();

            String roleArn = "arn:aws:iam::<account_id>:role/S3-Batch-Role";
            String accountId = <account_id>;
            s3ControlClient.createJob(new CreateJobRequest().withAccountId(accountId).withOperation(jobOperation)
                    .withManifest(manifest).withPriority(12).withRoleArn(roleArn).withReport(jobReport)
                    .withClientRequestToken(uuid).withDescription("S3 job").withConfirmationrequired(false));


        } catch (AmazonServiceException e) {
            // The call was transmitted successfully,but Amazon S3 Couldn't process
            // it and returned an error response.
            e.printstacktrace();
        } catch (SdkClientException e) {
            System.out.println("test2" + e.getMessage());

            // Amazon S3 Couldn't be contacted for a response,or the client
            // Couldn't parse the response from Amazon S3.
            e.printstacktrace();
        }

角色具有完整的IAM和s3批处理操作权限,lambda也具有对s3的访问权限。 还为批处理操作定义了信任策略。

这是我的错误日志-

 (Service: AWSS3Control; Status Code: 400; Error Code: 400 Bad Request; Request ID: null; Proxy: null)
com.amazonaws.services.s3control.model.AWSS3ControlException: null (Service: AWSS3Control; Status Code: 400; Error Code: 400 Bad Request; Request ID: null; Proxy: null)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1811)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleServiceErrorResponse(AmazonHttpClient.java:1395)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1371)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1145)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:802)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:770)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:744)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:704)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:686)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:550)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:530)
    at com.amazonaws.services.s3control.AWSS3ControlClient.doInvoke(AWSS3ControlClient.java:1532)
    at com.amazonaws.services.s3control.AWSS3ControlClient.invoke(AWSS3ControlClient.java:1499)
    at com.amazonaws.services.s3control.AWSS3ControlClient.invoke(AWSS3ControlClient.java:1488)
    at com.amazonaws.services.s3control.AWSS3ControlClient.executeCreateJob(AWSS3ControlClient.java:265)
    at com.amazonaws.services.s3control.AWSS3ControlClient.createJob(AWSS3ControlClient.java:236)
    at com.code.platformintegrationsscheduler.handlers.test.createS3Job(test.java:68)
    at com.code.platformintegrationsscheduler.handlers.test.main(test.java:27)

解决方法

我今天遇到了同样的问题,经过一些调试并在CLI上尝试了相同的操作后,我发现 new JobReport().withBucket(reportBucketName) 使用bucketArn代替存储桶名称。

实际情况可能与您的情况有所不同。我建议您从代码序列化您的请求,并尝试在CLI中进行相同的操作,并匹配两个请求。

当我们实际需要时,AWS错误消息通常不是很有帮助。

,

我遇到了问题,问题与gradle版本有关,我们需要确保所有aws服务gradle版本都相同。

就我而言-

    compile group: 'com.amazonaws',name: 'aws-java-sdk-dynamodb',version: '1.11.844'
    compile group: 'com.amazonaws',name: 'aws-java-sdk-iam',version: '1.11.844' 
    compile group: 'com.amazonaws',name: 'aws-java-sdk-events',name: 'aws-java-sdk-s3',name: 'aws-java-sdk-batch',name: 'aws-java-sdk-s3control',version:'1.11.844'