共用方式為


How to Start Spring Boot with Azure Media Service

[Obsolete, any new application is expected to use AAD instead of account key]

In this article, I will walk you through creating a custom application using the spring initializr, and then access the Azure media service and finally deploy to Azure Web app.
In order to access Azure media service in a spring boot application, we can leverage azure-mediaservices-spring-boot-starter, though it is not available in Spring Initializr yet, we can still use Spring Initializr to get start. Here, I will select both of "Azure Storage" and "Web" dependencies to generate the template shown below.

Let's open the downloaded template in Eclipse, open pom.xml, remove the Azure Storage dependency and add azure-mediaservices-spring-boot-starter dependency shown below

In order to operate with Azure media service, we need the corresponding credential. Let's execute Powershell command Get-AzureRmMediaServiceKeys shown below to get the primary key:

Now, we can open application.properties file and add below properties with the primary key we get. If you would like to access the media service via service principal, please refer to Get started with the Java client SDK for Azure Media Services and sample as well.

 azure.mediaservices.account-name=media-services-account-name
azure.mediaservices.account-key=media-services-account-key

Add a controller with below source code, visit localhost:8080/Assets will return all the assets belongs to this media service account. Notice that the MediaContract is the contract to interact with the back end of media service and it supports such as create, delete, list operations. For a detailed introduction to the usage, please refer to Azure-SDK-for-java.

 @RestController
public class AssetsController {
  @Autowired
        private MediaContract mediaService;
   
    @RequestMapping("/Assets")
  public ListResult Assets()
      throws Exception {
           return mediaService.list(Asset.list());
    }
}

Lastly, let's follow Deploy a Spring Boot application to the cloud with Azure App Service to deploy the sample app to azure. Below is my web.config which will be placed into wwwroot folder.

 <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
        arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar %HOME%\site\wwwroot\webapps\mediatest.jar;">
    </httpPlatform>
  </system.webServer>
</configuration>