Link hướng dẫn Youtube
https://www.youtube.com/watch?v=FFgn0RKlSI4
Cấu hình Azure Storage
Step 1: tạo Azure Storage Account
Step 2: tạo Container
Step 3: download tool Azure Storage Explorer dùng để duyệt file và thư mục tương tự như Explorer của Window
Download tại đây: https://azure.microsoft.com/en-us/features/storage-explorer/
Code chức năng Upload và Download BlobFile trên Azure Storage Container
Step 1: sử dụng NuGet Package manager để cài đặt thư viện
- Tool -> Nuget Package Manager -> Manage Nuget packages for Solution
- Chọn tab Browse -> gõ từ khóa
WindowsAzure.Storage
- Chọn Install để cài đặt thư viện
Step 2: lấy chuỗi kết nối (connection string) đến Azure Storage
- Truy cập
portal.azure.com
- Login tài khoản
- Chọn
Storage Accounts -> Access Key
- Copy chuỗi Connection String.
- Ví dụ: `DefaultEndpointsProtocol=https;AccountName=azurestudentsdiag959;AccountKey=yYLvvu+cHBGK/sBLIvbYkVzE/VBS+z5Tw4K5komw2ybyvFYIV7ilKtYZnfa7rYQwUvTSrmFniX9QNrLyq9y3sQ==;EndpointSuffix=core.windows.net`
Step 3: viết hàm Upload file lên Azure Storage
3.1. Tạo biến lưu trữ kết nối (connection string) đến Azure Storage
public string StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=azurestudentsdiag959;AccountKey=yYLvvu+cHBGK/sBLIvbYkVzE/VBS+z5Tw4K5komw2ybyvFYIV7ilKtYZnfa7rYQwUvTSrmFniX9QNrLyq9y3sQ==;EndpointSuffix=core.windows.net";
3.2. Bổ sung code upload file cho các action Create , Edit
// Update file lên thư mục Storage Azure
// Create Reference to Azure Storage Account
String strorageconn = this.StorageConnectionString;
CloudStorageAccount storageacc = CloudStorageAccount.Parse(strorageconn);
//Create Reference to Azure Blob
CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();
//The next 2 lines create if not exists a container named "democontainer"
CloudBlobContainer container = blobClient.GetContainerReference("democontainer");
container.CreateIfNotExists();
//The next 7 lines upload the file test.txt with the name DemoBlob on the container "democontainer"
CloudBlockBlob blockBlob = container.GetBlockBlobReference(_FileName);
blockBlob.UploadFromStream(image.InputStream);
Tham khảo code: https://github.com/kellyfire611/learning.nentang.vn-asp.net/blob/master/WebBanHang/WebBanHang/Controllers/Backend/ProductsController.cs
Step 4: viết hàm lấy Link hiển thị Hình ảnh từ Azure Storage
/// <summary>
/// Hàm lấy đường dẫn URL từ Storage Azure
/// </summary>
/// <returns>Image URL</returns>
public string GetImageUrlFromAzureStore()
{
string blobUrl = "";
// Update file lên thư mục Storage Azure
// Create Reference to Azure Storage Account
String strorageconn = this.StorageConnectionString;
CloudStorageAccount storageacc = CloudStorageAccount.Parse(strorageconn);
//Create Reference to Azure Blob
CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();
//The next 2 lines create if not exists a container named "democontainer"
CloudBlobContainer container = blobClient.GetContainerReference("democontainer");
//The next 7 lines upload the file test.txt with the name DemoBlob on the container "democontainer"
CloudBlockBlob blockBlob = container.GetBlockBlobReference(this.image); // hoahong.jpg
blobUrl = blockBlob.Uri.AbsoluteUri; //https://azure.storage.com/kellyfire/democontainer/hoahong.jpg
return blobUrl;
}
Tham khảo code: https://github.com/kellyfire611/learning.nentang.vn-asp.net/blob/master/WebBanHang/WebBanHang/EF/productsExtends.cs
|