Skip to content

Commit 0081e17

Browse files
authored
Merge pull request #114631 from codemillmatt/xam-storage-quickstart
creating quickstart for xamarin and blob storage
2 parents 0bc5b9b + eaf0eb0 commit 0081e17

File tree

3 files changed

+304
-0
lines changed

3 files changed

+304
-0
lines changed

articles/storage/blobs/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
href: storage-quickstart-blobs-php.md
6060
- name: Ruby
6161
href: storage-quickstart-blobs-ruby.md
62+
- name: Xamarin
63+
href: storage-quickstart-blobs-xamarin.md
6264
- name: Tutorials
6365
items:
6466
- name: Upload and process an image
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
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+
![Diagram of Blob storage architecture](./media/storage-blobs-introduction/blob1.png)
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 [​Download​Async](/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/).
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: "include file"
3+
description: "include file"
4+
services: storage
5+
author: codemillmatt
6+
ms.service: storage
7+
ms.topic: "include"
8+
ms.date: 11/23/2019
9+
ms.author: masoucou
10+
ms.custom: "include file"
11+
---
12+
13+
### Copy your credentials from the Azure portal
14+
15+
When the sample application makes a request to Azure Storage, it must be authorized. To authorize a request, add your storage account credentials to the application as a connection string. View your storage account credentials by following these steps:
16+
17+
1. Sign in to the [Azure portal](https://portal.azure.com).
18+
2. Locate your storage account.
19+
3. In the **Settings** section of the storage account overview, select **Access keys**. Here, you can view your account access keys and the complete connection string for each key.
20+
4. Find the **Connection string** value under **key1**, and select the **Copy** button to copy the connection string. You will add the connection string value to an environment variable in the next step.
21+
22+
![Screenshot showing how to copy a connection string from the Azure portal](./media/storage-copy-connection-string-portal/portal-connection-string.png)
23+
24+
### Configure your storage connection string
25+
26+
After you have copied your connection string, set it to a class level variable in your *MainPage.xaml.cs* file. Open up *MainPaage.xaml.cs* and find the `storageConnectionString` variable. Replace `<yourconnectionstring>` with your actual connection string.
27+
28+
Here's the code:
29+
30+
```csharp
31+
string storageConnectionString = "<yourconnectionstring>";
32+
```

0 commit comments

Comments
 (0)