LAB 04 ~2 giờ Module 3 Trung bình

Azure Storage — Blob & Files

Tạo Storage Account bảo mật chuẩn 2026, quản lý Blob container với access tiers, tạo SAS token có thời hạn, thiết lập Azure Files share, và dùng AzCopy để truyền dữ liệu hiệu quả.

Mục Tiêu Lab

Tạo Storage Account với chuẩn bảo mật: TLS 1.2, HTTPS-only, disable anonymous access

Tạo Blob container, upload files, quản lý access tiers (Hot/Cool/Archive)

Tạo SAS (Shared Access Signature) token có thời hạn để chia sẻ file an toàn

Thiết lập Azure Files share và mount vào Windows/Linux như network drive

Sử dụng AzCopy để bulk transfer dữ liệu hiệu suất cao

Cấu hình Lifecycle Management tự động chuyển blob sang tier rẻ hơn

Yêu Cầu Chuẩn Bị

Cần có:
  • Hoàn thành Lab 01 + Lab 02
  • Azure CLI 2.50+ đã đăng nhập
  • AzCopy đã cài: aka.ms/downloadazcopy
  • Một vài files test (ảnh, PDF, txt) để upload
Chi phí & lưu ý:
  • Storage LRS: ~$0.018/GB/tháng (Hot tier)
  • Archive tier: ~$0.002/GB/tháng nhưng rehydrate mất giờ
  • Azure Files: ~$0.06/GB/tháng (Standard)
  • Xóa RG sau lab để tránh chi phí nhỏ tích lũy

Kịch Bản & Tài Nguyên

Bạn cần thiết lập storage cho ứng dụng HoaTranLab Corp: Blob cho static assets của web app, Azure Files cho shared network drive của team, và SAS để chia sẻ báo cáo với đối tác bên ngoài.

Storage Account
sthoatranlab2026
Standard LRS
TLS 1.2 + HTTPS
Blob Containers
assets (Hot)
reports (Cool)
backup (Archive)
Azure Files
share-team
100 GiB quota
SMB 3.0

Các Bước Thực Hiện

1

Tạo Storage Account Bảo Mật

Storage Account name phải globally unique, lowercase, 3–24 ký tự, không có dấu gạch ngang. Chuẩn 2026: bắt buộc TLS 1.2, HTTPS-only, tắt anonymous blob access.

Naming convention: Vì name phải unique toàn cầu, hãy thêm số ngẫu nhiên: sthoatranlab2026 + số ngẫu nhiên (e.g. sthoatranlab20261234).
Cách 1 — Azure Portal
  1. 1.Portal → Storage accounts → + Create
  2. 2.RG: tạo mới rg-hoatranlab-lab04 | Name: sthoatranlab2026xxxx
  3. 3.Region: Southeast Asia | Performance: Standard | Redundancy: LRS
  4. 4.Tab Advanced: Minimum TLS version: TLS 1.2 | tick Require secure transfer (HTTPS)
  5. 5.Untick Allow Blob anonymous access (tắt hoàn toàn)
  6. 6.Review + Create → Create
Cách 2 — Azure CLI
Azure CLI
# Tạo Resource Group
az group create \
  --name rg-hoatranlab-lab04 \
  --location southeastasia

# Tạo Storage Account với bảo mật chuẩn 2026
# Đặt suffix ngẫu nhiên để tránh name conflict
SUFFIX=$RANDOM
SA_NAME="sthoatranlab${SUFFIX}"

az storage account create \
  --name $SA_NAME \
  --resource-group rg-hoatranlab-lab04 \
  --location southeastasia \
  --sku Standard_LRS \
  --kind StorageV2 \
  --min-tls-version TLS1_2 \
  --https-only true \
  --allow-blob-public-access false \
  --tags env=lab module=az104

echo "Storage Account: $SA_NAME"

# Lấy connection string (dùng cho AzCopy và CLI)
az storage account show-connection-string \
  --name $SA_NAME \
  --resource-group rg-hoatranlab-lab04 \
  --output tsv
2

Tạo Blob Container & Upload Files

Blob container tương tự như folder cấp cao nhất. Access level: Private (mặc định, bắt buộc auth), Blob (anonymous read từng blob), Container (anonymous list + read). Luôn dùng Private khi có thể.

Cách 1 — Azure Portal
  1. 1.Vào Storage Account → Containers (left menu) → + Container
  2. 2.Name: assets | Access level: Private → Create
  3. 3.Tạo thêm container reportsbackup (cùng Private)
  4. 4.Click vào container assetsUpload → chọn file → Upload
  5. 5.Click vào blob đã upload → xem Properties: URL, Content-Type, Size, Access tier
  6. 6.Tab Change tier: thay đổi blob sang Cool tier để xem
Cách 2 — Azure CLI
Azure CLI
# Lấy storage key để auth CLI
SA_KEY=$(az storage account keys list \
  --account-name $SA_NAME \
  --resource-group rg-hoatranlab-lab04 \
  --query "[0].value" -o tsv)

# Tạo 3 containers
for CONTAINER in assets reports backup; do
  az storage container create \
    --name $CONTAINER \
    --account-name $SA_NAME \
    --account-key $SA_KEY \
    --public-access off
done

# Tạo file test để upload
echo "HoaTranLab Azure Storage Lab 04" > testfile.txt
date >> testfile.txt

# Upload file vào container assets (Hot tier mặc định)
az storage blob upload \
  --account-name $SA_NAME \
  --account-key $SA_KEY \
  --container-name assets \
  --name "docs/testfile.txt" \
  --file testfile.txt \
  --tier Hot

# Upload file vào reports (Cool tier)
az storage blob upload \
  --account-name $SA_NAME \
  --account-key $SA_KEY \
  --container-name reports \
  --name "2026/report-q1.txt" \
  --file testfile.txt \
  --tier Cool

# Liệt kê blobs trong container assets
az storage blob list \
  --account-name $SA_NAME \
  --account-key $SA_KEY \
  --container-name assets \
  --output table
Kết quả (Output)— az storage blob list
Name                  Blob Type    Blob Tier    Length    Content Type
--------------------  -----------  -----------  --------  ------------
docs/testfile.txt     BlockBlob    Hot          42        text/plain
3

Tạo SAS Token — Chia Sẻ An Toàn Có Thời Hạn

SAS (Shared Access Signature) cho phép chia sẻ access có kiểm soát: chỉ định resource cụ thể, permissions (read/write/delete), IP range, thời gian hết hạn. Không cần chia sẻ account key.

Service SAS
Cho 1 service (Blob/File/Queue/Table)
Account SAS
Nhiều services, permissions rộng hơn
User Delegation SAS
Dùng Microsoft Entra ID — bảo mật nhất
Cách 1 — Azure Portal
  1. 1.Storage Account → Containers → assets → click vào file docs/testfile.txt
  2. 2.Tab Generate SAS → Permissions: Read only
  3. 3.Expiry: đặt +1 giờ từ hiện tại | Allowed protocols: HTTPS only
  4. 4.Click Generate SAS token and URL → copy Blob SAS URL
  5. 5.Paste URL vào trình duyệt → file download thành công mà không cần đăng nhập
  6. 6.Sửa URL: xóa token → thấy lỗi 403 Forbidden (confirm private)
Cách 2 — Azure CLI
Azure CLI
# Tính thời gian hết hạn: 2 giờ từ hiện tại
END_TIME=$(date -u -d '+2 hours' '+%Y-%m-%dT%H:%MZ' 2>/dev/null \
  || date -u -v+2H '+%Y-%m-%dT%H:%MZ')

# Tạo SAS token cho 1 blob cụ thể (Read only, HTTPS)
SAS_TOKEN=$(az storage blob generate-sas \
  --account-name $SA_NAME \
  --account-key $SA_KEY \
  --container-name assets \
  --name "docs/testfile.txt" \
  --permissions r \
  --expiry $END_TIME \
  --https-only \
  --output tsv)

# Tạo full URL để chia sẻ
BLOB_URL="https://${SA_NAME}.blob.core.windows.net/assets/docs/testfile.txt?${SAS_TOKEN}"
echo "SAS URL (valid 2 hours):"
echo $BLOB_URL

# Test download qua curl (không cần auth)
curl -s "$BLOB_URL"

# Tạo Account-level SAS (toàn storage account)
az storage account generate-sas \
  --account-name $SA_NAME \
  --account-key $SA_KEY \
  --resource-types sco \
  --services bfqt \
  --permissions rl \
  --expiry $END_TIME \
  --https-only \
  --output tsv
4

Azure Files Share — Network Drive Trên Cloud

Azure Files cung cấp SMB/NFS file share có thể mount vào Windows/Linux/macOS như ổ đĩa mạng. Phù hợp cho shared config, legacy apps cần network drive, và lift-and-shift.

Cách 1 — Azure Portal
  1. 1.Storage Account → File shares+ File share
  2. 2.Name: share-team | Tier: Transaction optimized | Quota: 100 GiB
  3. 3.Create → click vào share-teamUpload → upload file test
  4. 4.Click Connect → chọn OS: Windows → copy PowerShell script
  5. 5.Chạy script trong PowerShell → ổ Z: xuất hiện trong File Explorer
  6. 6.Thử tạo file trong Z: → file xuất hiện trên Portal File share
Cách 2 — Azure CLI
Azure CLI
# Tạo Azure File share
az storage share create \
  --name share-team \
  --account-name $SA_NAME \
  --account-key $SA_KEY \
  --quota 100

# Upload file vào file share
az storage file upload \
  --account-name $SA_NAME \
  --account-key $SA_KEY \
  --share-name share-team \
  --source testfile.txt \
  --path "shared/testfile.txt"

# Liệt kê files trong share
az storage file list \
  --account-name $SA_NAME \
  --account-key $SA_KEY \
  --share-name share-team \
  --output table
PowerShell— Mount file share vào Windows
# (Portal sinh ra script này tự động)
$connectTestResult = Test-NetConnection `
  -ComputerName "${SA_NAME}.file.core.windows.net" `
  -Port 445
if ($connectTestResult.TcpTestSucceeded) {
    $storageKey = ConvertTo-SecureString `
      -String "$SA_KEY" -AsPlainText -Force
    $credential = New-Object `
      System.Management.Automation.PSCredential `
      -ArgumentList "Azure\$SA_NAME", $storageKey
    New-PSDrive -Name Z -PSProvider FileSystem `
      -Root "\\${SA_NAME}.file.core.windows.net\share-team" `
      -Credential $credential -Persist
}
5

AzCopy — Transfer Dữ Liệu Hiệu Suất Cao

AzCopy là CLI tool tối ưu cho bulk transfer: parallel uploads, resume interrupted transfers, copy giữa storage accounts. Nhanh hơn azure-cli cho batch operations.

Cách 1 — Portal (Storage Explorer)
  1. 1.Tải Azure Storage Explorer và đăng nhập
  2. 2.Browse tới container → drag & drop files để upload/download
  3. 3.Right-click container → Get Shared Access Signature để tạo SAS từ GUI
  4. 4.Snapshot: right-click blob → Create Snapshot
Cách 2 — AzCopy CLI
Bash / PowerShell-CMD (Windows)— AzCopy v10
# Đăng nhập AzCopy bằng Microsoft Entra ID
azcopy login

# Tạo thư mục test với nhiều files
mkdir azcopy-test
for i in 1 2 3 4 5; do
  echo "File $i - $(date)" > azcopy-test/file${i}.txt
done

# Upload cả thư mục lên container (recursive)
azcopy copy \
  "azcopy-test/*" \
  "https://${SA_NAME}.blob.core.windows.net/assets/" \
  --recursive

# Xem kết quả upload
azcopy list \
  "https://${SA_NAME}.blob.core.windows.net/assets/"

# Copy giữa 2 containers trong cùng storage account
azcopy copy \
  "https://${SA_NAME}.blob.core.windows.net/assets/" \
  "https://${SA_NAME}.blob.core.windows.net/backup/" \
  --recursive

# Download container về local
azcopy copy \
  "https://${SA_NAME}.blob.core.windows.net/assets/" \
  "./download-test/" \
  --recursive
6

Lifecycle Management — Tự Động Chuyển Access Tier

Lifecycle policies tự động chuyển blob sang tier rẻ hơn theo thời gian, giúp tối ưu chi phí mà không cần can thiệp thủ công. Hot → Cool (30 ngày) → Archive (90 ngày) → Delete (365 ngày).

Cách 1 — Azure Portal
  1. 1.Storage Account → Lifecycle management+ Add a rule
  2. 2.Rule name: tier-down-old-blobs | Apply to all blobs
  3. 3.Base blobs → nếu last modified > 30 ngày: Move to cool
  4. 4.Nếu last modified > 90 ngày: Move to archive
  5. 5.Nếu last modified > 365 ngày: Delete blob
  6. 6.Add → Save
Cách 2 — Azure CLI
Azure CLI
# Tạo lifecycle policy (JSON inline)
az storage account management-policy create \
  --account-name $SA_NAME \
  --resource-group rg-hoatranlab-lab04 \
  --policy '{
    "rules": [{
      "name": "tier-down-old-blobs",
      "enabled": true,
      "type": "Lifecycle",
      "definition": {
        "filters": {
          "blobTypes": ["blockBlob"]
        },
        "actions": {
          "baseBlob": {
            "tierToCool": {
              "daysAfterModificationGreaterThan": 30
            },
            "tierToArchive": {
              "daysAfterModificationGreaterThan": 90
            },
            "delete": {
              "daysAfterModificationGreaterThan": 365
            }
          }
        }
      }
    }]
  }'

# Verify policy
az storage account management-policy show \
  --account-name $SA_NAME \
  --resource-group rg-hoatranlab-lab04

Kết Quả Đầu Ra Lab 04

Storage Account bảo mật

TLS 1.2, HTTPS-only, anonymous access disabled — confirm trong Portal → Configuration

3 Blob containers hoạt động

assets (Hot), reports (Cool), backup — az storage blob list trả về files đã upload

SAS URL hoạt động đúng

URL với token → download OK; URL không có token → HTTP 403 Forbidden

Azure Files share mounted

Ổ Z: hiển thị trong Windows Explorer, file tạo từ local xuất hiện trên Portal

AzCopy bulk upload thành công

5 files upload parallel, azcopy list hiển thị đầy đủ trong container assets

Lifecycle policy kích hoạt

Policy visible trong Lifecycle management, JSON rule đúng với 3 actions (Cool/Archive/Delete)

Dọn Dẹp Tài Nguyên

Azure CLI
# Xóa Resource Group chứa toàn bộ storage resources
az group delete \
  --name rg-hoatranlab-lab04 \
  --yes \
  --no-wait

# Xóa files local đã tạo
rm -rf testfile.txt azcopy-test download-test

Câu Hỏi Ôn Tập

1. So sánh 4 access tiers của Blob Storage: Hot, Cool, Cold, Archive. Khi nào nên dùng mỗi tier?

Gợi ý: Hot = truy cập thường xuyên (chi phí storage cao, access rẻ); Cool = 30+ ngày (storage rẻ hơn, access đắt hơn); Cold = 90+ ngày; Archive = 180+ ngày, rehydrate mất 1–15 giờ. Chi phí nghịch: storage thấp dần, retrieval cao dần.

2. Sự khác biệt giữa LRS, ZRS, GRS, GZRS là gì? Cho production critical data, nên chọn loại nào?

Gợi ý: LRS = 3 copies trong 1 datacenter; ZRS = 3 zones trong 1 region; GRS = LRS + region thứ hai; GZRS = ZRS + region thứ hai. Critical data: GZRS (SLA 99.99999999999999% - 16 nines).

3. Tại sao User Delegation SAS được coi là bảo mật hơn Account Key SAS? Khi nào KHÔNG nên dùng Account Key?

Gợi ý: User Delegation SAS dùng Microsoft Entra ID credential (có thể revoke qua Entra, không cần rotate key); Account Key = master key, nếu lộ phải rotate ngay. Không dùng Account Key trong code/CI-CD — dùng Managed Identity thay thế.

4. Azure Blob Storage và Azure Files khác nhau như thế nào? Ứng dụng phù hợp cho mỗi loại?

Gợi ý: Blob = object storage (HTTP/REST), tối ưu cho unstructured data, static web, backup; Files = SMB/NFS file system, mount như network drive, lift-and-shift legacy apps cần network share, shared config giữa VMs.

5. Lifecycle Management policy có thể áp dụng cho blob đang ở Archive tier để delete không? Cần làm gì trước?

Gợi ý: Có, delete action hoạt động trực tiếp trên Archive blob. Nhưng nếu muốn READ blob Archive → phải rehydrate sang Hot/Cool trước (mất 1–15 giờ, tùy priority). Rehydrate = copy to new tier, original vẫn ở Archive.

Lab 03: Virtual Machines Thư viện Labs Lab 05: Virtual Networking
Zalo