Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法使用boto从S3读取密钥,但可以使用aws cli读取_Python_Amazon Web Services_Amazon S3_Boto_Aws Cli - Fatal编程技术网

Python 无法使用boto从S3读取密钥,但可以使用aws cli读取

Python 无法使用boto从S3读取密钥,但可以使用aws cli读取,python,amazon-web-services,amazon-s3,boto,aws-cli,Python,Amazon Web Services,Amazon S3,Boto,Aws Cli,使用此aws cli命令(配置了访问密钥),我可以从S3本地复制密钥: aws s3 cp s3://<bucketname>/test.txt test.txt aws s3 cp s3:///test.txt test.txt 在boto中使用以下代码,我得到了S3ResponseError:403禁止,无论我是允许boto使用配置的凭据,还是显式地传递它的密钥 import boto c = boto.connect_s3() b = c.get_bucket('<b

使用此aws cli命令(配置了访问密钥),我可以从S3本地复制密钥:

aws s3 cp s3://<bucketname>/test.txt test.txt
aws s3 cp s3:///test.txt test.txt
在boto中使用以下代码,我得到了S3ResponseError:403禁止,无论我是允许boto使用配置的凭据,还是显式地传递它的密钥

import boto
c = boto.connect_s3()
b = c.get_bucket('<bucketname>')
k = b.get_key('test.txt')
d = k.get_contents_as_string() # exception thrown here
导入boto
c=双向连接(s3)
b=c.获取桶(“”)
k=b.get_键('test.txt')
d=k.获取内容作为字符串()#这里抛出异常
我看过其他关于不使用validate=False等验证密钥的SO帖子,但这些都不是我的问题。将密钥复制到同一存储桶中的另一个位置时,我会得到类似的结果。使用cli成功,但不使用boto

我已经查看了boto源代码,看看它是否在做任何需要额外权限的事情,但没有什么值得我注意的


有人有什么建议吗?boto如何解析其凭据?

显式设置您的凭据,以便我们的配置与CLI中的ENV变量相同

echo$ACCESS\u密钥

echo$SECRET\u密钥

导入boto3
client=boto3.client(
“s3”,
aws\u访问密钥\u id=访问密钥,
aws_secret_access_key=secret_key
)
b=客户端。获取存储桶(“”)
k=b.get_键('test.txt')
d=k.获取内容作为字符串()
boto如何解析其凭证

boto3查找凭据的机制是搜索可能位置的列表,并在找到凭据后立即停止。Boto3搜索凭据的顺序为:

  • 在boto.client()方法中将凭据作为参数传递
  • 创建会话对象时将凭据作为参数传递
  • 环境变量
  • 共享凭据文件(~/.aws/credentials)
  • AWS配置文件(~/.AWS/config)
  • 担任提供者角色
  • Boto2配置文件(/etc/boto.cfg和~/.boto)
  • 已配置IAM角色的Amazon EC2实例上的实例元数据服务

有时这是因为boto可能正在对aws s3 cp进行不同的API调用。这是因为
get\u contents\u as\u string()
和类似
s3:GetObject
的API调用之间没有1对1的映射。哪些权限与用户/角色关联?你能临时添加所有S3权限以查看是否可以修复它吗?是的,我认为boto需要比cli更多的权限,尽管我无法从source@SimonTrewhella我遇到了一个类似的问题,你找到解决办法了吗?具体需要哪些其他权限,或者解决方法?@monguin我最后不得不在代码中添加访问密钥,我想切换到boto2
import boto3
client = boto3.client(
    's3',
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY
 )

b = client.get_bucket('<bucketname>')
k = b.get_key('test.txt')
d = k.get_contents_as_string()