簡體   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