我正在使用Localstack在本地测试我的更改。我的Lambda函数应该执行putObject并在S3存储桶中创建对象。当直接在AWS环境中进行测试时,功能正常工作。但在Localstack中,它无法工作。我收到以下错误:
无法连接到端点URL:"http://localhost:4572/doyouknowme/pokemon.jpeg" raise EndpointConnectionError(endpoint_url=request.url, error=e)ponset_exceptionlhost:4572/doyouknowme/pokemon.jpeg"
AWS凭证:
[default]
aws_access_key_id = AKI****************
aws_secret_access_key = gL************************
region = us-east-1
Lambda函数代码:
import json
import urllib.parse
import boto3
import base64
print('Loading function')
# session = boto3.Session(profile_name='personal')
# s3 = session.client('s3', endpoint_url='http://localhost:4574')
s3 = boto3.client('s3', endpoint_url='http://localhost:4572', region_name='us-east-1')
def lambda_handler(event, context):
# raise Exception('Something went wrong')
print("Received event: " + json.dumps(event, indent=2))
try:
image_data = base64.b64decode(event['image_data'])
response = s3.put_object(
Body=image_data,
Bucket='doyouknowme',
Key='pokemon.jpeg',
ContentType='image/jpeg'
)
print(response)
return response
except Exception as e:
print(e)
# print(
# 'Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as '
# 'this function.'.format(
# key, bucket))
raise e
我不确定为什么S3密钥被附加到Lambda访问的端点URL上。感谢您的帮助解决这个问题。
http://%s:4566" % os.environ["LOCALSTACK_HOSTNAME"]
和http://localhost:4566
,但它们都没有起作用。无法将其与此处的命令列表协调一致:https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-invoke.html - TheSoldier