|
| 1 | +--- |
| 2 | +title: "Quickstart: Azure Blob storage library v12 - Xamarin" |
| 3 | +description: In this quickstart, you learn how to use the Azure Blob storage client library version 12 with Xamarin to create a container and a blob in Blob (object) storage. Next, you learn how to download the blob to your mobile device, and how to list all of the blobs in a container. |
| 4 | +author: codemillmatt |
| 5 | + |
| 6 | +ms.author: masoucou |
| 7 | +ms.date: 05/08/2020 |
| 8 | +ms.service: storage |
| 9 | +ms.subservice: blobs |
| 10 | +ms.topic: quickstart |
| 11 | +--- |
| 12 | + |
| 13 | +# Quickstart: Azure Blob storage client library v12 with Xamarin |
| 14 | + |
| 15 | +Get started with the Azure Blob storage client library v12 with Xamarin. Azure Blob storage is Microsoft's object storage solution for the cloud. Follow steps to install the package and try out example code for basic tasks. Blob storage is optimized for storing massive amounts of unstructured data. |
| 16 | + |
| 17 | +Use the Azure Blob storage client library v12 with Xamarin to: |
| 18 | + |
| 19 | +* Create a container |
| 20 | +* Upload a blob to Azure Storage |
| 21 | +* List all of the blobs in a container |
| 22 | +* Download the blob to your device |
| 23 | +* Delete a container |
| 24 | + |
| 25 | +[API reference documentation](/dotnet/api/azure.storage.blobs) | [Library source code](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/storage/Azure.Storage.Blobs) | [Package (NuGet)](https://www.nuget.org/packages/Azure.Storage.Blobs) | [Sample](https://github.com/Azure-Samples/storage-blobs-xamarin-quickstart) |
| 26 | + |
| 27 | +[!INCLUDE [storage-multi-protocol-access-preview](../../../includes/storage-multi-protocol-access-preview.md)] |
| 28 | + |
| 29 | +## Prerequisites |
| 30 | + |
| 31 | +* Azure subscription - [create one for free](https://azure.microsoft.com/free/) |
| 32 | +* Azure storage account - [create a storage account](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account) |
| 33 | +* Visual Studio with [Mobile Development for .NET workload](https://docs.microsoft.com/xamarin/get-started/installation/?pivots=windows) installed or [Visual Studio for Mac](https://docs.microsoft.com/visualstudio/mac/installation?view=vsmac-2019) |
| 34 | + |
| 35 | +## Setting up |
| 36 | + |
| 37 | +This section walks you through preparing a project to work with the Azure Blob storage client library v12 with Xamarin. |
| 38 | + |
| 39 | +### Create the project |
| 40 | + |
| 41 | +1. Open Visual Studio and create a Blank Forms App. |
| 42 | +1. Name it: BlobQuickstartV12 |
| 43 | + |
| 44 | +### Install the package |
| 45 | + |
| 46 | +1. Right-click your solution in the Solution Explorer pane and select **Manage NuGet Packages for Solution**. |
| 47 | +1. Search for **Azure.Storage.Blobs** and install the latest stable version into all projects in your solution. |
| 48 | + |
| 49 | +### Set up the app framework |
| 50 | + |
| 51 | +From the **BlobQuickstartV12** directory: |
| 52 | + |
| 53 | +1. Open up the *MainPage.xaml* file in your editor |
| 54 | +1. Remove everything between the `<ContentPage></ContentPage>` elements and replace with the below: |
| 55 | + |
| 56 | +```xaml |
| 57 | +<StackLayout HorizontalOptions="Center" VerticalOptions="Center"> |
| 58 | + |
| 59 | + <Button x:Name="uploadButton" Text="Upload Blob" Clicked="Upload_Clicked" IsEnabled="False"/> |
| 60 | + <Button x:Name="listButton" Text="List Blobs" Clicked="List_Clicked" IsEnabled="False" /> |
| 61 | + <Button x:Name="downloadButton" Text="Download Blob" Clicked="Download_Clicked" IsEnabled="False" /> |
| 62 | + <Button x:Name="deleteButton" Text="Delete Container" Clicked="Delete_Clicked" IsEnabled="False" /> |
| 63 | + |
| 64 | + <Label Text="" x:Name="resultsLabel" HorizontalTextAlignment="Center" Margin="0,20,0,0" TextColor="Red" /> |
| 65 | + |
| 66 | +</StackLayout> |
| 67 | +``` |
| 68 | + |
| 69 | +[!INCLUDE [storage-quickstart-credentials-xamarin-include](../../../includes/storage-quickstart-credentials-xamarin-include.md)] |
| 70 | + |
| 71 | +## Object model |
| 72 | + |
| 73 | +Azure Blob storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that does not adhere to a particular data model or definition, such as text or binary data. Blob storage offers three types of resources: |
| 74 | + |
| 75 | +* The storage account |
| 76 | +* A container in the storage account |
| 77 | +* A blob in the container |
| 78 | + |
| 79 | +The following diagram shows the relationship between these resources. |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +Use the following .NET classes to interact with these resources: |
| 84 | + |
| 85 | +* [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient): The `BlobServiceClient` class allows you to manipulate Azure Storage resources and blob containers. |
| 86 | +* [BlobContainerClient](/dotnet/api/azure.storage.blobs.blobcontainerclient): The `BlobContainerClient` class allows you to manipulate Azure Storage containers and their blobs. |
| 87 | +* [BlobClient](/dotnet/api/azure.storage.blobs.blobclient): The `BlobClient` class allows you to manipulate Azure Storage blobs. |
| 88 | +* [BlobDownloadInfo](/dotnet/api/azure.storage.blobs.models.blobdownloadinfo): The `BlobDownloadInfo` class represents the properties and content returned from downloading a blob. |
| 89 | + |
| 90 | +## Code examples |
| 91 | + |
| 92 | +These example code snippets show you how to perform the following tasks with the Azure Blob storage client library for .NET in a Xamarin.Forms app: |
| 93 | + |
| 94 | +* [Create class level variables](#create-class-level-variables) |
| 95 | +* [Create a container](#create-a-container) |
| 96 | +* [Upload blobs to a container](#upload-blobs-to-a-container) |
| 97 | +* [List the blobs in a container](#list-the-blobs-in-a-container) |
| 98 | +* [Download blobs](#download-blobs) |
| 99 | +* [Delete a container](#delete-a-container) |
| 100 | + |
| 101 | +### Create class level variables |
| 102 | + |
| 103 | +The code below declares several class level variables. They needed to communicate to Azure Blob storage throughout the rest of this sample. |
| 104 | + |
| 105 | +These are in addition to the connection string for the storage account set in the [Configure your storage connection string](#configure-your-storage-connection-string) section. |
| 106 | + |
| 107 | +Add this code as class level variables inside the *MainPage.xaml.cs* file: |
| 108 | + |
| 109 | +```csharp |
| 110 | +string storageConnectionString = "{set in the Configure your storage connection string section}"; |
| 111 | +string fileName = $"{Guid.NewGuid()}-temp.txt"; |
| 112 | + |
| 113 | +BlobServiceClient client; |
| 114 | +BlobContainerClient containerClient; |
| 115 | +BlobClient blobClient; |
| 116 | +``` |
| 117 | + |
| 118 | +### Create a container |
| 119 | + |
| 120 | +Decide on a name for the new container. The code below appends a GUID value to the container name to ensure that it is unique. |
| 121 | + |
| 122 | +> [!IMPORTANT] |
| 123 | +> 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). |
| 124 | +
|
| 125 | +Create an instance of the [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient) class. Then, call the [CreateBlobContainerAsync](/dotnet/api/azure.storage.blobs.blobserviceclient.createblobcontainerasync) method to create the container in your storage account. |
| 126 | + |
| 127 | +Add this code to *MainPage.xaml.cs* file: |
| 128 | + |
| 129 | +```csharp |
| 130 | +protected async override void OnAppearing() |
| 131 | +{ |
| 132 | + string containerName = $"quickstartblobs{Guid.NewGuid()}"; |
| 133 | + |
| 134 | + client = new BlobServiceClient(storageConnectionString); |
| 135 | + containerClient = await client.CreateBlobContainerAsync(containerName); |
| 136 | + |
| 137 | + resultsLabel.Text = "Container Created\n"; |
| 138 | + |
| 139 | + blobClient = containerClient.GetBlobClient(fileName); |
| 140 | + |
| 141 | + uploadButton.IsEnabled = true; |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +### Upload blobs to a container |
| 146 | + |
| 147 | +The following code snippet: |
| 148 | + |
| 149 | +1. Creates a `MemoryStream` of text. |
| 150 | +1. Uploads the text to a Blob by calling the [UploadAsync](/dotnet/api/azure.storage.blobs.blobcontainerclient.uploadblobasync?view=azure-dotnet#Azure_Storage_Blobs_BlobContainerClient_UploadBlobAsync_System_String_System_IO_Stream_System_Threading_CancellationToken_) function of the [BlobContainerClient](/dotnet/api/azure.storage.blobs.blobcontainerclient) class, passing it both the filename defined the class level variable and the `MemoryStream` of text. This method creates the blob if it doesn't already exist, and overwrites it if it does. |
| 151 | + |
| 152 | +Add this code to the *MainPage.xaml.cs* file: |
| 153 | + |
| 154 | +```csharp |
| 155 | +async void Upload_Clicked(object sender, EventArgs e) |
| 156 | +{ |
| 157 | + using MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("Hello World!")); |
| 158 | + |
| 159 | + await containerClient.UploadBlobAsync(fileName, memoryStream); |
| 160 | + |
| 161 | + resultsLabel.Text += "Blob Uploaded\n"; |
| 162 | + |
| 163 | + uploadButton.IsEnabled = false; |
| 164 | + listButton.IsEnabled = true; |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +### List the blobs in a container |
| 169 | + |
| 170 | +List the blobs in the container by calling the [GetBlobsAsync](/dotnet/api/azure.storage.blobs.blobcontainerclient.getblobsasync) method. In this case, only one blob has been added to the container, so the listing operation returns just that one blob. |
| 171 | + |
| 172 | +Add this code to the *MainPage.xaml.cs* file: |
| 173 | + |
| 174 | +```csharp |
| 175 | +async void List_Clicked(object sender, EventArgs e) |
| 176 | +{ |
| 177 | + await foreach (BlobItem blobItem in containerClient.GetBlobsAsync()) |
| 178 | + { |
| 179 | + resultsLabel.Text += blobItem.Name + "\n"; |
| 180 | + } |
| 181 | + |
| 182 | + listButton.IsEnabled = false; |
| 183 | + downloadButton.IsEnabled = true; |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +### Download blobs |
| 188 | + |
| 189 | +Download the previously created blob by calling the [DownloadAsync](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.downloadasync) method. The example code copies the `Stream` representation of the blob first into a `MemoryStream` and then into a `StreamReader` so the text can be displayed. |
| 190 | + |
| 191 | +Add this code to the *MainPage.xaml.cs* file: |
| 192 | + |
| 193 | +```csharp |
| 194 | +async void Download_Clicked(object sender, EventArgs e) |
| 195 | +{ |
| 196 | + BlobDownloadInfo downloadInfo = await blobClient.DownloadAsync(); |
| 197 | + |
| 198 | + using MemoryStream memoryStream = new MemoryStream(); |
| 199 | + |
| 200 | + await downloadInfo.Content.CopyToAsync(memoryStream); |
| 201 | + memoryStream.Position = 0; |
| 202 | + |
| 203 | + using StreamReader streamReader = new StreamReader(memoryStream); |
| 204 | + |
| 205 | + resultsLabel.Text += "Blob Contents: \n"; |
| 206 | + resultsLabel.Text += await streamReader.ReadToEndAsync(); |
| 207 | + resultsLabel.Text += "\n"; |
| 208 | + |
| 209 | + downloadButton.IsEnabled = false; |
| 210 | + deleteButton.IsEnabled = true; |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +### Delete a container |
| 215 | + |
| 216 | +The following code cleans up the resources the app created by deleting the entire container by using [DeleteAsync](/dotnet/api/microsoft.azure.storage.blob.cloudblobcontainer.deleteasync). |
| 217 | + |
| 218 | +The app first prompts to confirm before it deletes the blob and container. This is a good chance to verify that the resources were created correctly, before they are deleted. |
| 219 | + |
| 220 | +Add this code to the *MainPage.xaml.cs* file: |
| 221 | + |
| 222 | +```csharp |
| 223 | +async void Delete_Clicked(object sender, EventArgs e) |
| 224 | +{ |
| 225 | + var deleteContainer = await Application.Current.MainPage.DisplayAlert("Delete Container", |
| 226 | + "You are about to delete the container proceeed?", "OK", "Cancel"); |
| 227 | + |
| 228 | + if (deleteContainer == false) |
| 229 | + return; |
| 230 | + |
| 231 | + await containerClient.DeleteAsync(); |
| 232 | + |
| 233 | + resultsLabel.Text += "Container Deleted"; |
| 234 | + |
| 235 | + deleteButton.IsEnabled = false; |
| 236 | +} |
| 237 | +``` |
| 238 | + |
| 239 | +## Run the code |
| 240 | + |
| 241 | +When the app starts, it will first create the container as it appears. Then you will need to click the buttons in order to upload, list, download the blobs, and delete the container. |
| 242 | + |
| 243 | +To run the app on Windows press F5. To run the app on Mac press Cmd+Enter. |
| 244 | + |
| 245 | +The app writes to the screen after every operation. The output of the app is similar to the example below: |
| 246 | + |
| 247 | +```output |
| 248 | +Container Created |
| 249 | +Blob Uploaded |
| 250 | +98d9a472-8e98-4978-ba4f-081d69d2e6f8-temp.txt |
| 251 | +Blob Contents: |
| 252 | +Hello World! |
| 253 | +Container Deleted |
| 254 | +``` |
| 255 | + |
| 256 | +Before you begin the clean-up process, verify the output of the blob's contents on screen match the value that was uploaded. |
| 257 | + |
| 258 | +After you've verified the values, confirm the prompt to delete the container and finish the demo. |
| 259 | + |
| 260 | +## Next steps |
| 261 | + |
| 262 | +In this quickstart, you learned how to upload, download, and list blobs using Azure Blob storage client library v12 with Xamarin. |
| 263 | + |
| 264 | +To see Blob storage sample apps, continue to: |
| 265 | + |
| 266 | +> [!div class="nextstepaction"] |
| 267 | +> [Azure Blob storage SDK v12 Xamarin sample](https://github.com/Azure-Samples/storage-blobs-xamarin-quickstart) |
| 268 | +
|
| 269 | +* For tutorials, samples, quick starts and other documentation, visit [Azure for mobile developers](/azure/mobile-apps). |
| 270 | +* To learn more about Xamarin, see [Getting started with Xamarin](/xamarin/get-started/). |
0 commit comments