-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathHomeController.cs
More file actions
192 lines (176 loc) · 9.16 KB
/
HomeController.cs
File metadata and controls
192 lines (176 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//----------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious. No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
namespace WebApp_Storage_DotNet.Controllers
{
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web;
using System.Threading.Tasks;
using System.IO;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.Azure;
using System.Configuration;
/// <summary>
/// Azure Blob Storage Photo Gallery - Demonstrates how to use the Blob Storage service.
/// Blob storage stores unstructured data such as text, binary data, documents or media files.
/// Blobs can be accessed from anywhere in the world via HTTP or HTTPS.
///
/// Note: This sample uses the .NET 4.5 asynchronous programming model to demonstrate how to call the Storage Service using the
/// storage client libraries asynchronous API's. When used in real applications this approach enables you to improve the
/// responsiveness of your application. Calls to the storage service are prefixed by the await keyword.
///
/// Documentation References:
/// - What is a Storage Account - http://azure.microsoft.com/en-us/documentation/articles/storage-whatis-account/
/// - Getting Started with Blobs - http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/
/// - Blob Service Concepts - http://msdn.microsoft.com/en-us/library/dd179376.aspx
/// - Blob Service REST API - http://msdn.microsoft.com/en-us/library/dd135733.aspx
/// - Blob Service C# API - http://go.microsoft.com/fwlink/?LinkID=398944
/// - Delegating Access with Shared Access Signatures - http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-shared-access-signature-part-1/
/// </summary>
public class HomeController : Controller
{
static CloudBlobClient blobClient;
const string blobContainerName = "webappstoragedotnet-imagecontainer";
static CloudBlobContainer blobContainer;
/// <summary>
/// Task<ActionResult> Index()
/// Documentation References:
/// - What is a Storage Account: http://azure.microsoft.com/en-us/documentation/articles/storage-whatis-account/
/// - Create a Storage Account: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#create-an-azure-storage-account
/// - Create a Storage Container: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#create-a-container
/// - List all Blobs in a Storage Container: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#list-the-blobs-in-a-container
/// </summary>
public async Task<ActionResult> Index()
{
try
{
// Retrieve storage account information from connection string
// How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"].ToString());
// Create a blob client for interacting with the blob service.
blobClient = storageAccount.CreateCloudBlobClient();
blobContainer = blobClient.GetContainerReference(blobContainerName);
await blobContainer.CreateIfNotExistsAsync();
// To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate
// access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions
// to allow public access to blobs in this container. Comment the line below to not use this approach and to use SAS. Then you can view the image
// using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/webappstoragedotnet-imagecontainer/FileName
await blobContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
// Gets all Cloud Block Blobs in the blobContainerName and passes them to teh view
List<Uri> allBlobs = new List<Uri>();
foreach (IListBlobItem blob in blobContainer.ListBlobs())
{
if (blob.GetType() == typeof(CloudBlockBlob))
allBlobs.Add(blob.Uri);
}
return View(allBlobs);
}
catch (Exception ex)
{
ViewData["message"] = ex.Message;
ViewData["trace"] = ex.StackTrace;
return View("Error");
}
}
/// <summary>
/// Task<ActionResult> UploadAsync()
/// Documentation References:
/// - UploadFromFileAsync Method: https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.blob.cloudpageblob.uploadfromfileasync.aspx
/// </summary>
[HttpPost]
public async Task<ActionResult> UploadAsync()
{
try
{
HttpFileCollectionBase files = Request.Files;
int fileCount = files.Count;
if (fileCount > 0)
{
for (int i = 0; i < fileCount; i++)
{
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(GetRandomBlobName(files[i].FileName));
blob.Properties.ContentType = files[i].ContentType;
await blob.UploadFromStreamAsync(files[i].InputStream);
}
}
return RedirectToAction("Index");
}
catch (Exception ex)
{
ViewData["message"] = ex.Message;
ViewData["trace"] = ex.StackTrace;
return View("Error");
}
}
/// <summary>
/// Task<ActionResult> DeleteImage(string name)
/// Documentation References:
/// - Delete Blobs: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#delete-blobs
/// </summary>
[HttpPost]
public async Task<ActionResult> DeleteImage(string name)
{
try
{
Uri uri = new Uri(name);
string filename = Path.GetFileName(uri.LocalPath);
var blob = blobContainer.GetBlockBlobReference(filename);
await blob.DeleteIfExistsAsync();
return RedirectToAction("Index");
}
catch (Exception ex)
{
ViewData["message"] = ex.Message;
ViewData["trace"] = ex.StackTrace;
return View("Error");
}
}
/// <summary>
/// Task<ActionResult> DeleteAll(string name)
/// Documentation References:
/// - Delete Blobs: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#delete-blobs
/// </summary>
[HttpPost]
public async Task<ActionResult> DeleteAll()
{
try
{
foreach (var blob in blobContainer.ListBlobs())
{
if (blob.GetType() == typeof(CloudBlockBlob))
{
await ((CloudBlockBlob)blob).DeleteIfExistsAsync();
}
}
return RedirectToAction("Index");
}
catch (Exception ex)
{
ViewData["message"] = ex.Message;
ViewData["trace"] = ex.StackTrace;
return View("Error");
}
}
/// <summary>
/// string GetRandomBlobName(string filename): Generates a unique random file name to be uploaded
/// </summary>
private string GetRandomBlobName(string filename)
{
string ext = Path.GetExtension(filename);
return string.Format("{0:10}_{1}{2}", DateTime.Now.Ticks, Guid.NewGuid(), ext);
}
}
}