先決條件

步驟

  1. 創(chuàng)建 S3 存儲(chǔ)桶
  1. 為 Lambda 創(chuàng)建 IAM 角色
  1. 創(chuàng)建 Lambda 函數(shù)
  1. 配置 Lambda 函數(shù)
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. 配置功能設(shè)置
  1. 測(cè)試 Lambda 函數(shù)

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

上一篇:

最全AI數(shù)字人API推薦,不一樣的數(shù)字體驗(yàn)

下一篇:

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

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門(mén)場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)