How to Run Aspose.CAD Docker image in Azure Function

Prerequisites

  • Docker must be installed on your system. For information on how to install Docker on Windows or Mac, refer to the links in the “See Also” section.
  • IntelliJ IDEA.
  • Azure Toolkit for IntelliJ.
  • Postman.

Azure Function

In this example, you create a simple conversion function that converts a CAD file and saves it as an image. The application can then be built in Docker and run in Azure Function.

Creating the Azure Function

To create the Azure Function program, follow the steps below:

  1. Once Docker is installed, make sure that it uses Linux Containers (default). If necessary, select the Switch to Linux containers option from the Docker Desktops menu.

  2. Create Azure Function project in IntelliJ IDEA.
    Create azure function project
    Create azure function project-final

  3. Tools->Azure->Sing In and select OAuth 2.0 authentication.
    Azure sign In

  4. Log in browser.

  5. Select Subscription name.

  6. Add docker support.
    Azure sign In

  7. Edit the DockerFile as in the Configuring a Dockerfile section.

  8. Add blocks for repository aspose.cad in pom.xml.

    <repositories>
        <repository>
    		<id>AsposeJavaAPI</id>
            <name>Aspose Java API</name>
            <url>https://releases.aspose.com/java/repo/</url>
        </repository>
    </repositories>
    
    
    <dependencies>
     <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-cad</artifactId>
        <version>22.3</version>
        <scope>compile</scope>
      </dependency>
    </dependencies>

  9. When all required dependencies are added, write a simple program that creates an ellipse and saves it as an image:

    public class HttpTriggerFunction {
        /**
         * This function listens at endpoint "/api/HttpExample". Two ways to invoke it using "curl" command in bash:
         * 1. curl -d "HTTP Body" {your host}/api/HttpExample
         * 2. curl "{your host}/api/HttpExample?name=HTTP%20Query"
         */
        @FunctionName("HttpExample")
        public HttpResponseMessage run(
                @HttpTrigger(
                    name = "req",
                    methods = {HttpMethod.GET, HttpMethod.POST},
                    authLevel = AuthorizationLevel.ANONYMOUS)
                    HttpRequestMessage<Optional<String>> request,
                final ExecutionContext context) throws FileNotFoundException {
            context.getLogger().info("Java HTTP trigger processed a request.");
    
            try{
                String body = request.getBody().get();
                InputStream targetStream = new ByteArrayInputStream(body.getBytes());
    
                CadImage image = (CadImage)Image.load(targetStream);
                {
                    CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
                    rasterizationOptions.setPageWidth(1200);
                    rasterizationOptions.setPageHeight(1200);
    
                    ImageOptionsBase options = new PngOptions();
                    options.setVectorRasterizationOptions(rasterizationOptions);
    
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
    
                    image.save(out, options);
    
                    return request.createResponseBuilder(HttpStatus.OK)
                            .header("Content-type", "image/png")
                            .header("Content-Disposition", "attachment;filename=filename.png")
                            .body(out.toByteArray()).build();
                }
            }
            catch (Exception e)
    		{
                return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body(e.getMessage()).build();
            }
        }
    }

Configuring a Dockerfile

The next step is to create and configure the Dockerfile in root project folder.

  1. In the Dockerfile, specify:
    FROM mcr.microsoft.com/azure-functions/java:3.0-java8-build AS installer-env
    
    COPY . /src/java-function-app
    RUN cd /src/java-function-app && \
        mkdir -p /home/site/wwwroot && \
        mvn clean package && \
        cd ./target/azure-functions/ && \
        cd $(ls -d */|head -n 1) && \
        cp -a . /home/site/wwwroot
    
    FROM mcr.microsoft.com/azure-functions/java:3.0-java8-appservice
    
    ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
        AzureFunctionsJobHost__Logging__Console__IsEnabled=true
    
    COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

The above is a simple Dockerfile, which contains the following instructions:

  • The SDK image to be used. Docker will download it when the build is run. The version of SDK is specified as a tag.
  • The working directory, which is specified in the next line.
  • The command to copy everything to container, publish the application, and specify the entry point.

Docker Hub

  1. Login Docker Hub
  2. Create public Repository

Building and Running the Application in Docker

Now the application can be built and run in Docker. Open your favorite command prompt, change directory to the folder with the application (folder where the solution file and the Dockerfile are placed) and run the following command:

  1. Build dockerfile command in console

    //example
    docker build -t <user name>/<repository name> .
    
    docker build -t user/asposefunction .

  2. The first time you run this command, it may take longer because Docker needs to download the necessary images. After the previous command completes, run the following command to push the image to docker hub.

    //example
    docker push <user name>/<repository name>:tagname
    
    docker push user/aspose-cad-java:latest

  3. Run dockerfile in IDE and after push to docker hub.
    Run docker in ide

  4. Enter the name of the image, as in the Docker HUb repository.
    Run docker in ide-next

  5. Wait for the end.

Azure

  1. Login Azure.
  2. Choose Azure services.
  3. Choose Function App and create a function.
    Azure create function button
  4. Repeat the basic settings as in the image below.
    Azure create function settings
  5. Click ‘Review + create’ -> Create.
  6. Wait for deployment to finish.
  7. Click ‘Go to resorce’ button.
    Resource button
  8. Stop aspose-cad-docker-example function.
    Stop conteiner
  9. Go to the deployment center menu and make the appropriate settings.
    Deployment center
  10. Save settings
  11. Copy Webhook URL from deployment center settings.
    Webhook url
  12. Go to Docker Hub, select your repository and select webhooks.
  13. Paste the ‘Webhook url’ from Azure into the Docker Hub webhook url and set the name.
    Webhook settings in docker
  14. Click create button.
  15. Return to overview azure function and start container.
    Overview menu

Execution example

  1. Postman settings.
    Overview menu
  2. Select any DXF, DWG, DGN, DWF, DWFX, IFC, STL, DWT, IGES, PLT, CF2, OBJ, HPGL, IGS, PCL, FBX, PDF, SVG file.
  3. Click the send button.
  4. Save result Save responce

More Examples

For more samples of how you can use Aspose.CAD in Docker, see the examples.

See Also