我试图使用Boto3将文件上传到s3并将上传的文件公开并将其作为网址返回.
class UtilResource(BaseZMPResource):
class Meta(BaseZMPResource.Meta):
queryset = Configuration.objects.none()
resource_name = 'util_resource'
allowed_methods = ['get']
def post_list(self, request, **kwargs):
fileToUpload = request.FILES
# write code to upload to amazone s3
# see: https://boto3.readthedocs.org/en/latest/reference/services/s3.html
self.session = Session(aws_access_key_id=settings.AWS_KEY_ID,
aws_secret_access_key=settings.AWS_ACCESS_KEY,
region_name=settings.AWS_REGION)
client = self.session.client('s3')
client.upload_file('zango-static','fileToUpload')
url = "some/test/url"
return self.create_response(request, {
'url': url // return's public url of uploaded file
})
Run Code Online (Sandbox Code Playgroud)
我搜索了整个文档,我找不到任何链接,描述如何做到这一点,有人可以解释或提供任何资源,我可以找到灵魂?
我正在努力找出如何从我的代码动态获取我的aws_access_key_id和aws_secret_access_key.
在boto2中,我可以执行以下操作:boto.config.get_value('Credentials', 'aws_secret_access_key')但我似乎无法在boto3中找到类似的方法.如果我看到的话,我能够找到钥匙,boto3.Session()._session._credentials但这对我来说似乎是所有黑客的母亲,我宁愿不去那条路.
我正在尝试发布到SNS主题,然后通知Lambda函数以及SQS队列.我的Lambda函数会被调用,但CloudWatch会记录我的"event"对象是None.boto3文档声明使用kwarg MessageStructure ='json'但会抛出ClientError.
希望我已经提供了足够的信息.
示例代码:
import json
import boto3
message = {"foo": "bar"}
client = boto3.client('sns')
response = client.publish(
TargetArn=arn,
Message=json.dumps(message)
)
Run Code Online (Sandbox Code Playgroud) 该文件建议使用该消息属性,但我似乎无法找出什么属性名称使用.
这项工作到目前为止:
sns = boto3.client('sns', region_name='eu-west-1')
sns.publish(
PhoneNumber='+491701234567',
Message='hi there',
MessageAttributes={
'AWS.SNS.SMS.SenderID': {
'DataType': 'String',
'StringValue': 'MySenderID'
}
}
)
Run Code Online (Sandbox Code Playgroud)
SMS已发送,但发件人ID字段中包含一些(随机?)值.所以我的消息属性设置似乎被忽略了.设置自定义发件人ID的正确方法是什么?
当我试图从表中获取项目时,它会打印此错误
botocore.exceptions.ClientError:调用GetItem操作时发生错误(ValidationException):提供的键元素与架构不匹配
这是我的代码
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('testDynamodb')
response = table.get_item(Key={'userId': "user2873"})
item = response['Item']
print(item)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢.
如何检查 S3 中的特定目录中是否存在特定文件?我使用 Boto3 并尝试了这段代码(不起作用):
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
key = 'dootdoot.jpg'
objs = list(bucket.objects.filter(Prefix=key))
if len(objs) > 0 and objs[0].key == key:
print("Exists!")
else:
print("Doesn't exist")
Run Code Online (Sandbox Code Playgroud) 如果我有一个带有userId的散列键和productId的范围键的表,那么只有在使用boto3的dynamodb绑定不存在的情况下,如何才能将该项放入该表中?
对put_item的正常调用如下所示
table.put_item(Item={'userId': 1, 'productId': 2})
Run Code Online (Sandbox Code Playgroud)
我使用ConditionExpression的调用如下所示:
table.put_item(
Item={'userId': 1, 'productId': 2},
ConditionExpression='userId <> :uid AND productId <> :pid',
ExpressionAttributeValues={':uid': 1, ':pid': 3}
)
Run Code Online (Sandbox Code Playgroud)
但是每次都会引发ConditionalCheckFailedException.项目是否存在具有相同productId的项目.
我正在使用boto3并尝试上传文件.这将是有益的,如果有人会解释之间的精确差异file_upload()和put_object()S3存储方法在boto3 ?
是否可以在python中使用boto3创建ec2实例?Boto3文档在这里没有帮助,我在网上找不到任何帮助文档.请提供一些示例代码/链接.
我正在使用 Lambda Python 3.9 运行时。我还在 Lambda 中使用 boto3 和 botocore 默认包。
今天,我突然收到此错误:“无法从‘botocore.docs’导入名称‘DEPRECATED_SERVICE_NAMES’””。当我将 botocore 包添加到 lambda 运行时时,我才成功修复了它。我想避免它,因为它会使层的大小增加 10 MB。
有什么帮助吗?谢谢
boto3 ×10
python ×6
amazon-s3 ×3
amazon-sns ×2
python-3.x ×2
amazon-ec2 ×1
aws-lambda ×1
boto ×1
lambda ×1
python-2.7 ×1
sms ×1