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

qua*_*nta 6 python amazon-s3 s3cmd amazon-ami

s3cmdepel-testing通过运行从repo 安装:

yum --enablerepo epel-testing install s3cmd
Run Code Online (Sandbox Code Playgroud)

然后我调用了配置工具,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'
Run Code Online (Sandbox Code Playgroud)

我确定钥匙是正确的.

你对此有什么想法吗?


更新时间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/'
Run Code Online (Sandbox Code Playgroud)

在失败的系统上:

DEBUG: SignHeaders: 'GET\n\n\n\nx-amz-date:Fri, 21 Mar 2014 07:40:56 +0000\nx-amz-security-token:AQoDYXdzENb...\n/'
Run Code Online (Sandbox Code Playgroud)

此安全令牌取自元数据:

# 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"
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题应该改为:为什么有时会在运行时s3cmd添加x-amz-security-token标题--configure(我不使用--add-header)?

小智 9

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

使用这个新的IAM再次出现错误.

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

接下来,我试着跑步.

s3cmd --access_key=xxxx --secret_key=xxxxxxxxxxxxx ls
Run Code Online (Sandbox Code Playgroud)

使用带有S3策略的IAM密钥并且它有效.

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

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

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


小智 7

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

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

export AWS_ACCESS_KEY_ID=EXAMPLE_KEY
export AWS_SECRET_ACCESS_KEY=EXAMPLE_SECRET
Run Code Online (Sandbox Code Playgroud)

您还可以在〜/ .aws中设置aws_config_file.

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