先決條件

步驟

  1. 創建 S3 存儲桶
  1. 為 Lambda 創建 IAM 角色
  1. 創建 Lambda 函數
  1. 配置 Lambda 函數
import json
import boto3

def lambda_handler(event, context):
# Extract image prompt from the event
prompt = event["prompt"]

# Initialize S3 client
s3_client = boto3.client('s3')

# Configure Bedrock client (replace with your credentials)
bedrock_client = boto3.client('bedrock',
endpoint_url="<Bedrock_Endpoint_URL>",
aws_access_key_id="<Your_Access_Key_ID>",
aws_secret_access_key="<Your_Secret_Access_Key>")

# Generate image using Stability Diffusion model
response = bedrock_client.invoke_model(
model_id="stability-diffusion", # Replace with specific model ID if needed
prompt=prompt
)

# Extract image data from response
image_data = base64.b64decode(response["image"])

# Generate image filename based on timestamp
filename = f"image_{round(time.time())}.jpg"

# Upload image to S3 bucket
s3_client.put_object(Body=image_data, Bucket="<Your_Bucket_Name>", Key=filename)

# Return success message with image location
return {
"statusCode": 200,
"body": json.dumps(f"Image generated and stored in S3: s3://<Your_Bucket_Name>/{filename}")
}
import json
import boto3

def lambda_handler(event, context):
# Extract image prompt from the event
prompt = event["prompt"]

# Initialize S3 client
s3_client = boto3.client('s3')

# Configure Bedrock client (replace with your credentials)
bedrock_client = boto3.client('bedrock',
endpoint_url="<Bedrock_Endpoint_URL>",
aws_access_key_id="<Your_Access_Key_ID>",
aws_secret_access_key="<Your_Secret_Access_Key>")

# Generate image using Stability Diffusion model
response = bedrock_client.invoke_model(
model_id="stability-diffusion", # Replace with specific model ID if needed
prompt=prompt
)

# Extract image data from response
image_data = base64.b64decode(response["image"])

# Generate image filename based on timestamp
filename = f"image_{round(time.time())}.jpg"

# Upload image to S3 bucket
s3_client.put_object(Body=image_data, Bucket="<Your_Bucket_Name>", Key=filename)

# Return success message with image location
return {
"statusCode": 200,
"body": json.dumps(f"Image generated and stored in S3: s3://<Your_Bucket_Name>/{filename}")
}
  1. 配置功能設置
  1. 測試 Lambda 函數

原文鏈接:https://dzone.com/articles/building-powerful-ai-applications-with-amazon

上一篇:

最全AI數字人API推薦,不一樣的數字體驗

下一篇:

人工智能可最大化日志價值的 8 種方法
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費