错误:测试失败:400(InvalidToken):提供的令牌格式错误或无效

时间:2014-03-20 13:34:52

标签: python amazon-s3 ami s3cmd

s3cmd是从epel-testing repo安装的:

yum --enablerepo epel-testing install s3cmd

然后我用s3cmd --configure调用了配置工具,但是我收到了这个错误:

Test access with supplied credentials? [Y/n] 
Please wait, attempting to list all buckets...
ERROR: Test failed: 400 (InvalidToken): The provided token is malformed or otherwise invalid.

Invoked as: /usr/bin/s3cmd --configureProblem: AttributeError: 'S3Error' object has no attribute 'find'
S3cmd:   1.5.0-beta1
python:   2.6.8 (unknown, Mar 14 2013, 09:31:22) 
[GCC 4.6.2 20111027 (Red Hat 4.6.2-2)]

Traceback (most recent call last):
  File "/usr/bin/s3cmd", line 2323, in <module>
    main()
  File "/usr/bin/s3cmd", line 2221, in main
    run_configure(options.config, args)
  File "/usr/bin/s3cmd", line 1704, in run_configure
    if e.find('403') != -1:
AttributeError: 'S3Error' object has no attribute 'find'

我确定按键是正确的。

你对此有什么想法吗?


更新3月21日星期五22:44:42 ICT 2014

在调试模式下运行时找到了一些线索。

使用相同的凭据,在工作系统上:

DEBUG: SignHeaders: 'GET\n\n\n\nx-amz-date:Fri, 21 Mar 2014 07:07:18 +0000\n/'

在失败的系统上:

DEBUG: SignHeaders: 'GET\n\n\n\nx-amz-date:Fri, 21 Mar 2014 07:40:56 +0000\nx-amz-security-token:AQoDYXdzENb...\n/'

此安全令牌取自元数据:

# wget -O - -q 'http://169.254.169.254/latest/meta-data/iam/security-credentials/myrole'
{
  "Code" : "Success",
  "LastUpdated" : "2014-03-21T12:45:27Z",
  "Type" : "AWS-HMAC",
  "AccessKeyId" : "foo",
  "SecretAccessKey" : "bar",
  "Token" : "AQoDYXdzENb....",
  "Expiration" : "2014-03-21T19:18:02Z"
}

因此,我的问题应更改为:运行s3cmd时,为什么有时x-amz-security-token会在标头中添加--configure?(我没有使用--add-header)?

4 个答案:

答案 0 :(得分:9)

安装s3cmd,设置IAM并附加“Amazon S3 Full Access”角色后 - 我也遇到了“提供的令牌格式错误或无效”错误。接下来,我创建了一个IAM并附加了一个带有管理员凭据(一切)的策略。

使用这个新的IAM再次出现

错误。

在github.com主题中,@ mdomsch提到了使用带有嵌入式IAM角色的EC2的一些可能问题 - 这就是我使用s3cmd的EC2所具有的。

接下来,我试着跑步。

s3cmd --access_key=xxxx --secret_key=xxxxxxxxxxxxx ls

使用带有S3策略的IAM密钥,它可以工作。

因此s3cmd默认使用EC2中嵌入角色的凭据。我确信,如果您使用可以访问S3的角色启动另一个EC2 - 您将无法获得“提供的令牌格式错误或无效”错误。

但是 - 请不要使用s3cmd将-access_key=xxxx --secret_key=xxxxxxxxxxxxx参数粘贴到任何脚本中。尽可能 - 尝试在激活它们时将角色嵌入到EC2中(良好的安全性实践)

无论如何 - 要测试 - 我启动了另一个EC2 w / out任何嵌入式IAM角色,已安装s3cmd,已配置 - 并且所有内容都按照预期使用具有S3策略的IAM用户工作。

答案 1 :(得分:7)

这也让我持续了一两个小时。我在EC2实例上修复此问题的方法是在配置s3cmd之前在环境中设置我的安全凭据。

最简单的方法是将AWS Key和Secret添加为环境变量:

export AWS_ACCESS_KEY_ID=EXAMPLE_KEY
export AWS_SECRET_ACCESS_KEY=EXAMPLE_SECRET

您还可以在〜/ .aws。

中设置aws_config_file

设置环境后,运行s3cmd --configure,然后设置为go。

答案 2 :(得分:1)

对于那些有同样问题的人来说这是我的解决方案: 创建/编辑文件/ home / vagrant / .s3cfg 并在引号之间添加你的秘密和密钥,例如:

access_key="BLAHBBLAHBBLAHBBLAHB" secret_key="2T0wyvmhfGw42T0wyvmhfGw4+-"

答案 3 :(得分:1)

请注意,在Windows 10上使用以下命令也会发生此问题:

aws s3 ls s3://mybucketname/

我也为EC2实例分配了具有完全管理员访问权限的IAM角色。创建具有完全管理员权限的用户后,我要做的就是rm .aws / config和.aws / credentials文件,再次输入:

aws configure

很好的旧式拆卸和更换让我回了头。

相关问题