我正在boto3使用亚马逊的kms服务.
def __init__(self):
self.kms_client = boto3.client('kms')
def encrypt_text(self, text):
response = self.kms_client.encrypt(
KeyId = self.global_key_alias,
Plaintext = text
)
return response['CiphertextBlob']
def decrypt_text(self, encrypted_text):
# official docs state that encrypted_text should be a byte(doesn't exists in python 2)
# currently it's working when sending a string, but it's dangerous
response = self.kms_client.decrypt(
CiphertextBlob = encrypted_text
)
Run Code Online (Sandbox Code Playgroud)
我正在使用boto3,因为新功能将在那里开发.
正如笔记所说,我发送一个字符串而不是官方文档,声明你需要发送一个字节类型.
我想知道这可能会在未来发生变化吗?然后我对亚马逊的api将没用,因为我没有bytespython 2.7.9中的类型
任何意见 ?想法?
如何使用 Python boto3 库查找有关 Amazon EC2 实例的 MAC 地址的信息。
我找不到boto3的代码.我可以单独获取elb名称和InstanceID,但无法将它们链接在一起以查找ELB名称的附加实例.
如何使用 boto3 列出给定 ASG 的所有实例(id 和 IP)?如果您有一个可行的示例,请告诉我。
我正在尝试获取 s3 存储桶中上传的文件的元数据值
#我必须专门使用 boto3.resource('s3') 来进行项目中的其他 api 调用。
我在元数据字段下有以下可用数据
#元数据
Key=Content-Type
Value= application/json
Run Code Online (Sandbox Code Playgroud)
下面是代码
bucket= 'mybucket'
key='L1/input/file.json'
s3_resource = boto3.resource('s3')
object = s3_resource.Object(bucket,key)
metadata = object.metadata
Run Code Online (Sandbox Code Playgroud)
但我遇到了以下错误
[ERROR] ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
Run Code Online (Sandbox Code Playgroud)
谁可以帮我这个事。
我想备份 VPC 中的所有资源。目前,我正在通过 python 脚本做类似的事情,
我只是想知道我们是否可以一次性备份整个VPC,而不是使用python编程语言单独备份所有资源。
我正在尝试运行一个清除 AWS SQS 队列的函数,但我不断收到此错误:
DataNotFoundError(data_path = name)botocore.exceptions.DataNotFoundError:无法加载数据:sqs
我已经安装了python 3.7、boto3-1.21.22、botocore-1.24.22。我也跑了pip install --upgrade botocore,但仍然遇到同样的错误。
sqs_client = boto3.client('sqs', AWS_DEFAULT_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
def purge_queue(queue_url):
try:
response = sqs_client.purge_queue(QueueUrl=queue_url)
except ClientError as e:
logger.exception("Unexpected exception! %s", e)
raise
else:
return response
Run Code Online (Sandbox Code Playgroud) 我在 dynamodb 表上使用过滤器。它会抛出以下错误。Boto3 文档显示 response = table.scan(FilterExpression=Attr('myattribute').eq('myvalue')
我也做了同样的事情。我想要此表中的项目,其中 agentRole = Receiver
Response
{
"errorMessage": "name 'Attr' is not defined",
"errorType": "NameError",
"requestId": "1b2fbee6-5fa2-4951-8689-3d1bfec76e5c",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 21, in lambda_handler\n
response = tableresource.scan(FilterExpression=Attr('agentRole').eq('Receiver'))\n"
]
}
Run Code Online (Sandbox Code Playgroud)
这是代码:
import json
import os
import boto3
from pprint import pprint
#Find records that has agentRole as 'Receiver'
tableName = os.environ.get('TABLE')
fieldName = os.environ.get('FIELD')
keytofind = os.environ.get('FILTER')
fieldname = "agentRole"
dbclient = boto3.resource('dynamodb')
def lambda_handler(event, context):
tableresource = dbclient.Table(tableName)
count = tableresource.item_count
response …Run Code Online (Sandbox Code Playgroud) 我想用python脚本创建一个弹性ip。它在文档中找不到方法。
我想将文件和文件夹从一个 s3 存储桶复制到另一个存储桶。我无法通过阅读文档找到解决方案。只能从 s3 存储桶复制文件,但不能复制文件夹。这是我的代码:
import boto3
s3 = boto3.resource('s3')
copy_source = {
'Bucket': 'mybucket',
'Key': 'mykey'
}
s3.meta.client.copy(copy_source, 'otherbucket', 'otherkey')
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 boto3 与我的 AWS 基础设施进行交互。当我直接从 python CLI (v 3.6.1) 使用 boto3 时,以下代码正在工作:
import boto3
client = boto3.ressource('iam')
print(client.User('myusername').arn)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试从 python 脚本文件 (test.py) 使用它,我会收到以下错误:
$ python test.py
Traceback (most recent call last):
File "test.py", line 5, in <module>
client = boto3.ressource('iam')
AttributeError: 'module' object has no attribute 'ressource'
Run Code Online (Sandbox Code Playgroud)
提前谢谢你
当我执行此代码时,它返回 None,但我想像循环中那样打印整数,有人知道为什么吗?
import boto3
client = boto3.client('autoscaling')
clearesponse = client.describe_auto_scaling_groups(
AutoScalingGroupNames=[
'XXXX',
])
for reservation in clearesponse ['AutoScalingGroups']:
test=print(reservation['DesiredCapacity'])
nb_desired_cap = test
print (nb_desired_cap)
print (test)
Run Code Online (Sandbox Code Playgroud)
跑步 :
python 描述实例.py
4
没有任何
没有任何
运行代码并希望在其他变量中捕获结果 4 以用于代码的其他部分
boto3 ×12
python ×10
amazon-ec2 ×2
amazon-s3 ×2
python-3.x ×2
amazon-elb ×1
amazon-vpc ×1
autoscaling ×1
aws-backup ×1
aws-lambda ×1
boto ×1
botocore ×1
loops ×1
metadata ×1
python-2.7 ×1
variables ×1