Skip to content

Commit cb897c6

Browse files
Store applicationStore application data with Azure Blob Storage data with Azure Blob Storage
1 parent d9ad2aa commit cb897c6

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
138 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Store application data with Azure Blob Storage
2+
3+
## What are blobs?
4+
5+
Blobs are files for the cloud. Apps work with blobs in much the same way as they work with files on a disk. Apps read and write data to blobs. However, unlike a local file, you can reach blobs from anywhere with an internet connection.
6+
7+
Azure Blob Storage is unstructured. There are no restrictions on the kinds of data it can hold. For example, a blob can hold a PDF document, a JPG image, a JSON file, video content, and more.
8+
9+
Blobs aren't limited to common file formats. A blob could contain gigabytes of binary data streamed from a scientific instrument, an encrypted message for another application, or data in a custom format for an app you're developing.
10+
11+
## Design a storage organization strategy
12+
13+
When you design an app that needs to store data, it's important to think about how the app is going to organize data across storage accounts, containers, and blobs.
14+
15+
### Storage accounts
16+
17+
A single storage account is flexible enough to organize your blobs. However, you should use more storage accounts as necessary to logically separate costs and control access to data.
18+
19+
### Containers and blobs
20+
21+
The nature of your app and the data it stores should drive your strategy for naming and organizing containers and blobs.
22+
23+
Apps that use blobs as part of a storage scheme that includes a database often don't need to rely heavily on organization, naming, or metadata to indicate anything about their data. Such apps commonly use identifiers like GUIDs as blob names and reference these identifiers in database records. The app uses the database to determine where blobs are stored and the kind of data they contain.
24+
25+
## Exercise - Blob uploads and downloads
26+
27+
### Create new blobs
28+
29+
- In the editor, in BlobStorage.cs, replace Save with the following code. Use CTRL+S to save your work.
30+
31+
```
32+
public Task Save(Stream fileStream, string name)
33+
{
34+
BlobServiceClient blobServiceClient = new BlobServiceClient(storageConfig.ConnectionString);
35+
36+
// Get the container (folder) the file will be saved in
37+
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(storageConfig.FileContainerName);
38+
39+
// Get the Blob Client used to interact with (including create) the blob
40+
BlobClient blobClient = containerClient.GetBlobClient(name);
41+
42+
// Upload the blob
43+
return blobClient.UploadAsync(fileStream);
44+
}
45+
```
46+
47+
- Replace Load with this code and save your work using CTRL + S.
48+
49+
```
50+
public Task<Stream> Load(string name)
51+
{
52+
BlobServiceClient blobServiceClient = new BlobServiceClient(storageConfig.ConnectionString);
53+
54+
// Get the container the blobs are saved in
55+
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(storageConfig.FileContainerName);
56+
57+
// Get a client to operate on the blob so we can read it.
58+
BlobClient blobClient = containerClient.GetBlobClient(name);
59+
60+
return blobClient.OpenReadAsync();
61+
}
62+
```
63+
64+
### Deploy and run in Azure
65+
66+
```
67+
az appservice plan create --name blob-exercise-plan --resource-group learn-543091fd-9c4f-4382-86af-6722220bdccd --sku FREE --location eastus
68+
```
69+
70+
```
71+
az webapp create --name <your-unique-app-name> --plan blob-exercise-plan --resource-group learn-543091fd-9c4f-4382-86af-6722220bdccd
72+
```
73+
74+
Example
75+
76+
```
77+
az webapp create --name azurestorageaccount --plan blob-exercise-plan --resource-group learn-543091fd-9c4f-4382-86af-6722220bdccd
78+
```
79+
80+
```
81+
CONNECTIONSTRING=$(az storage account show-connection-string --name <your-unique-storage-account-name> --resource-group learn-543091fd-9c4f-4382-86af-6722220bdccd --output tsv)
82+
```
83+
84+
Example
85+
86+
```
87+
CONNECTIONSTRING=$(az storage account show-connection-string --name cloudshell1107978403 --resource-group learn-543091fd-9c4f-4382-86af-6722220bdccd --output tsv)
88+
```
89+
90+
```
91+
az webapp config appsettings set --name <your-unique-app-name> --resource-group learn-543091fd-9c4f-4382-86af-6722220bdccd --settings AzureStorageConfig:ConnectionString=$CONNECTIONSTRING AzureStorageConfig:FileContainerName=files
92+
```
93+
94+
Example
95+
96+
```
97+
az webapp config appsettings set --name azurestorageaccount --resource-group learn-543091fd-9c4f-4382-86af-6722220bdccd --settings AzureStorageConfig:ConnectionString=$CONNECTIONSTRING AzureStorageConfig:FileContainerName=files
98+
```
99+
100+
## Deploy your app.
101+
102+
```
103+
dotnet publish -o pub
104+
cd pub
105+
zip -r ../site.zip *
106+
```
107+
108+
```
109+
az webapp deployment source config-zip --src ../site.zip --name <your-unique-app-name> --resource-group learn-543091fd-9c4f-4382-86af-6722220bdccd
110+
```
111+
112+
Example
113+
114+
```
115+
az webapp deployment source config-zip --src ../site.zip --name azurestorageaccount --resource-group learn-543091fd-9c4f-4382-86af-6722220bdccd
116+
```
117+
118+
![alt text](image.png)
119+
120+
URL
121+
122+
```
123+
https://azurestorageaccount.scm.azurewebsites.net/api/deployments/latest
124+
```
125+
126+
```
127+
https://<your-unique-app-name>.azurewebsites.net
128+
```
129+
130+
Example
131+
132+
```
133+
https://azurestorageaccount.azurewebsites.net
134+
```
135+
136+
```
137+
az storage blob list --account-name --container-name files --query [].{Name:name} --output table
138+
```
139+
140+
Example
141+
142+
```
143+
az storage blob list --account-name cloudshell1107978403 --container-name files --query [].{Name:name} --output table
144+
```
145+
146+
msbuild FileUploader.csproj
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit decbffde55c08a8ec32bd6d16299339aec542772

0 commit comments

Comments
 (0)