@@ -0,0 +1,3279 @@
+//snippet-sourcedescription:%5BCreateFunction.java demonstrates how to create a CloudFront function.%5D%0A//snippet-keyword:%5BAWS SDK for Java v2%5D%0A//snippet-keyword:%5BCode Sample%5D%0A//snippet-keyword:%5BAmazon CloudFront%5D%0A//snippet-sourcetype:%5Bfull-example%5D%0A//snippet-sourcedate:%5B02/20/2021%5D%0A//snippet-sourceauthor:%5Bscmacdon AWS%5D%0A/*%0A   Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.%0A   SPDX-License-Identifier: Apache-2.0%0A*/%0A%0A%0Apackage com.example.cloudfront;%0A%0Aimport software.amazon.awssdk.core.SdkBytes;%0Aimport software.amazon.awssdk.regions.Region;%0Aimport software.amazon.awssdk.services.cloudfront.CloudFrontClient;%0Aimport software.amazon.awssdk.services.cloudfront.model.FunctionConfig;%0Aimport software.amazon.awssdk.services.cloudfront.model.FunctionRuntime;%0Aimport software.amazon.awssdk.services.cloudfront.model.CreateFunctionRequest;%0Aimport software.amazon.awssdk.services.cloudfront.model.CreateFunctionResponse;%0Aimport software.amazon.awssdk.services.cloudfront.model.CloudFrontException;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.InputStream;%0A%0A%0A/**%0A * To run this Java V2 code example, ensure that you have setup your development environment, including your credentials.%0A *%0A * For information, see this documentation topic:%0A *%0A * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html%0A */%0Apublic class CreateFunction %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A%0A        final String USAGE = %22%5Cn%22 +%0A                %22Usage:%5Cn%22 +%0A                %22    %3CfunctionName%3E %5Cn%5Cn%22 +%0A                %22Where:%5Cn%22 +%0A                %22    functionName - the name of the function to delete. %5Cn%22;%0A%0A         if (args.length != 1) %7B%0A             System.out.println(USAGE);%0A             System.exit(1);%0A         %7D%0A%0A        String functionName = args%5B0%5D;%0A        String filePath = args%5B1%5D ;%0A        CloudFrontClient cloudFrontClient = CloudFrontClient.builder()%0A                .region(Region.AWS_GLOBAL)%0A                .build();%0A%0A        String funArn = createNewFunction(cloudFrontClient, functionName, filePath);%0A        System.out.println(%22The function ARN is %22+funArn);%0A        cloudFrontClient.close();%0A    %7D%0A%0A    public static String createNewFunction(CloudFrontClient cloudFrontClient, String functionName, String filePath) %7B%0A%0A        try %7B%0A%0A            InputStream is = new FileInputStream(filePath);%0A            SdkBytes functionCode = SdkBytes.fromInputStream(is);%0A%0A            FunctionConfig config = FunctionConfig.builder()%0A                    .comment(%22Created by using the CloudFront Java API%22)%0A                    .runtime(FunctionRuntime.CLOUDFRONT_JS_1_0)%0A                    .build();%0A%0A            CreateFunctionRequest functionRequest = CreateFunctionRequest.builder()%0A                    .name(functionName)%0A                    .functionCode(functionCode)%0A                    .functionConfig(config)%0A                    .build();%0A%0A            CreateFunctionResponse response = cloudFrontClient.createFunction(functionRequest);%0A            return response.functionSummary().functionMetadata().functionARN();%0A%0A        %7D catch (CloudFrontException %7C FileNotFoundException e)%7B%0A            System.err.println(e.getMessage());%0A            System.exit(1);%0A        %7D%0A        return %22%22;%0A    %7D%0A%7D%0A%0A

Create CreateFunction.java