AWS code-library documentation change
Summary
Added new Spring Boot example for generating videos with Amazon Bedrock using Nova-Reel model and S3 output
Security assessment
The change introduces a code example for a generative AI feature but does not address security vulnerabilities or document security controls. The hardcoded S3 bucket URI ('s3://mygeneratedvidoenovatest') could represent a minor security risk if used in production without proper access controls, but this is not explicitly addressed in the documentation.
Diff
diff --git a/code-library/latest/ug/java_2_bedrock-runtime_code_examples.md b/code-library/latest/ug/java_2_bedrock-runtime_code_examples.md index 59ca7bb3e..21608332b 100644 --- a//code-library/latest/ug/java_2_bedrock-runtime_code_examples.md +++ b//code-library/latest/ug/java_2_bedrock-runtime_code_examples.md @@ -72,0 +73,123 @@ The example also lists and displays the foundation models you have access to, al +The following code example shows how to a Spring Boot app that generates videos from text prompts using Amazon Bedrock and the Nova-Reel model. + +**SDK for Java 2.x** + + +###### Note + +There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/bedrock-runtime#code-examples). + +Generate videos from text prompts using Amazon Bedrock and Nova-Reel. + + + import org.springframework.stereotype.Service; + import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; + import software.amazon.awssdk.core.document.Document; + import software.amazon.awssdk.regions.Region; + import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient; + import software.amazon.awssdk.services.bedrockruntime.model.*; + + import java.util.concurrent.CompletableFuture; + + @Service + public class VideoGenerationService { + + public GenerateVideoResponse generateVideo(String prompt) { + + // add S3 bucket you want to store your generated videos + String s3Bucket = "s3://mygeneratedvidoenovatest"; + + + //Create json request as an instance of Document class + Document novaRequest = prepareDocument(prompt); + + // Create request + StartAsyncInvokeRequest request = StartAsyncInvokeRequest.builder() + .modelId("amazon.nova-reel-v1:0") + .modelInput(novaRequest) + .outputDataConfig(AsyncInvokeOutputDataConfig.builder() + .s3OutputDataConfig(AsyncInvokeS3OutputDataConfig.builder().s3Uri(s3Bucket).build()) + .build()) + .build(); + + try (BedrockRuntimeAsyncClient bedrockClient = getBedrockRuntimeAsyncClient()) { + CompletableFuture<StartAsyncInvokeResponse> startAsyncInvokeResponseCompletableFuture = bedrockClient.startAsyncInvoke(request); + + //blocking operation to wait for the AWS API response + StartAsyncInvokeResponse startAsyncInvokeResponse = startAsyncInvokeResponseCompletableFuture.get(); + System.out.println("invocation ARN: " + startAsyncInvokeResponse.invocationArn()); + + GenerateVideoResponse response = new GenerateVideoResponse(); + response.setStatus("inProgress"); + response.setExecutionArn(startAsyncInvokeResponse.invocationArn()); + + return response; + } catch (Exception e) { + System.out.println(e); + throw new RuntimeException(e); + } + + } + + public GenerateVideoResponse checkGenerationStatus(String invocationArn) { + GenerateVideoResponse response = new GenerateVideoResponse(); + + try (BedrockRuntimeAsyncClient bedrockClient = getBedrockRuntimeAsyncClient()) { + //creating async request to fetch status by invocation Arn + GetAsyncInvokeRequest asyncRequest = GetAsyncInvokeRequest.builder().invocationArn(invocationArn).build(); + + CompletableFuture<GetAsyncInvokeResponse> asyncInvoke = bedrockClient.getAsyncInvoke(asyncRequest); + + //blocking operation to wait for the AWS API response + GetAsyncInvokeResponse asyncInvokeResponse = asyncInvoke.get(); + System.out.println("Invocation status =" + asyncInvokeResponse.statusAsString()); + + response.setExecutionArn(invocationArn); + response.setStatus(asyncInvokeResponse.statusAsString()); + return response; + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + + } + + private static BedrockRuntimeAsyncClient getBedrockRuntimeAsyncClient() { + BedrockRuntimeAsyncClient bedrockClient = BedrockRuntimeAsyncClient.builder() + .region(Region.US_EAST_1) + .credentialsProvider(ProfileCredentialsProvider.create()) + .build(); + return bedrockClient; + } + + private static Document prepareDocument(String prompt) { + Document textToVideoParams = Document.mapBuilder() + .putString("text", prompt) + .build(); + + Document videoGenerationConfig = Document.mapBuilder() + .putNumber("durationSeconds", 6) + .putNumber("fps", 24) + .putString("dimension", "1280x720") + .build(); + + Document novaRequest = Document.mapBuilder() + .putString("taskType", "TEXT_VIDEO") + .putDocument("textToVideoParams", textToVideoParams) + .putDocument("videoGenerationConfig", videoGenerationConfig) + .build(); + return novaRequest; + } + } + + + + * For API details, see the following topics in _AWS SDK for Java 2.x API Reference_. + + * [GetAsyncInvoke](https://docs.aws.amazon.com/goto/SdkForJavaV2/bedrock-runtime-2023-09-30/GetAsyncInvoke) + + * [StartAsyncInvoke](https://docs.aws.amazon.com/goto/SdkForJavaV2/bedrock-runtime-2023-09-30/StartAsyncInvoke) + + + +