Azure Blob 存储 #

什么是 Blob 存储? #

Azure Blob 存储是 Azure 提供的对象存储服务,用于存储大量非结构化数据。

text
┌─────────────────────────────────────────────────────────────┐
│                    Blob 存储概览                             │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  存储类型                                                    │
│  ├── 块 Blob: 文本、二进制数据                              │
│  ├── 追加 Blob: 日志追加                                   │
│  └── 页 Blob: 虚拟磁盘                                     │
│                                                             │
│  适用场景                                                    │
│  ├── 图片和视频存储                                         │
│  ├── 文档和备份                                             │
│  ├── 日志文件                                               │
│  ├── 静态网站托管                                           │
│  └── 大数据分析                                             │
│                                                             │
│  核心概念                                                    │
│  ├── 存储账户: 顶层容器                                     │
│  ├── 容器: Blob 的逻辑分组                                  │
│  └── Blob: 实际数据对象                                     │
│                                                             │
└─────────────────────────────────────────────────────────────┘

容器管理 #

创建容器 #

bash
# 创建容器
az storage container create \
  --name mycontainer \
  --account-name mystorageaccount

# 列出所有容器
az storage container list \
  --account-name mystorageaccount \
  --output table

# 查看容器属性
az storage container show \
  --name mycontainer \
  --account-name mystorageaccount

容器访问级别 #

text
┌─────────────────────────────────────────────────────────────┐
│                    容器访问级别                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  私有 (Private)                                              │
│  ├── 需要认证访问                                           │
│  ├── 默认选项                                               │
│  └── 最安全                                                 │
│                                                             │
│  Blob (匿名读取 Blob)                                        │
│  ├── Blob 可匿名访问                                        │
│  ├── 容器级别需要认证                                       │
│  └── 适合公开文件                                           │
│                                                             │
│  容器 (匿名读取容器和 Blob)                                  │
│  ├── 容器和 Blob 都可匿名访问                               │
│  ├── 列出容器内容                                           │
│  └── 谨慎使用                                               │
│                                                             │
└─────────────────────────────────────────────────────────────┘

设置访问级别 #

bash
# 设置容器访问级别
az storage container set-permission \
  --name mycontainer \
  --account-name mystorageaccount \
  --public-access blob

# 获取访问级别
az storage container show-permission \
  --name mycontainer \
  --account-name mystorageaccount

Blob 操作 #

上传 Blob #

bash
# 上传文件
az storage blob upload \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --name myfile.txt \
  --file ./localfile.txt

# 上传大文件(分块上传)
az storage blob upload \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --name largefile.zip \
  --file ./largefile.zip \
  --max-connections 4

# 批量上传
az storage blob upload-batch \
  --account-name mystorageaccount \
  --destination mycontainer \
  --source ./myfiles/

下载 Blob #

bash
# 下载单个 Blob
az storage blob download \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --name myfile.txt \
  --file ./downloaded.txt

# 批量下载
az storage blob download-batch \
  --account-name mystorageaccount \
  --source mycontainer \
  --destination ./downloads/

列出和查看 #

bash
# 列出容器中的 Blob
az storage blob list \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --output table

# 查看 Blob 属性
az storage blob show \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --name myfile.txt

# 搜索 Blob
az storage blob list \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --prefix "images/"

删除 Blob #

bash
# 删除单个 Blob
az storage blob delete \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --name myfile.txt

# 批量删除
az storage blob delete-batch \
  --account-name mystorageaccount \
  --source mycontainer \
  --pattern "*.log"

访问控制 #

共享访问签名 (SAS) #

bash
# 生成容器 SAS
az storage container generate-sas \
  --account-name mystorageaccount \
  --name mycontainer \
  --permissions rwdl \
  --expiry 2026-12-31T23:59:59Z \
  --output tsv

# 生成 Blob SAS
az storage blob generate-sas \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --name myfile.txt \
  --permissions r \
  --expiry 2026-12-31T23:59:59Z \
  --output tsv

使用 SAS URL #

bash
# 使用 SAS 下载
curl "https://mystorageaccount.blob.core.windows.net/mycontainer/myfile.txt?<sas-token>" -o downloaded.txt

# 使用 SAS 上传
azcopy copy "./localfile.txt" "https://mystorageaccount.blob.core.windows.net/mycontainer/?<sas-token>"

RBAC 权限 #

text
┌─────────────────────────────────────────────────────────────┐
│                    存储 RBAC 角色                            │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  存储 Blob 数据所有者                                        │
│  └── 完全访问 Blob 数据和权限管理                           │
│                                                             │
│  存储 Blob 数据读取者                                        │
│  └── 读取 Blob 数据                                         │
│                                                             │
│  存储 Blob 数据参与者                                        │
│  └── 读写 Blob 数据                                         │
│                                                             │
│  存储账户贡献者                                              │
│  └── 管理存储账户,不含数据访问                             │
│                                                             │
└─────────────────────────────────────────────────────────────┘

生命周期管理 #

生命周期策略 #

text
┌─────────────────────────────────────────────────────────────┐
│                    生命周期策略                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  规则示例:                                                  │
│                                                             │
│  1. 自动分层                                                 │
│     ├── 30 天后移动到冷层                                   │
│     ├── 90 天后移动到归档层                                 │
│     └── 365 天后删除                                        │
│                                                             │
│  2. 删除规则                                                 │
│     ├── 删除旧版本                                          │
│     ├── 删除软删除快照                                      │
│     └── 清理未完成的分块                                    │
│                                                             │
│  3. 过滤条件                                                 │
│     ├── 前缀匹配                                            │
│     ├── Blob 类型                                           │
│     └── Blob 匹配                                           │
│                                                             │
└─────────────────────────────────────────────────────────────┘

创建生命周期策略 #

bash
# 创建策略文件
cat <<EOF > lifecycle-policy.json
{
  "rules": [
    {
      "name": "move-to-cool",
      "enabled": true,
      "type": "Lifecycle",
      "definition": {
        "filters": {
          "blobTypes": ["blockBlob"],
          "prefixMatch": ["logs/"]
        },
        "actions": {
          "baseBlob": {
            "tierToCool": {
              "daysAfterModificationGreaterThan": 30
            },
            "tierToArchive": {
              "daysAfterModificationGreaterThan": 90
            },
            "delete": {
              "daysAfterModificationGreaterThan": 365
            }
          }
        }
      }
    }
  ]
}
EOF

# 应用策略
az storage account management-policy create \
  --account-name mystorageaccount \
  --policy @lifecycle-policy.json

版本控制 #

启用版本控制 #

bash
# 启用 Blob 版本控制
az storage account blob-service-properties update \
  --account-name mystorageaccount \
  --enable-versioning true

# 查看版本
az storage blob list \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --include v

恢复版本 #

bash
# 列出版本
az storage blob list \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --name myfile.txt \
  --include v \
  --query "[].{Version:versionId,LastModified:properties.lastModified}"

# 恢复到特定版本
az storage blob set-legal-hold \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --name myfile.txt \
  --version-id "<version-id>"

静态网站托管 #

启用静态网站 #

bash
# 启用静态网站托管
az storage blob service-properties update \
  --account-name mystorageaccount \
  --static-website \
  --index-document index.html \
  --404-document 404.html

# 上传网站文件
az storage blob upload-batch \
  --account-name mystorageaccount \
  --destination \$web \
  --source ./website/

# 获取网站 URL
az storage account show \
  --name mystorageaccount \
  --query primaryEndpoints.web \
  --output tsv

最佳实践 #

性能优化 #

text
┌─────────────────────────────────────────────────────────────┐
│                    性能优化建议                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 使用正确的 Blob 类型                                     │
│     ├── 块 Blob: 通用文件                                   │
│     ├── 追加 Blob: 日志                                     │
│     └── 页 Blob: 虚拟磁盘                                   │
│                                                             │
│  2. 分块上传                                                 │
│     └── 大文件使用分块上传                                  │
│                                                             │
│  3. 并行操作                                                 │
│     └── 使用 AzCopy 并行传输                                │
│                                                             │
│  4. CDN 加速                                                 │
│     └── 使用 Azure CDN 缓存                                 │
│                                                             │
│  5. 分区命名                                                 │
│     └── 使用前缀提高查询效率                                │
│                                                             │
└─────────────────────────────────────────────────────────────┘

安全建议 #

text
┌─────────────────────────────────────────────────────────────┐
│                    安全最佳实践                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 禁用公共访问                                             │
│     └── 除非必要,否则禁用                                  │
│                                                             │
│  2. 使用 Azure AD 认证                                       │
│     └── 替代共享密钥                                        │
│                                                             │
│  3. 使用 SAS 时设置短过期时间                                │
│     └── 最小化暴露时间                                      │
│                                                             │
│  4. 启用软删除                                               │
│     └── 防止意外删除                                        │
│                                                             │
│  5. 启用版本控制                                             │
│     └── 数据保护                                            │
│                                                             │
└─────────────────────────────────────────────────────────────┘

下一步 #

现在你已经掌握了 Blob 存储的使用,接下来学习 文件存储 了解 Azure 文件共享服务!

最后更新:2026-03-29