
API 版本控制策略的 4 個最佳實踐
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}")
}
<Bedrock_Endpoint_URL>
:替換為您所在地區特定的 Bedrock 端點 URL。<Your_Access_Key_ID>
:替換為您的 AWS 訪問密鑰 ID。<Your_Secret_Access_Key>
:用您的 AWS 秘密訪問密鑰替換(安全存儲)。<Your_Bucket_Name>
:替換為您的 S3 存儲桶的名稱。原文鏈接:https://dzone.com/articles/building-powerful-ai-applications-with-amazon