Skip to content

Commit 5cf1bad

Browse files
Merge pull request #267950 from pauljewellmsft/azd-java
Add azd support to Java quickstart for blobs
2 parents f6fb183 + eaef71c commit 5cf1bad

File tree

1 file changed

+175
-17
lines changed

1 file changed

+175
-17
lines changed

articles/storage/blobs/storage-quickstart-blobs-java.md

Lines changed: 175 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,42 @@ description: In this quickstart, you learn how to use the Azure Blob Storage cli
44
author: pauljewellmsft
55
ms.author: pauljewell
66
ms.custom: devx-track-java, mode-api, passwordless-java, devx-track-extended-java
7-
ms.date: 10/24/2022
7+
ms.date: 03/04/2024
88
ms.service: azure-blob-storage
99
ms.topic: quickstart
1010
ms.devlang: java
11+
zone_pivot_groups: azure-blob-storage-quickstart-options
1112
---
1213

1314
# Quickstart: Azure Blob Storage client library for Java
1415

15-
Get started with the Azure Blob Storage client library for Java to manage blobs and containers. Follow these steps to install the package and try out example code for basic tasks.
16+
::: zone pivot="blob-storage-quickstart-scratch"
17+
18+
> [!NOTE]
19+
> The **Build from scratch** option walks you step by step through the process of creating a new project, installing packages, writing the code, and running a basic console app. This approach is recommended if you want to understand all the details involved in creating an app that connects to Azure Blob Storage. If you prefer to automate deployment tasks and start with a completed project, choose [Start with a template](storage-quickstart-blobs-java.md?pivots=blob-storage-quickstart-template).
20+
21+
::: zone-end
22+
23+
::: zone pivot="blob-storage-quickstart-template"
24+
25+
> [!NOTE]
26+
> The **Start with a template** option uses the Azure Developer CLI to automate deployment tasks and starts you off with a completed project. This approach is recommended if you want to explore the code as quickly as possible without going through the setup tasks. If you prefer step by step instructions to build the app, choose [Build from scratch](storage-quickstart-blobs-java.md?pivots=blob-storage-quickstart-scratch).
27+
28+
::: zone-end
29+
30+
Get started with the Azure Blob Storage client library for Java to manage blobs and containers.
31+
32+
::: zone pivot="blob-storage-quickstart-scratch"
33+
34+
In this article, you follow steps to install the package and try out example code for basic tasks.
35+
36+
::: zone-end
37+
38+
::: zone pivot="blob-storage-quickstart-template"
39+
40+
In this article, you use the [Azure Developer CLI](/azure/developer/azure-developer-cli/overview) to deploy Azure resources and run a completed console app with just a few commands.
41+
42+
::: zone-end
1643

1744
> [!TIP]
1845
> If you're working with Azure Storage resources in a Spring application, we recommend that you consider [Spring Cloud Azure](/azure/developer/java/spring-framework/) as an alternative. Spring Cloud Azure is an open-source project that provides seamless Spring integration with Azure services. To learn more about Spring Cloud Azure, and to see an example using Blob Storage, see [Upload a file to an Azure Storage Blob](/azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-storage).
@@ -21,13 +48,28 @@ Get started with the Azure Blob Storage client library for Java to manage blobs
2148

2249
## Prerequisites
2350

24-
- Azure account with an active subscription - [create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio).
51+
::: zone pivot="blob-storage-quickstart-scratch"
52+
53+
- Azure account with an active subscription - [create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio)
2554
- Azure Storage account - [create a storage account](../common/storage-account-create.md).
26-
- [Java Development Kit (JDK)](/java/azure/jdk/) version 8 or above.
27-
- [Apache Maven](https://maven.apache.org/download.cgi).
55+
- [Java Development Kit (JDK)](/java/azure/jdk/) version 8 or above
56+
- [Apache Maven](https://maven.apache.org/download.cgi)
57+
58+
::: zone-end
59+
60+
::: zone pivot="blob-storage-quickstart-template"
61+
62+
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
63+
- [Java Development Kit (JDK)](/java/azure/jdk/) version 8 or above
64+
- [Apache Maven](https://maven.apache.org/download.cgi)
65+
- [Azure Developer CLI](/azure/developer/azure-developer-cli/install-azd)
66+
67+
::: zone-end
2868

2969
## Setting up
3070

71+
::: zone pivot="blob-storage-quickstart-scratch"
72+
3173
This section walks you through preparing a project to work with the Azure Blob Storage client library for Java.
3274

3375
### Create the project
@@ -173,6 +215,73 @@ public class App
173215
}
174216
```
175217

218+
::: zone-end
219+
220+
::: zone pivot="blob-storage-quickstart-template"
221+
222+
With [Azure Developer CLI](/azure/developer/azure-developer-cli/install-azd) installed, you can create a storage account and run the sample code with just a few commands. You can run the project in your local development environment, or in a [DevContainer](https://code.visualstudio.com/docs/devcontainers/containers).
223+
224+
### Initialize the Azure Developer CLI template and deploy resources
225+
226+
From an empty directory, follow these steps to initialize the `azd` template, provision Azure resources, and get started with the code:
227+
228+
- Clone the quickstart repository assets from GitHub and initialize the template locally:
229+
230+
```console
231+
azd init --template blob-storage-quickstart-java
232+
```
233+
234+
You'll be prompted for the following information:
235+
236+
- **Environment name**: This value is used as a prefix for all Azure resources created by Azure Developer CLI. The name must be unique across all Azure subscriptions and must be between 3 and 24 characters long. The name can contain numbers and lowercase letters only.
237+
238+
- Log in to Azure:
239+
240+
```console
241+
azd auth login
242+
```
243+
- Provision and deploy the resources to Azure:
244+
245+
```console
246+
azd up
247+
```
248+
249+
You'll be prompted for the following information:
250+
251+
- **Subscription**: The Azure subscription that your resources are deployed to.
252+
- **Location**: The Azure region where your resources are deployed.
253+
254+
The deployment might take a few minutes to complete. The output from the `azd up` command includes the name of the newly created storage account, which you'll need later to run the code.
255+
256+
## Run the sample code
257+
258+
At this point, the resources are deployed to Azure and the code is almost ready to run. Follow these steps to update the name of the storage account in the code, and run the sample console app:
259+
260+
- **Update the storage account name**:
261+
1. In the local directory, navigate to the *blob-quickstart/src/main/java/com/blobs/quickstart* directory.
262+
1. Open the file named **App.java** in your editor. Find the `<storage-account-name>` placeholder and replace it with the actual name of the storage account created by the `azd up` command.
263+
1. Save the changes.
264+
- **Run the project**:
265+
1. Navigate to the *blob-quickstart* directory containing the `pom.xml` file. Compile the project by using the following `mvn` command:
266+
```console
267+
mvn compile
268+
```
269+
1. Package the compiled code in its distributable format:
270+
```console
271+
mvn package
272+
```
273+
1. Run the following `mvn` command to execute the app:
274+
```console
275+
mvn exec:java
276+
```
277+
- **Observe the output**: This app creates a test file in your local *data* folder and uploads it to a container in the storage account. The example then lists the blobs in the container and downloads the file with a new name so that you can compare the old and new files.
278+
279+
To learn more about how the sample code works, see [Code examples](#code-examples).
280+
281+
When you're finished testing the code, see the [Clean up resources](#clean-up-resources) section to delete the resources created by the `azd up` command.
282+
283+
::: zone-end
284+
176285
## Object model
177286

178287
Azure Blob Storage is optimized for storing massive amounts of unstructured data. Unstructured data doesn't adhere to a particular data model or definition, such as text or binary data. Blob storage offers three types of resources:
@@ -203,10 +312,21 @@ These example code snippets show you how to perform the following actions with t
203312
- [List the blobs in a container](#list-the-blobs-in-a-container)
204313
- [Download blobs](#download-blobs)
205314
- [Delete a container](#delete-a-container)
206-
315+
316+
::: zone pivot="blob-storage-quickstart-scratch"
317+
207318
> [!IMPORTANT]
208319
> Make sure you have the correct dependencies in pom.xml and the necessary directives for the code samples to work, as described in the [setting up](#setting-up) section.
209320

321+
::: zone-end
322+
323+
::: zone pivot="blob-storage-quickstart-template"
324+
325+
> [!NOTE]
326+
> The Azure Developer CLI template includes a file with sample code already in place. The following examples provide detail for each part of the sample code. The template implements the recommended passwordless authentication method, as described in the [Authenticate to Azure](#authenticate-to-azure-and-authorize-access-to-blob-data) section. The connection string method is shown as an alternative, but isn't used in the template and isn't recommended for production code.
327+
328+
::: zone-end
329+
210330
### Authenticate to Azure and authorize access to blob data
211331

212332
[!INCLUDE [storage-quickstart-passwordless-auth-intro](../../../includes/storage-quickstart-passwordless-auth-intro.md)]
@@ -308,8 +428,12 @@ export AZURE_STORAGE_CONNECTION_STRING="<yourconnectionstring>"
308428

309429
The code below retrieves the connection string for the storage account from the environment variable created earlier, and uses the connection string to construct a service client object.
310430

431+
::: zone pivot="blob-storage-quickstart-scratch"
432+
311433
Add this code to the end of the `Main` method:
312434

435+
::: zone-end
436+
313437
```java
314438
// Retrieve the connection string for use with the application.
315439
String connectStr = System.getenv("AZURE_STORAGE_CONNECTION_STRING");
@@ -328,39 +452,45 @@ BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
328452

329453
### Create a container
330454

331-
Decide on a name for the new container. The code below appends a UUID value to the container name to ensure that it's unique.
455+
Create a new container in your storage account by calling the [createBlobContainer](/java/api/com.azure.storage.blob.blobserviceclient#method-details) method on the `blobServiceClient` object. In this example, the code appends a GUID value to the container name to ensure that it's unique.
332456

333-
> [!IMPORTANT]
334-
> Container names must be lowercase. For more information about naming containers and blobs, see [Naming and Referencing Containers, Blobs, and Metadata](/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata).
335-
336-
Next, create an instance of the [BlobContainerClient](/java/api/com.azure.storage.blob.blobcontainerclient) class, then call the [create](/java/api/com.azure.storage.blob.blobcontainerclient.create) method to actually create the container in your storage account.
457+
::: zone pivot="blob-storage-quickstart-scratch"
337458

338459
Add this code to the end of the `Main` method:
339460

461+
::: zone-end
462+
340463
:::code language="java" source="~/azure-storage-snippets/blobs/quickstarts/Java/blob-quickstart/src/main/java/com/blobs/quickstart/App.java" id="Snippet_CreateContainer":::
341464

342465
To learn more about creating a container, and to explore more code samples, see [Create a blob container with Java](storage-blob-container-create-java.md).
343466

467+
> [!IMPORTANT]
468+
> Container names must be lowercase. For more information about naming containers and blobs, see [Naming and Referencing Containers, Blobs, and Metadata](/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata).
469+
344470
### Upload blobs to a container
345471

346-
Add this code to the end of the `Main` method:
472+
Upload a blob to a container by calling the [uploadFromFile](/java/api/com.azure.storage.blob.blobclient.uploadfromfile) method. The example code creates a text file in the local *data* directory to upload to the container.
347473

348-
:::code language="java" source="~/azure-storage-snippets/blobs/quickstarts/Java/blob-quickstart/src/main/java/com/blobs/quickstart/App.java" id="Snippet_UploadBlobFromFile":::
474+
::: zone pivot="blob-storage-quickstart-scratch"
475+
476+
Add this code to the end of the `Main` method:
349477

350-
The code snippet completes the following steps:
478+
::: zone-end
351479

352-
1. Creates a text file in the local *data* directory.
353-
1. Gets a reference to a [BlobClient](/java/api/com.azure.storage.blob.blobclient) object by calling the [getBlobClient](/java/api/com.azure.storage.blob.blobcontainerclient.getblobclient) method on the container from the [Create a container](#create-a-container) section.
354-
1. Uploads the local text file to the blob by calling the [uploadFromFile](/java/api/com.azure.storage.blob.blobclient.uploadfromfile) method. This method creates the blob if it doesn't already exist, but won't overwrite it if it does.
480+
:::code language="java" source="~/azure-storage-snippets/blobs/quickstarts/Java/blob-quickstart/src/main/java/com/blobs/quickstart/App.java" id="Snippet_UploadBlobFromFile":::
355481

356482
To learn more about uploading blobs, and to explore more code samples, see [Upload a blob with Java](storage-blob-upload-java.md).
357483

358484
### List the blobs in a container
359485

360486
List the blobs in the container by calling the [listBlobs](/java/api/com.azure.storage.blob.blobcontainerclient.listblobs) method. In this case, only one blob has been added to the container, so the listing operation returns just that one blob.
361487

488+
::: zone pivot="blob-storage-quickstart-scratch"
489+
362490
Add this code to the end of the `Main` method:
363491

492+
::: zone-end
493+
364494
:::code language="java" source="~/azure-storage-snippets/blobs/quickstarts/Java/blob-quickstart/src/main/java/com/blobs/quickstart/App.java" id="Snippet_ListBlobs":::
365495

366496
To learn more about listing blobs, and to explore more code samples, see [List blobs with Java](storage-blobs-list-java.md).
@@ -369,8 +499,12 @@ To learn more about listing blobs, and to explore more code samples, see [List b
369499

370500
Download the previously created blob by calling the [downloadToFile](/java/api/com.azure.storage.blob.specialized.blobclientbase.downloadtofile) method. The example code adds a suffix of "DOWNLOAD" to the file name so that you can see both files in local file system.
371501

502+
::: zone pivot="blob-storage-quickstart-scratch"
503+
372504
Add this code to the end of the `Main` method:
373505

506+
::: zone-end
507+
374508
:::code language="java" source="~/azure-storage-snippets/blobs/quickstarts/Java/blob-quickstart/src/main/java/com/blobs/quickstart/App.java" id="Snippet_DownloadBlob":::
375509

376510
To learn more about downloading blobs, and to explore more code samples, see [Download a blob with Java](storage-blob-download-java.md).
@@ -381,12 +515,18 @@ The following code cleans up the resources the app created by removing the entir
381515

382516
The app pauses for user input by calling `System.console().readLine()` before it deletes the blob, container, and local files. This is a good chance to verify that the resources were created correctly, before they're deleted.
383517

518+
::: zone pivot="blob-storage-quickstart-scratch"
519+
384520
Add this code to the end of the `Main` method:
385521

522+
::: zone-end
523+
386524
:::code language="java" source="~/azure-storage-snippets/blobs/quickstarts/Java/blob-quickstart/src/main/java/com/blobs/quickstart/App.java" id="Snippet_DeleteContainer":::
387525

388526
To learn more about deleting a container, and to explore more code samples, see [Delete and restore a blob container with Java](storage-blob-container-delete-java.md).
389527

528+
::: zone pivot="blob-storage-quickstart-scratch"
529+
390530
## Run the code
391531

392532
This app creates a test file in your local folder and uploads it to Blob storage. The example then lists the blobs in the container and downloads the file with a new name so that you can compare the old and new files.
@@ -446,10 +586,28 @@ Done
446586

447587
Before you begin the cleanup process, check your *data* folder for the two files. You can compare them and observe that they're identical.
448588

589+
::: zone-end
590+
449591
## Clean up resources
450592

593+
::: zone pivot="blob-storage-quickstart-scratch"
594+
451595
After you've verified the files and finished testing, press the **Enter** key to delete the test files along with the container you created in the storage account. You can also use [Azure CLI](storage-quickstart-blobs-cli.md#clean-up-resources) to delete resources.
452596

597+
::: zone-end
598+
599+
::: zone pivot="blob-storage-quickstart-template"
600+
601+
When you're done with the quickstart, you can clean up the resources you created by running the following command:
602+
603+
```console
604+
azd down
605+
```
606+
607+
You'll be prompted to confirm the deletion of the resources. Enter `y` to confirm.
608+
609+
::: zone-end
610+
453611
## Next steps
454612

455613
In this quickstart, you learned how to upload, download, and list blobs using Java.

0 commit comments

Comments
 (0)