繁体   English   中英

无法通过 boto3 创建 S3 批处理作业 - 请求无效

[英]Can't create S3 batch job through boto3 - gets request invalid

我正在尝试使用 S3Control 通过 boto3 创建 S3 Batch(不是 AWS Batch,这是 S3 Batch 操作)作业,但我收到“无效请求”响应。 我通过控制台通过 AWS S3 批处理操作进行了尝试,但现在我正在尝试通过 boto3 来创建批处理作业。 附件是代码和错误信息。

import json
import boto3
import time
import datetime
import os
s3ControlClient = boto3.client('s3control')
def lambda_handler(event, context):
    accountId = boto3.client('sts').get_caller_identity().get('Account')
    print(accountId)
    response = s3ControlClient.create_job(
        AccountId = accountId,
        ConfirmationRequired = False,
        Operation = {
            'S3PutObjectRetention': {
                'BypassGovernanceRetention': False,
                'Retention': {
                    'RetainUntilDate'   : datetime.datetime(2050, 06, 03),
                    'Mode'              : 'GOVERNANCE'
                }
            }
        },
        Report={
            'Bucket'        : 'arn:aws:s3:::test-s3-inventory',
            'Format'        : 'Report_CSV_20180820',
            'Enabled'       : True,
            #'Prefix'        : time.strftime('%Y%m%d'),
            'Prefix'        : 'report',
            'ReportScope'   : 'AllTasks'
        },
        Manifest={
            'Spec': {
                'Format': 'Report_CSV_20180820',
                'Fields': [
                    'Bucket', 'Key', 'VersionId', 'TaskStatus', 'ErrorCode', 'HTTPStatusCode', 'ResultMessage'
                ]
            },
            'Location': {
                'ObjectArn'         : 'https://test-s3-inventory.s3.amazonaws.com/job-34455-4eb5-829d-7eedrrr8564/manifest.json',
                'ETag'              : 'f4a7c0618aaed7777a5be40c266abe1f'
              }
        },
        Description = time.strftime('%Y-%m-%d')+' - Apply Retention and Legal Hold',
        Priority    = 10,
        RoleArn     = 'arn:aws:iam::277696433388194:role/s3BatchRole',
        Tags        = [
            {'Key': LOB', 'Value': 'Test'},
            {'Key': 'project',       'Value': ‘test project’}
        ]
    )

Error:
    Response:
    {
      "errorMessage": "An error occurred (InvalidRequest) when calling the CreateJob operation: Request invalid",
      "errorType": "ClientError",
      "stackTrace": [
        "  File \"/var/task/lambda_function.py\", line 13, in lambda_handler\n    response = s3ControlClient.create_job(\n",
        "  File \"/opt/python/botocore/client.py\", line 316, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
        "  File \"/opt/python/botocore/client.py\", line 635, in _make_api_call\n    raise error_class(parsed_response, operation_name)\n"
      ]
    }

我认为您的代码中有两个小错误:

  1. 正如@Limsanity82 所述,您的 object ARN 应采用以下格式:
     arn:aws:s3:::test-s3-inventory/job-34455-4eb5-829d-7eedrrr8564/manifest.json
  2. 在您的清单规范中,格式值( Report_CSV_20180820 )是错误的; 相反,您想要:
     S3BatchOperations_CSV_20180820

此外,缺少 ClientRequestToken。 根据文档

ClientRequestToken(字符串)——[必需]

一个幂等性令牌,以确保您不会意外地提交相同的请求两次。 您可以使用最大长度的任何字符串。

如果未提供,此字段将自动填充。

可以通过以下方式添加 ClientRequestToken -

import uuid
...

def lambda_handler(event, context):
    ...
    RequestToken = str(uuid.uuid4())
    ...
    response = clientControl.create_job(
      ...
      ClientRequestToken=RequestToken,
      ...
    )
    ....

此外,作为最佳实践,您还需要在创建 boto3 客户端时指定区域。

s3ControlClient=boto3.client('s3control', region_name=[Region_Name])

我已经在S3 Control Create Job Invalid Request Error中记录了我使用创建作业的语法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM