ansible aws_s3错误:botocore.exceptions.ClientError:调用HeadBucket操作时发生错误(400):错误的请求

时间:2020-04-07 11:25:56

标签: python-3.x amazon-s3 ansible boto3

我尝试使用可播放的剧本检查s3中是否存在密钥:

---

- name: s3 Handler
  hosts: localhost

  tasks:

    - name: Get AWS_SECRET_ACCESS_KEY
      debug:
        msg: "{{ lookup('env','AWS_SECRET_ACCESS_KEY') }} is an environment variable"

    - name: Get AWS_ACCESS_KEY_ID
      debug:
        msg: "{{ lookup('env','AWS_ACCESS_KEY_ID') }} is an environment variable"    

    - name: Get S3 key 
      aws_s3:
        aws_access_key: "{{ lookup('env','AWS_SECRET_ACCESS_KEY') }}"
        aws_secret_key: "{{ lookup('env','AWS_ACCESS_KEY_ID') }}"
        region: us-east-1
        bucket: dist
        object: packages/foo.1.7.3_3.zip
        dest: /home/ec2-user/foo.1.7.3_3.zip
        mode: get
        overwrite: different

但我收到错误消息:

The full traceback is:
Traceback (most recent call last):
  File "/home/ec2-user/tmp/ansible_aws_s3_payload_fdvpcpi7/__main__.py", line 381, in bucket_check
    s3.head_bucket(Bucket=bucket)
  File "/home/ec2-user/myapp/lib64/python3.6/dist-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/ec2-user/myapp/lib64/python3.6/dist-packages/botocore/client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (400) when calling the HeadBucket operation: Bad Request
fatal: [localhost]: FAILED! => {
    "boto3_version": "1.9.148",
    "botocore_version": "1.12.148",
    "changed": false,
    "error": {
        "code": "400",
        "message": "Bad Request"
    },
    "invocation": {
        "module_args": {
            "aws_access_key": "xxxxx",
            "aws_secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "bucket": "dist",
            "dest": "/home/ec2-user/foo.1.7.3_3.zip",
            "dualstack": false,
            "ec2_url": null,
            "encrypt": true,
            "encryption_kms_key_id": null,
            "encryption_mode": "AES256",
            "expiry": 600,
            "headers": null,
            "ignore_nonexistent_bucket": false,
            "marker": "",
            "max_keys": 1000,
            "metadata": null,
            "mode": "get",
            "object": "packages/foo.1.7.3_3.zip",
            "overwrite": "different",
            "permission": [
                "private"
            ],
            "prefix": "",
            "profile": null,
            "region": "us-east-1",
            "retries": 0,
            "rgw": false,
            "s3_url": null,
            "security_token": null,
            "src": null,
            "validate_certs": true,
            "version": null
        }
    },
    "msg": "Failed while looking up bucket (during bucket_check) dist.: An error occurred (400) when calling the HeadBucket operation: Bad Request",
    "response_metadata": {
        "host_id": "ZytPQdipMal9v3klLuWw1+OfAKujauS9sw+rB4K8xRAsrfdOVbfXNgccEZDUBgKamaOWLsgVN0M1111=",
        "http_headers": {
            "connection": "close",
            "content-type": "application/xml",
            "date": "Tue, 07 Apr 2020 11:10:49 GMT",
            "server": "AmazonS3",
            "transfer-encoding": "chunked",
            "x-amz-bucket-region": "us-east-1",
            "x-amz-id-2": "ZytPQdipMal9v3klLuWw1+OfAKujauS9sw+rB4K8xRAsrfdOVbfXNgccEZDUBgKamaOWLsgVN0M1111=",
            "x-amz-request-id": "731700079A94E38F"
        },
        "http_status_code": 400,
        "request_id": "731700079A94E38F",
        "retry_attempts": 1
    }
}
        to retry, use: --limit @/home/ec2-user/create_s3.retry

问题是,当我在python中从boto3运行代码时,所有事情都在工作 工作脚本:

import boto3
import botocore
from botocore.client import ClientError
import os



# us-east-1 credentials

    AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
    AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
    ec2_resource = boto3.resource('ec2', 'us-east-1', aws_access_key_id=AWS_ACCESS_KEY_ID,
                                  aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

    ec2_client = boto3.client('ec2', 'us-east-1', aws_access_key_id=AWS_ACCESS_KEY_ID,
                              aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

    dnl_dir = os.environ.get('PROJECT_HOME')
    s3 = boto3.resource('s3')
    bucket = s3.Bucket('dist')

    try:
        s3.Object('dist', 'packages/foo.1.7.3_3.zip').load()
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == "404":
            # The object does not exist.
            print("The object does not exist.")
        else:
            # Something else has gone wrong.
            raise


    try:
        bucket.download_file('packages/foo.1.7.3_3.zip', dnl_dir+'/foo.1.7.3_3.zip')
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == "404":
            print("The object does not exist.")
        else:
            raise

    print("done")

0 个答案:

没有答案