You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Quickstart: Azure Blob Storage client library for Java
14
15
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
16
43
17
44
> [!TIP]
18
45
> 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
21
48
22
49
## Prerequisites
23
50
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)
25
54
- Azure Storage account - [create a storage account](../common/storage-account-create.md).
26
-
-[Java Development Kit (JDK)](/java/azure/jdk/) version 8 or above.
This section walks you through preparing a project to work with the Azure Blob Storage client library for Java.
32
74
33
75
### Create the project
@@ -173,6 +215,73 @@ public class App
173
215
}
174
216
```
175
217
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
+
176
285
## Object model
177
286
178
287
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
203
312
- [List the blobs in a container](#list-the-blobs-in-a-container)
204
313
- [Download blobs](#download-blobs)
205
314
- [Delete a container](#delete-a-container)
206
-
315
+
316
+
::: zone pivot="blob-storage-quickstart-scratch"
317
+
207
318
> [!IMPORTANT]
208
319
> Make sure you have the correct dependencies in pom.xml and the necessary directives forthe code samples to work, as describedin the [setting up](#setting-up) section.
209
320
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 foreach part of the sample code. The template implements the recommended passwordless authentication method, as describedin 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
+
210
330
### Authenticate to Azure and authorize access to blob data
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.
310
430
431
+
::: zone pivot="blob-storage-quickstart-scratch"
432
+
311
433
Add this code to the end of the `Main` method:
312
434
435
+
::: zone-end
436
+
313
437
```java
314
438
// Retrieve the connection string for use with the application.
@@ -328,39 +452,45 @@ BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
328
452
329
453
### Create a container
330
454
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.
332
456
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.
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).
343
466
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
+
344
470
### Upload blobs to a container
345
471
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.
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.
To learn more about uploading blobs, and to explore more code samples, see [Upload a blob with Java](storage-blob-upload-java.md).
357
483
358
484
### List the blobs in a container
359
485
360
486
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.
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
369
499
370
500
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.
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
381
515
382
516
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.
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).
389
527
528
+
::: zone pivot="blob-storage-quickstart-scratch"
529
+
390
530
## Run the code
391
531
392
532
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
446
586
447
587
Before you begin the cleanup process, check your *data* folder for the two files. You can compare them and observe that they're identical.
448
588
589
+
::: zone-end
590
+
449
591
## Clean up resources
450
592
593
+
::: zone pivot="blob-storage-quickstart-scratch"
594
+
451
595
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.
452
596
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
+
453
611
## Next steps
454
612
455
613
In this quickstart, you learned how to upload, download, and list blobs using Java.
0 commit comments