我在Ubuntu 16.04 LTS中使用Python 2.7.12.我正在学习如何使用以下链接中的boto3:https://boto3.readthedocs.io/en/latest/guide/quickstart.html#using-boto-3 .我怀疑的是何时使用资源,客户端或会话以及它们各自的功能.
我只是选择python作为我的首选脚本语言,我试图找出如何使用boto3进行正确的错误处理.
我正在尝试创建一个IAM用户:
def create_user(username, iam_conn):
try:
user = iam_conn.create_user(UserName=username)
return user
except Exception as e:
return e
Run Code Online (Sandbox Code Playgroud)
当对create_user的调用成功时,我得到一个整洁的对象,其中包含API调用的http状态代码和新创建的用户的数据.
例:
{'ResponseMetadata':
{'HTTPStatusCode': 200,
'RequestId': 'omitted'
},
u'User': {u'Arn': 'arn:aws:iam::omitted:user/omitted',
u'CreateDate': datetime.datetime(2015, 10, 11, 17, 13, 5, 882000, tzinfo=tzutc()),
u'Path': '/',
u'UserId': 'omitted',
u'UserName': 'omitted'
}
}
Run Code Online (Sandbox Code Playgroud)
这非常有效.但是当这个失败时(比如用户已经存在),我只得到一个类型为botocore.exceptions.ClientError的对象,只有文本告诉我出了什么问题.
示例:ClientError('调用CreateUser操作时发生错误(EntityAlreadyExists):名称已省略的用户已存在.',)
这个(AFAIK)非常难以处理错误,因为我不能只打开生成的http状态代码(根据IAM的AWS API文档,用户已经存在409).这让我觉得我必须以错误的方式做事.最佳方式是boto3永远不会抛出异常,但juts总是返回一个反映API调用方式的对象.
任何人都可以在这个问题上启发我或指出我正确的方向吗?
非常感谢!
如何查看S3中的存储桶内部有boto3什么?(即做一个"ls")?
执行以下操作:
import boto3
s3 = boto3.resource('s3')
my_bucket = s3.Bucket('some/path/')
Run Code Online (Sandbox Code Playgroud)
收益:
s3.Bucket(name='some/path/')
Run Code Online (Sandbox Code Playgroud)
我怎么看其内容?
我知道使用Boto 2可以打开一个S3对象作为字符串:
get_contents_as_string() http://boto.readthedocs.org/en/latest/ref/file.html?highlight=contents%20string#boto.file.key.Key.get_contents_as_string
boto3中是否有等效功能?
我想知道boto3中是否存在密钥.我可以循环存储桶内容并检查密钥是否匹配.
但这似乎更长,而且有点矫枉过正.Boto3官方文档明确说明了如何做到这一点.
可能是我错过了显而易见的事.任何人都可以指出我如何实现这一目标.
我是使用Python的AWS新手,我正在尝试学习boto API但是我注意到Python有两个主要的版本/包.那将是boto和boto3.
AWS boto和boto3库有什么区别?
我正在尝试用AWS的新boto3客户端做一个"hello world" .
我使用的用例非常简单:从S3获取对象并将其保存到文件中.
在boto 2.XI会这样做:
import boto
key = boto.connect_s3().get_bucket('foo').get_key('foo')
key.get_contents_to_filename('/tmp/foo')
Run Code Online (Sandbox Code Playgroud)
在博托3.我找不到干净的方法来做同样的事情,所以我手动迭代"Streaming"对象:
import boto3
key = boto3.resource('s3').Object('fooo', 'docker/my-image.tar.gz').get()
with open('/tmp/my-image.tar.gz', 'w') as f:
chunk = key['Body'].read(1024*8)
while chunk:
f.write(chunk)
chunk = key['Body'].read(1024*8)
Run Code Online (Sandbox Code Playgroud)
要么
import boto3
key = boto3.resource('s3').Object('fooo', 'docker/my-image.tar.gz').get()
with open('/tmp/my-image.tar.gz', 'w') as f:
for chunk in iter(lambda: key['Body'].read(4096), b''):
f.write(chunk)
Run Code Online (Sandbox Code Playgroud)
它工作正常.我想知道是否有任何"本机"boto3功能可以执行相同的任务?
我有一个boto3客户端:
boto3.client('kms')
Run Code Online (Sandbox Code Playgroud)
但它发生在新机器上,它们动态地打开和关闭.
if endpoint is None:
if region_name is None:
# Raise a more specific error message that will give
# better guidance to the user what needs to happen.
raise NoRegionError()
Run Code Online (Sandbox Code Playgroud)
为什么会这样?为什么只有部分时间?
我正在使用Boto 3 python库,并希望连接到AWS CloudFront.我需要指定正确的AWS Profile(AWS Credentials),但查看官方文档,我认为无法指定它.
我正在使用代码初始化客户端:
client = boto3.client('cloudfront')
但是,这会导致它使用默认配置文件进行连接.我找不到一种方法可以指定要使用的配置文件.
当我只是运行以下代码时,我总是得到这个错误.
s3 = boto3.resource('s3')
bucket_name = "python-sdk-sample-%s" % uuid.uuid4()
print("Creating new bucket with name:", bucket_name)
s3.create_bucket(Bucket=bucket_name)
Run Code Online (Sandbox Code Playgroud)
我保存了我的凭据文件
C:\Users\myname\.aws\credentials,Boto应该从那里读取我的凭据.
我的设置错了吗?
这是输出boto3.set_stream_logger('botocore', level='DEBUG').
2015-10-24 14:22:28,761 botocore.credentials [DEBUG] Skipping environment variable credential check because profile name was explicitly set.
2015-10-24 14:22:28,761 botocore.credentials [DEBUG] Looking for credentials via: env
2015-10-24 14:22:28,773 botocore.credentials [DEBUG] Looking for credentials via: shared-credentials-file
2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking for credentials via: config-file
2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking for credentials via: ec2-credentials-file
2015-10-24 14:22:28,774 botocore.credentials [DEBUG] Looking …Run Code Online (Sandbox Code Playgroud)