Merge pull request #284 from roygara/newGetStarts

New get-started articles for Eclipse and IntelliJ IDEs
This commit is contained in:
Robert Outlaw 2017-11-28 16:59:05 -08:00 коммит произвёл GitHub
Родитель 902b3b9792 7dd4564493
Коммит 4dd23aefa3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 911 добавлений и 53 удалений

Просмотреть файл

@ -0,0 +1,419 @@
---
title: Get started with Azure for Java using Eclipse
description: Get started with basic use of the Azure libraries for Java with your own Azure subscription.
keywords: Azure, Java, SDK, API, authenticate, get-started
author: roygara
ms.author: v-rogara
manager: timlt
ms.date: 10/30/2017
ms.topic: get-started-article
ms.prod: azure
ms.technology: azure
ms.devlang: java
ms.service: multiple
---
# Get started with the Azure libraries using Eclipse
This guide walks you through setting up a development environment and using the Azure libraries for Java. You'll create a service principal to authenticate with Azure and run some sample code that creates and uses Azure resources in your subscription. Using Eclipse is optional for Java development with Azure. Any IDE that has Maven integration works. Alternatively, you can run your code from the commandline using Maven if you prefer not to use any IDE.
## Prerequisites
- An Azure account. If you don't have one, [get a free trial](https://azure.microsoft.com/free/)
- [Azure Cloud Shell](https://docs.microsoft.com/azure/cloud-shell/quickstart) or [Azure CLI 2.0](https://docs.microsoft.com/cli/azure/install-az-cli2).
- The latest stable version of [Eclipse](http://www.eclipse.org/downloads/)
## Set up authentication
Your Java application needs read and create permissions in your Azure subscription to run the sample code in this tutorial. Create a service principal and configure your application to run with its credentials. Service principals provide a way to create a non-interactive account associated with your identity to which you grant only the privileges your app needs to run.
[Create a service principal](/cli/azure/create-an-azure-service-principal-azure-cli) to grant your code permission to create and update resources in your subscription without using your account credentials directly. Make sure to capture the output. Provide a [secure password](https://docs.microsoft.com/azure/active-directory/active-directory-passwords-policy) in the password argument instead of `MY_SECURE_PASSWORD`. Your password must be 8 to 16 characters and match at least 3 out of the 4 following criteria:
* Include lowercase characters
* Include uppercase characters
* Include numbers
* Include one of the following symbols: @ # $ % ^ & * - _ ! + = [ ] { } | \ : , . ? / ` ~ “ ( ) ;
```azurecli-interactive
az ad sp create-for-rbac --name AzureJavaTest --password "MY_SECURE_PASSWORD"
```
Which gives you a reply in the following format:
```json
{
"appId": "a487e0c1-82af-47d9-9a0b-af184eb87646d",
"displayName": "AzureJavaTest",
"name": "http://AzureJavaTest",
"password": password,
"tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt"
}
```
Next, copy the following into a text file on your system:
```text
# sample management library properties file
subscription=ssssssss-ssss-ssss-ssss-ssssssssssss
client=cccccccc-cccc-cccc-cccc-cccccccccccc
key=kkkkkkkkkkkkkkkk
tenant=tttttttt-tttt-tttt-tttt-tttttttttttt
managementURI=https\://management.core.windows.net/
baseURL=https\://management.azure.com/
authURL=https\://login.windows.net/
graphURL=https\://graph.windows.net/
```
Replace the top four values with the following:
- subscription: use the *id* value from `az account show` in the Azure CLI 2.0.
- client: use the *appId* value from the output taken from a service principal output.
- key: use the *password* value from the service principal output.
- tenant: use the *tenant* value from the service principal output.
Save this file in a secure location on your system where your code can read it. You may use this file for future code so it's recommended to store it somewhere external to the application in this article.
Set an environment variable `AZURE_AUTH_LOCATION` with the full path to the authentication file in your shell.
```bash
export AZURE_AUTH_LOCATION=/Users/raisa/azureauth.properties
```
If you're working in a windows environment, add the variable to your system properties. Open PowerShell and, after replacing the second variable with the path to your file, enter the following command:
```powershell
[Environment]::SetEnvironmentVariable("AZURE_AUTH_LOCATION", "C:\<fullpath>\azureauth.properties", "Machine")
```
## Create a new Maven project
> [!NOTE]
> This guide uses Maven build tool to build and run the sample code, but other build tools such as Gradle also work with the Azure libraries for Java.
Open Eclipse, select **File** -> **New**. In the new window that appears open the Maven folder then select Maven Project.
Leave the selections on the next screen defaults, and select **Next**. Do the same for this screen regarding archetypes.
When you come to the screen asking for groupID, ArtifactID, etc. Enter "com.fabrikam" for the groupID and enter "AzureApp" for the artifactID.
Now, open the pom.xml file. Inside the `dependencies` tag add the following code:
```XML
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.1.jre8</version>
</dependency>
```
Now, save the pom.xml. This prompts Eclipse to download all the specified dependencies. This may take a moment.
## Install the azure toolkit for Eclipse
The [Azure toolkit](azure-toolkit-for-eclipse.md) is necessary if you're going to be deploying web apps or APIs programmatically but is not currently used for any other kinds of development. The following is a summary of the installation process. For detailed stpes, visit [Installing the Azure Toolkit for Eclipse](azure-toolkit-for-eclipse.md).
Select the **Help** menu and then select **Install New software**.
In the **Work with:** field enter `http://dl.microsoft.com/eclipse` and press enter.
Then, select the checkbox next to **Azure toolkit for Java** and uncheck the checkbox for **Contact all update sites during install to find required software**. Then select next.
## Create a Linux virtual machine
Create a new file named `AzureApp.java` in the project's `src/main/java` directory and paste in the following block of code. Update the `userName` and `sshKey` variables with real values for your machine. The code creates a new Linux VM with name `testLinuxVM` in a resource group `sampleResourceGroup` running in the US East Azure region.
In order to create an `sshkey`, open the azure cloud shell and enter `ssh-keygen -t rsa -b 2048`. Name the file and then access the .public file to get the key, which you use in the following code, copy and paste it all into your variable `sshKey`.
```java
package com.fabrikam.AzureApp;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.VirtualMachine;
import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage;
import com.microsoft.azure.management.compute.VirtualMachineSizeTypes;
import com.microsoft.azure.management.appservice.PricingTier;
import com.microsoft.azure.management.appservice.WebApp;
import com.microsoft.azure.management.storage.StorageAccount;
import com.microsoft.azure.management.storage.SkuName;
import com.microsoft.azure.management.storage.StorageAccountKey;
import com.microsoft.azure.management.sql.SqlDatabase;
import com.microsoft.azure.management.sql.SqlServer;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import com.microsoft.rest.LogLevel;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
public class AzureApp {
public static void main(String[] args) {
final String userName = "YOUR_VM_USERNAME";
final String sshKey = "YOUR_PUBLIC_SSH_KEY";
try {
// use the properties file with the service principal information to authenticate
// change the name of the environment variable if you used a different name in the previous step
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
// create a Ubuntu virtual machine in a new resource group
VirtualMachine linuxVM = azure.virtualMachines().define("testLinuxVM")
.withRegion(Region.US_EAST)
.withNewResourceGroup("sampleVmResourceGroup")
.withNewPrimaryNetwork("10.0.0.0/24")
.withPrimaryPrivateIPAddressDynamic()
.withoutPrimaryPublicIPAddress()
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
.withRootUsername(userName)
.withSsh(sshKey)
.withUnmanagedDisks()
.withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
.create();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
```
You see some REST requests and responses in the console as the SDK makes the underlying calls to the Azure REST API to configure the virtual machine and its resources. When the program finishes, verify the virtual machine in your subscription with the Azure CLI 2.0:
```azurecli-interactive
az vm list --resource-group sampleVmResourceGroup
```
Once you've verified that the code worked, use the CLI to delete the VM and its resources.
```azurecli-interactive
az group delete --name sampleVmResourceGroup
```
## Deploy a web app from a GitHub repo
Replace the main method in `AzureApp.java` with the one below, updating the `appName` variable to a unique value before running the code. This code deploys a web application from the `master` branch in a public GitHub repo into a new [Azure App Service Web App](https://docs.microsoft.com/azure/app-service-web/app-service-web-overview) running in the free pricing tier.
```java
public static void main(String[] args) {
try {
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
final String appName = "YOUR_APP_NAME";
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
WebApp app = azure.webApps().define(appName)
.withRegion(Region.US_WEST2)
.withNewResourceGroup("sampleWebResourceGroup")
.withNewWindowsPlan(PricingTier.FREE_F1)
.defineSourceControl()
.withPublicGitRepository(
"https://github.com/Azure-Samples/app-service-web-java-get-started")
.withBranch("master")
.attach()
.create();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
```
Run the code as before using Maven:
Open a browser pointed to the application using the CLI:
```azurecli-interactive
az appservice web browse --resource-group sampleWebResourceGroup --name YOUR_APP_NAME
```
Remove the web app and plan from your subscription once you've verified the deployment.
```azurecli-interactive
az group delete --name sampleWebResourceGroup
```
## Connect to an Azure SQL database
Replace the current main method in `AzureApp.java` with the code below, setting a real value for the `dbPassword` variable.
This code creates a new SQL database with a firewall rule allowing remote access, and then connects to it using the SQL Database JBDC driver.
```java
public static void main(String args[])
{
// create the db using the management libraries
try {
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
final String adminUser = SdkContext.randomResourceName("db",8);
final String sqlServerName = SdkContext.randomResourceName("sql",10);
final String sqlDbName = SdkContext.randomResourceName("dbname",8);
final String dbPassword = "YOUR_PASSWORD_HERE";
SqlServer sampleSQLServer = azure.sqlServers().define(sqlServerName)
.withRegion(Region.US_EAST)
.withNewResourceGroup("sampleSqlResourceGroup")
.withAdministratorLogin(adminUser)
.withAdministratorPassword(dbPassword)
.withNewFirewallRule("0.0.0.0","255.255.255.255")
.create();
SqlDatabase sampleSQLDb = sampleSQLServer.databases().define(sqlDbName).create();
// assemble the connection string to the database
final String domain = sampleSQLServer.fullyQualifiedDomainName();
String url = "jdbc:sqlserver://"+ domain + ":1433;" +
"database=" + sqlDbName +";" +
"user=" + adminUser+ "@" + sqlServerName + ";" +
"password=" + dbPassword + ";" +
"encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
// connect to the database, create a table and insert a entry into it
Connection conn = DriverManager.getConnection(url);
String createTable = "CREATE TABLE CLOUD ( name varchar(255), code int);";
String insertValues = "INSERT INTO CLOUD (name, code ) VALUES ('Azure', 1);";
String selectValues = "SELECT * FROM CLOUD";
Statement createStatement = conn.createStatement();
createStatement.execute(createTable);
Statement insertStatement = conn.createStatement();
insertStatement.execute(insertValues);
Statement selectStatement = conn.createStatement();
ResultSet rst = selectStatement.executeQuery(selectValues);
while (rst.next()) {
System.out.println(rst.getString(1) + " "
+ rst.getString(2));
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace().toString());
}
}
```
Run the sample from the command line:
```
mvn clean compile exec:java
```
Then clean up the resources using the CLI:
```azurecli-interactive
az group delete --name sampleSqlResourceGroup
```
## Write a blob into a new storage account
Replace the current main method in `AzureApp.java` with the code below. This code creates an [Azure storage account](https://docs.microsoft.com/azure/storage/storage-introduction) and then uses the Azure Storage libraries for Java to create a new text file in the cloud.
```java
public static void main(String[] args) {
try {
// use the properties file with the service principal information to authenticate
// change the name of the environment variable if you used a different name in the previous step
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
// create a new storage account
String storageAccountName = SdkContext.randomResourceName("st",8);
StorageAccount storage = azure.storageAccounts().define(storageAccountName)
.withRegion(Region.US_WEST2)
.withNewResourceGroup("sampleStorageResourceGroup")
.create();
// create a storage container to hold the files
List<StorageAccountKey> keys = storage.getKeys();
final String storageConnection = "DefaultEndpointsProtocol=https;"
+ "AccountName=" + storage.name()
+ ";AccountKey=" + keys.get(0).value()
+ ";EndpointSuffix=core.windows.net";
CloudStorageAccount account = CloudStorageAccount.parse(storageConnection);
CloudBlobClient serviceClient = account.createCloudBlobClient();
// Container name must be lower case.
CloudBlobContainer container = serviceClient.getContainerReference("helloazure");
container.createIfNotExists();
// Make the container public
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
container.uploadPermissions(containerPermissions);
// write a blob to the container
CloudBlockBlob blob = container.getBlockBlobReference("helloazure.txt");
blob.uploadText("hello Azure");
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
```
Run the sample from the command line:
You can browse for the `helloazure.txt` file in your storage account through the Azure portal or with [Azure Storage Explorer](https://docs.microsoft.com/azure/vs-azure-tools-storage-explorer-blobs).
Clean up the storage account using the CLI:
```azurecli-interactive
az group delete --name sampleStorageResourceGroup
```
## Explore more samples
To learn more about how to use the Azure management libraries for Java to manage resources and automate tasks, see our sample code for [virtual machines](../java-sdk-azure-virtual-machine-samples.md), [web apps](../java-sdk-azure-web-apps-samples.md) and [SQL database](../java-sdk-azure-sql-database-samples.md).
## Reference and release notes
A [reference](http://docs.microsoft.com/java/api) is available for all packages.
## Get help and give feedback
Post questions to the community on [Stack Overflow](http://stackoverflow.com/questions/tagged/azure+java). Report bugs and open issues against the Azure libraries for Java on the [project GitHub](https://github.com/Azure/azure-sdk-for-java).

Просмотреть файл

@ -0,0 +1,420 @@
---
title: Get started with Azure for Java using Intellij
description: Get started with basic use of the Azure libraries for Java with your own Azure subscription.
keywords: Azure, Java, SDK, API, authenticate, get-started
author: roygara
ms.author: v-rogara
manager: timlt
ms.date: 10/30/2017
ms.topic: get-started-article
ms.prod: azure
ms.technology: azure
ms.devlang: java
ms.service: multiple
---
# Get started with the Azure libraries using Intellij
This guide walks you through setting up a development environment and using the Azure libraries for Java. You'll create a service principal to authenticate with Azure and run some sample code that creates and uses Azure resources in your subscription. Using Intellij is optional for Java development with Azure. Any IDE that has Maven integration works. Alternatively, you can run your code from the commandline using Maven if you prefer not to use any IDE.
## Prerequisites
- An Azure account. If you don't have one, [get a free trial](https://azure.microsoft.com/free/)
- [Azure Cloud Shell](https://docs.microsoft.com/azure/cloud-shell/quickstart) or [Azure CLI 2.0](https://docs.microsoft.com/cli/azure/install-az-cli2).
- The latest stable version of [Intellij](https://www.jetbrains.com/idea/)
## Set up authentication
Your Java application needs read and create permissions in your Azure subscription to run the sample code in this tutorial. Create a service principal and configure your application to run with its credentials. Service principals provide a way to create a non-interactive account associated with your identity to which you grant only the privileges your app needs to run.
[Create a service principal](/cli/azure/create-an-azure-service-principal-azure-cli) to grant your code permission to create and update resources in your subscription without using your account credentials directly. Make sure to capture the output. Provide a [secure password](https://docs.microsoft.com/azure/active-directory/active-directory-passwords-policy) in the password argument instead of `MY_SECURE_PASSWORD`. Your password must be 8 to 16 characters and match at least 3 out of the 4 following criteria:
* Include lowercase characters
* Include uppercase characters
* Include numbers
* Include one of the following symbols: @ # $ % ^ & * - _ ! + = [ ] { } | \ : , . ? / ` ~ “ ( ) ;
```azurecli-interactive
az ad sp create-for-rbac --name AzureJavaTest --password "MY_SECURE_PASSWORD"
```
Which gives you a reply in the following format:
```json
{
"appId": "a487e0c1-82af-47d9-9a0b-af184eb87646d",
"displayName": "AzureJavaTest",
"name": "http://AzureJavaTest",
"password": password,
"tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt"
}
```
Next, copy the following into a text file on your system:
```text
# sample management library properties file
subscription=ssssssss-ssss-ssss-ssss-ssssssssssss
client=cccccccc-cccc-cccc-cccc-cccccccccccc
key=kkkkkkkkkkkkkkkk
tenant=tttttttt-tttt-tttt-tttt-tttttttttttt
managementURI=https\://management.core.windows.net/
baseURL=https\://management.azure.com/
authURL=https\://login.windows.net/
graphURL=https\://graph.windows.net/
```
Replace the top four values with the following:
- subscription: use the *id* value from `az account show` in the Azure CLI 2.0.
- client: use the *appId* value from the output taken from a service principal output.
- key: use the *password* value from the service principal output.
- tenant: use the *tenant* value from the service principal output.
Save this file in a secure location on your system where your code can read it. You may use this file for future code so it's recommended to store it somewhere external to the application in this article.
Set an environment variable `AZURE_AUTH_LOCATION` with the full path to the authentication file in your shell.
```bash
export AZURE_AUTH_LOCATION=/Users/raisa/azureauth.properties
```
If you're working in a windows environment, add the variable to your system properties. Open PowerShell and, after replacing the second variable with the path to your file, enter the following command:
```powershell
[Environment]::SetEnvironmentVariable("AZURE_AUTH_LOCATION", "C:\<fullpath>\azureauth.properties", "Machine")
```
## Create a new Maven project
> [!NOTE]
> This guide uses Maven build tool to build and run the sample code, but other build tools such as Gradle also work with the Azure libraries for Java.
Open Intellij, select File > New > Project... Then proceed to the next screen.
Enter "com.fabrikam" for the groupID and enter an artifactID of your choice.
Proceed to the final screen and finish creating the project.
Now, open the pom.xml file. And add the following code:
```XML
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.1.jre8</version>
</dependency>
</dependencies>
```
Save the pom.xml.
## Install the azure toolkit for Intellij
The [Azure toolkit](azure-toolkit-for-intellij-installation.md) is necessary if you're going to be deploying web apps or APIs programmatically but is not currently used for any other kinds of development. The following is a summary of the installation process. For detailed stpes, visit [Installing the Azure Toolkit for IntelliJ](azure-toolkit-for-intellij-installation.md).
Select the **File** menu and then select **Settings...**.
Select **Browse repositories...** and then search "Azure" and install the **Azure toolkit for Intellij**.
Restart Intellij.
## Create a Linux virtual machine
Create a new file named `AzureApp.java` in the project's `src/main/java` directory and paste in the following block of code. Update the `userName` and `sshKey` variables with real values for your machine. The code creates a new Linux VM with name `testLinuxVM` in a resource group `sampleResourceGroup` running in the US East Azure region.
In order to create an `sshkey`, open the azure cloud shell and enter `ssh-keygen -t rsa -b 2048`. Enter a name for your file and then access the .public file to get the key, which you use in the following code, copy and paste it all into your variable `sshKey`.
```java
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.VirtualMachine;
import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage;
import com.microsoft.azure.management.compute.VirtualMachineSizeTypes;
import com.microsoft.azure.management.appservice.PricingTier;
import com.microsoft.azure.management.appservice.WebApp;
import com.microsoft.azure.management.storage.StorageAccount;
import com.microsoft.azure.management.storage.SkuName;
import com.microsoft.azure.management.storage.StorageAccountKey;
import com.microsoft.azure.management.sql.SqlDatabase;
import com.microsoft.azure.management.sql.SqlServer;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import com.microsoft.rest.LogLevel;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
public class AzureApp {
public static void main(String[] args) {
final String userName = "YOUR_VM_USERNAME";
final String sshKey = "YOUR_PUBLIC_SSH_KEY";
try {
// use the properties file with the service principal information to authenticate
// change the name of the environment variable if you used a different name in the previous step
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
// create a Ubuntu virtual machine in a new resource group
VirtualMachine linuxVM = azure.virtualMachines().define("testLinuxVM")
.withRegion(Region.US_EAST)
.withNewResourceGroup("sampleVmResourceGroup")
.withNewPrimaryNetwork("10.0.0.0/24")
.withPrimaryPrivateIPAddressDynamic()
.withoutPrimaryPublicIPAddress()
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
.withRootUsername(userName)
.withSsh(sshKey)
.withUnmanagedDisks()
.withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
.create();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
```
You'll see some REST requests and responses in the console as the SDK makes the underlying calls to the Azure REST API to configure the virtual machine and its resources. When the program finishes, verify the virtual machine in your subscription with the Azure CLI 2.0:
```azurecli-interactive
az vm list --resource-group sampleVmResourceGroup
```
Once you've verified that the code worked, use the CLI to delete the VM and its resources.
```azurecli-interactive
az group delete --name sampleVmResourceGroup
```
## Deploy a web app from a GitHub repo
Replace the main method in `AzureApp.java` with the one below, updating the `appName` variable to a unique value before running the code. This code deploys a web application from the `master` branch in a public GitHub repo into a new [Azure App Service Web App](https://docs.microsoft.com/azure/app-service-web/app-service-web-overview) running in the free pricing tier.
```java
public static void main(String[] args) {
try {
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
final String appName = "YOUR_APP_NAME";
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
WebApp app = azure.webApps().define(appName)
.withRegion(Region.US_WEST2)
.withNewResourceGroup("sampleWebResourceGroup")
.withNewWindowsPlan(PricingTier.FREE_F1)
.defineSourceControl()
.withPublicGitRepository(
"https://github.com/Azure-Samples/app-service-web-java-get-started")
.withBranch("master")
.attach()
.create();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
```
Run the code as before using Maven:
Open a browser pointed to the application using the CLI:
```azurecli-interactive
az appservice web browse --resource-group sampleWebResourceGroup --name YOUR_APP_NAME
```
Remove the web app and plan from your subscription once you've verified the deployment.
```azurecli-interactive
az group delete --name sampleWebResourceGroup
```
## Connect to an Azure SQL database
Replace the current main method in `AzureApp.java` with the code below, setting a real value for the `dbPassword` variable.
This code creates a new SQL database with a firewall rule allowing remote access, and then connects to it using the SQL Database JBDC driver.
```java
public static void main(String args[])
{
// create the db using the management libraries
try {
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
final String adminUser = SdkContext.randomResourceName("db",8);
final String sqlServerName = SdkContext.randomResourceName("sql",10);
final String sqlDbName = SdkContext.randomResourceName("dbname",8);
final String dbPassword = "YOUR_PASSWORD_HERE";
SqlServer sampleSQLServer = azure.sqlServers().define(sqlServerName)
.withRegion(Region.US_EAST)
.withNewResourceGroup("sampleSqlResourceGroup")
.withAdministratorLogin(adminUser)
.withAdministratorPassword(dbPassword)
.withNewFirewallRule("0.0.0.0","255.255.255.255")
.create();
SqlDatabase sampleSQLDb = sampleSQLServer.databases().define(sqlDbName).create();
// assemble the connection string to the database
final String domain = sampleSQLServer.fullyQualifiedDomainName();
String url = "jdbc:sqlserver://"+ domain + ":1433;" +
"database=" + sqlDbName +";" +
"user=" + adminUser+ "@" + sqlServerName + ";" +
"password=" + dbPassword + ";" +
"encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
// connect to the database, create a table and insert a entry into it
Connection conn = DriverManager.getConnection(url);
String createTable = "CREATE TABLE CLOUD ( name varchar(255), code int);";
String insertValues = "INSERT INTO CLOUD (name, code ) VALUES ('Azure', 1);";
String selectValues = "SELECT * FROM CLOUD";
Statement createStatement = conn.createStatement();
createStatement.execute(createTable);
Statement insertStatement = conn.createStatement();
insertStatement.execute(insertValues);
Statement selectStatement = conn.createStatement();
ResultSet rst = selectStatement.executeQuery(selectValues);
while (rst.next()) {
System.out.println(rst.getString(1) + " "
+ rst.getString(2));
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace().toString());
}
}
```
Run the sample from the command line:
```
mvn clean compile exec:java
```
Then clean up the resources using the CLI:
```azurecli-interactive
az group delete --name sampleSqlResourceGroup
```
## Write a blob into a new storage account
Replace the current main method in `AzureApp.java` with the code below. This code creates an [Azure storage account](https://docs.microsoft.com/azure/storage/storage-introduction) and then uses the Azure Storage libraries for Java to create a new text file in the cloud.
```java
public static void main(String[] args) {
try {
// use the properties file with the service principal information to authenticate
// change the name of the environment variable if you used a different name in the previous step
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
// create a new storage account
String storageAccountName = SdkContext.randomResourceName("st",8);
StorageAccount storage = azure.storageAccounts().define(storageAccountName)
.withRegion(Region.US_WEST2)
.withNewResourceGroup("sampleStorageResourceGroup")
.create();
// create a storage container to hold the file
List<StorageAccountKey> keys = storage.getKeys();
final String storageConnection = "DefaultEndpointsProtocol=https;"
+ "AccountName=" + storage.name()
+ ";AccountKey=" + keys.get(0).value()
+ ";EndpointSuffix=core.windows.net";
CloudStorageAccount account = CloudStorageAccount.parse(storageConnection);
CloudBlobClient serviceClient = account.createCloudBlobClient();
// Container name must be lower case.
CloudBlobContainer container = serviceClient.getContainerReference("helloazure");
container.createIfNotExists();
// Make the container public
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
container.uploadPermissions(containerPermissions);
// write a blob to the container
CloudBlockBlob blob = container.getBlockBlobReference("helloazure.txt");
blob.uploadText("hello Azure");
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
```
Run the sample from the command line:
You can browse for the `helloazure.txt` file in your storage account through the Azure portal or with [Azure Storage Explorer](https://docs.microsoft.com/azure/vs-azure-tools-storage-explorer-blobs).
Clean up the storage account using the CLI:
```azurecli-interactive
az group delete --name sampleStorageResourceGroup
```
## Explore more samples
To learn more about how to use the Azure management libraries for Java to manage resources and automate tasks, see our sample code for [virtual machines](../java-sdk-azure-virtual-machine-samples.md), [web apps](../java-sdk-azure-web-apps-samples.md) and [SQL database](../java-sdk-azure-sql-database-samples.md).
## Reference and release notes
A [reference](http://docs.microsoft.com/java/api) is available for all packages.
## Get help and give feedback
Post questions to the community on [Stack Overflow](http://stackoverflow.com/questions/tagged/azure+java). Report bugs and open issues against the Azure libraries for Java on the [project GitHub](https://github.com/Azure/azure-sdk-for-java).

Просмотреть файл

@ -1,7 +1,7 @@
---
title: Get started with the Azure libraries for Java
description: Get started with basic use of the Azure libraries for Java with your own Azure subscription.
keywords: Azure, Java, SDK, API ,authenticate, get-started
keywords: Azure, Java, SDK, API, authenticate, get-started
author: rloutlaw
ms.author: routlaw
manager: douge
@ -16,11 +16,11 @@ ms.assetid: b1e10b79-f75e-4605-aecd-eed64873e2d3
# Get started with the Azure libraries for Java
This guide walks you through setting up a development environment with an Azure service principal and running sample code that creates and uses resources in your Azure subscription using the Azure libraries for Java.
This guide walks you through setting up a development environment with an Azure service principal. It also provides you with sample code that creates and uses resources in your Azure subscription using the Azure libraries for Java.
## Prerequisites
- An Azure account. If you don't have one , [get a free trial](https://azure.microsoft.com/free/)
- An Azure account. If you don't have one, [get a free trial](https://azure.microsoft.com/free/)
- [Azure Cloud Shell](https://docs.microsoft.com/azure/cloud-shell/quickstart) or [Azure CLI 2.0](https://docs.microsoft.com/cli/azure/install-az-cli2).
- [Java 8](https://www.azul.com/downloads/zulu/) (included in Azure Cloud Shell)
- [Maven 3](http://maven.apache.org/download.cgi) (included in Azure Cloud Shell)
@ -29,20 +29,27 @@ This guide walks you through setting up a development environment with an Azure
Your Java application needs read and create permissions in your Azure subscription to run the sample code in this tutorial. Create a service principal and configure your application to run with its credentials. Service principals provide a way to create a non-interactive account associated with your identity to which you grant only the privileges your app needs to run.
[Create a service principal using the Azure CLI 2.0](/cli/azure/create-an-azure-service-principal-azure-cli) and capture the output. You'll need to provide a [secure password](https://docs.microsoft.com/azure/active-directory/active-directory-passwords-policy) in the password argument instead of `MY_SECURE_PASSWORD`.
[Create a service principal using the Azure CLI 2.0](/cli/azure/create-an-azure-service-principal-azure-cli) and capture the output. Provide a [secure password](https://docs.microsoft.com/azure/active-directory/active-directory-passwords-policy) in the password argument instead of `MY_SECURE_PASSWORD`. Your password must be 8 to 16 characters and match at least 3 out of the 4 following criteria:
* Include lowercase characters
* Include uppercase characters
* Include numbers
* Include one of the following symbols: @ # $ % ^ & * - _ ! + = [ ] { } | \ : , . ? / ` ~ “ ( ) ;
```azurecli-interactive
az ad sp create-for-rbac --name AzureJavaTest --password "MY_SECURE_PASSWORD"
```
Which gives you a reply in the following format:
```json
{
"appId": "a487e0c1-82af-47d9-9a0b-af184eb87646d",
"displayName": "AzureJavaTest",
"name": "http://AzureJavaTest",
"password": password,
"tenant": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
"tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt"
}
```
@ -50,10 +57,10 @@ Next, copy the following into a text file on your system:
```text
# sample management library properties file
subscription=########-####-####-####-############
client=########-####-####-####-############
key=XXXXXXXXXXXXXXXX
tenant=########-####-####-####-############
subscription=ssssssss-ssss-ssss-ssss-ssssssssssss
client=cccccccc-cccc-cccc-cccc-cccccccccccc
key=kkkkkkkkkkkkkkkk
tenant=tttttttt-tttt-tttt-tttt-tttttttttttt
managementURI=https\://management.core.windows.net/
baseURL=https\://management.azure.com/
authURL=https\://login.windows.net/
@ -64,15 +71,23 @@ Replace the top four values with the following:
- subscription: use the *id* value from `az account show` in the Azure CLI 2.0.
- client: use the *appId* value from the output taken from a service principal output.
- key: use the *password* value from the service principal output .
- key: use the *password* value from the service principal output.
- tenant: use the *tenant* value from the service principal output.
Save this file in a secure location on your system where your code can read it. Set an environment variable `AZURE_AUTH_LOCATION` with the full path to the authentication file in your shell.
Save this file in a secure location on your system where your code can read it. You may use this file for future code so it's recommended to store it somewhere external to the application in this article.
Set an environment variable `AZURE_AUTH_LOCATION` with the full path to the authentication file in your shell.
```bash
export AZURE_AUTH_LOCATION=/Users/raisa/azureauth.properties
```
If you're working in a windows environment, add the variable to your system properties. Open PowerShell and, after replacing the second variable with the path to your file, enter the following command:
```powershell
[Environment]::SetEnvironmentVariable("AZURE_AUTH_LOCATION", "C:\<fullpath>\azureauth.properties", "Machine")
```
## Create a new Maven project
> [!NOTE]
@ -83,7 +98,7 @@ Create a Maven project from the command line in a new directory on your system:
```
mkdir java-azure-test
cd java-azure-test
mvn archetype:generate -DgroupId=com.fabrikam -DartifactId=testAzureApp \
mvn archetype:generate -DgroupId=com.fabrikam -DartifactId=AzureApp \
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
```
@ -128,7 +143,7 @@ Add a `build` entry under the top-level `project` element to use the [maven-exec
Create a new file named `AzureApp.java` in the project's `src/main/java` directory and paste in the following block of code. Update the `userName` and `sshKey` variables with real values for your machine. The code creates a new Linux VM with name `testLinuxVM` in a resource group `sampleResourceGroup` running in the US East Azure region.
```java
package com.fabrikam.testAzureApp;
package com.fabrikam.AzureApp;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.VirtualMachine;
@ -176,8 +191,8 @@ public class AzureApp {
.withRegion(Region.US_EAST)
.withNewResourceGroup("sampleVmResourceGroup")
.withNewPrimaryNetwork("10.0.0.0/24")
.withPrimaryPrivateIpAddressDynamic()
.withoutPrimaryPublicIpAddress()
.withPrimaryPrivateIPAddressDynamic()
.withoutPrimaryPublicIPAddress()
.withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
.withRootUsername(userName)
.withSsh(sshKey)
@ -263,7 +278,7 @@ Remove the web app and plan from your subscription once you've verified the depl
az group delete --name sampleWebResourceGroup
```
## Connect to a SQL database
## Connect to an Azure SQL database
Replace the current main method in `AzureApp.java` with the code below, setting a real value for the `dbPassword` variable.
This code creates a new SQL database with a firewall rule allowing remote access, and then connects to it using the SQL Database JBDC driver.
@ -346,52 +361,52 @@ az group delete --name sampleSqlResourceGroup
Replace the current main method in `AzureApp.java` with the code below. This code creates an [Azure storage account](https://docs.microsoft.com/azure/storage/storage-introduction) and then uses the Azure Storage libraries for Java to create a new text file in the cloud.
```java
public static void main(String[] args) {
public static void main(String[] args) {
try {
try {
// use the properties file with the service principal information to authenticate
// change the name of the environment variable if you used a different name in the previous step
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
// use the properties file with the service principal information to authenticate
// change the name of the environment variable if you used a different name in the previous step
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BASIC)
.authenticate(credFile)
.withDefaultSubscription();
// create a new storage account
String storageAccountName = SdkContext.randomResourceName("st",8);
StorageAccount storage = azure.storageAccounts().define(storageAccountName)
.withRegion(Region.US_WEST2)
.withNewResourceGroup("sampleStorageResourceGroup")
.create();
// create a new storage account
String storageAccountName = SdkContext.randomResourceName("st",8);
StorageAccount storage = azure.storageAccounts().define(storageAccountName)
.withRegion(Region.US_WEST2)
.withNewResourceGroup("sampleStorageResourceGroup")
.create();
// create a storage container to hold the file
List<StorageAccountKey> keys = storage.getKeys();
final String storageConnection = "DefaultEndpointsProtocol=https;"
+ "AccountName=" + storage.name()
+ ";AccountKey=" + keys.get(0).value()
+ ";EndpointSuffix=core.windows.net";
// create a storage container to hold the file
List<StorageAccountKey> keys = storage.getKeys();
final String storageConnection = "DefaultEndpointsProtocol=https;"
+ "AccountName=" + storage.name()
+ ";AccountKey=" + keys.get(0).value()
+ ";EndpointSuffix=core.windows.net";
CloudStorageAccount account = CloudStorageAccount.parse(storageConnection);
CloudBlobClient serviceClient = account.createCloudBlobClient();
CloudStorageAccount account = CloudStorageAccount.parse(storageConnection);
CloudBlobClient serviceClient = account.createCloudBlobClient();
// Container name must be lower case.
CloudBlobContainer container = serviceClient.getContainerReference("helloazure");
container.createIfNotExists();
// Container name must be lower case.
CloudBlobContainer container = serviceClient.getContainerReference("helloazure");
container.createIfNotExists();
// Make the container public
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
container.uploadPermissions(containerPermissions);
// Make the container public
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
container.uploadPermissions(containerPermissions);
// write a blob to the container
CloudBlockBlob blob = container.getBlockBlobReference("helloazure.txt");
blob.uploadText("hello Azure");
// write a blob to the container
CloudBlockBlob blob = container.getBlockBlobReference("helloazure.txt");
blob.uploadText("hello Azure");
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
```

Просмотреть файл

@ -16,6 +16,10 @@
href: /azure/cosmos-db/create-documentdb-java
- name: Get started with Java
href: java-sdk-azure-get-started.md
- name: Get started with Java using Eclipse
href: eclipse/java-sdk-azure-get-started-eclipse.md
- name: Get started with Java using Intellij
href: intellij/java-sdk-azure-get-started-intellij.md
- name: Tools
href: java-azure-tools.md
items: