Hel*_*lad 12 amazon-s3 amazon-web-services boto3 aws-lambda
我在aws lambda中使用boto3来查找位于法兰克福地区的S3中的fecth对象.
v4是必要的.否则将返回错误
"errorMessage": "An error occurred (InvalidRequest) when calling
the GetObject operation: The authorization mechanism you have
provided is not supported. Please use AWS4-HMAC-SHA256."
Run Code Online (Sandbox Code Playgroud)
实现了配置signature_version的方法http://boto3.readthedocs.org/en/latest/guide/configuration.html
但由于我使用的是AWS lambda,因此我无法访问底层配置文件
我的AWS lambda函数的代码
from __future__ import print_function
import boto3
def lambda_handler (event, context):
input_file_bucket = event["Records"][0]["s3"]["bucket"]["name"]
input_file_key = event["Records"][0]["s3"]["object"]["key"]
input_file_name = input_file_bucket+"/"+input_file_key
s3=boto3.resource("s3")
obj = s3.Object(bucket_name=input_file_bucket, key=input_file_key)
response = obj.get()
return event #echo first key valuesdf
Run Code Online (Sandbox Code Playgroud)
可以在此代码中配置signature_version吗?以Session为例.或者有什么解决方法吗?
omu*_*thu 19
而不是使用默认会话,尝试使用boto3.session中的自定义会话和配置
import boto3
import boto3.session
session = boto3.session.Session(region_name='eu-central-1')
s3client = session.client('s3', config= boto3.session.Config(signature_version='s3v4'))
s3client.get_object(Bucket='<Bkt-Name>', Key='S3-Object-Key')
Run Code Online (Sandbox Code Playgroud)
小智 6
我尝试了会话方法,但是遇到了问题。这种方法对我来说效果更好,您的里程可能会有所不同:
s3 = boto3.resource('s3', config=Config(signature_version='s3v4'))
Run Code Online (Sandbox Code Playgroud)
您需要从botocore.client导入Config才能完成这项工作。有关测试存储桶(列出对象)的功能方法,请参见下文。假定您是在管理身份验证的环境中运行它,例如具有IAM角色的Amazon EC2或Lambda:
import boto3
from botocore.client import Config
from botocore.exceptions import ClientError
def test_bucket(bucket):
print 'testing bucket: ' + bucket
try:
s3 = boto3.resource('s3', config=Config(signature_version='s3v4'))
b = s3.Bucket(bucket)
objects = b.objects.all()
for obj in objects:
print obj.key
print 'bucket test SUCCESS'
except ClientError as e:
print 'Client Error'
print e
print 'bucket test FAIL'
Run Code Online (Sandbox Code Playgroud)
要对其进行测试,只需使用存储桶名称调用该方法。您的角色将必须授予适当的权限。
| 归档时间: |
|
| 查看次数: |
7610 次 |
| 最近记录: |