如何使用 PowerShell 管理 Azure blobs

qq_40158049 2017-09-08 05:51:11
如何管理 Azure blob
Azure Blob 存储是用于存储大量非结构化数据(例如文本或二进制数据)的服务,这些数据可通过 HTTP 或 HTTPS 从世界各地进行访问。 本部分假设用户已熟悉了 Azure Blob 存储服务的概念。

如何创建容器
Azure 存储中的每个 Blob 都必须在容器中。 可以使用 New-AzureStorageContainer cmdlet 创建专用容器:
$StorageContainerName = "yourcontainername"
New-AzureStorageContainer -Name $StorageContainerName -Permission Off

引用
Note
有三种级别的匿名读取访问权限:Off、Blob 和 Container。 要防止对 Blob 进行匿名访问,请将 Permission 参数设置为 Off。 默认情况下,新容器是专用容器,只能由帐户所有者访问。 要允许对 Blob 资源进行匿名公共读取访问,但不允许访问容器元数据或容器中的 Blob 列表,请将 Permission 参数设置为 Blob。 要允许对 Blob 资源、容器元数据和容器中的 Blob 列表进行完全公开读取访问,请将 Permission 参数设置为 Container。 有关详细信息,请参阅管理对容器和 Blob 的匿名读取访问。


如何将 Blob 上传到容器
Azure Blob 存储支持块 Blob 和页 Blob。 有关详细信息,请参阅 Understanding Block Blobs, Append BLobs, and Page Blobs(了解块 Blob、追加 Blob 和页 Blob)。
要将 Blob 上传到容器,可以使用 Set-AzureStorageBlobContent cmdlet。 默认情况下,此命令会将本地文件上传到块 Blob。 若要指定 Blob 的类型,可以使用 -BlobType 参数。
以下示例将运行 Get-ChildItem cmdlet 以获取指定文件夹中的所有文件,然后,通过使用管道运算符将这些文件传递到下一个 cmdlet。 Set-AzureStorageBlobContent cmdlet 将本地文件上传到容器:
Get-ChildItem -Path C:\Images\* | Set-AzureStorageBlobContent -Container "yourcontainername"


如何从容器下载 Blob
以下示例演示如何从容器下载 Blob。 该示例首先使用存储帐户上下文(包括存储帐户名称及其主访问密钥)与 Azure 存储建立连接。 然后,该示例使用 Get-AzureStorageBlob cmdlet 检索 Blob 引用。 接下来,该示例使用 Get-AzureStorageBlobContent cmdlet 将 Blob 下载到本地目标文件夹。
#Define the variables.
$ContainerName = "yourcontainername"
$DestinationFolder = "C:\DownloadImages"

#Define the storage account and context.
$StorageAccountName = "yourstorageaccount"
$StorageAccountKey = "Storage key for yourstorageaccount ends with =="
$Ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

#List all blobs in a container.
$blobs = Get-AzureStorageBlob -Container $ContainerName -Context $Ctx

#Download blobs from a container.
New-Item -Path $DestinationFolder -ItemType Directory -Force
$blobs | Get-AzureStorageBlobContent -Destination $DestinationFolder -Context $Ctx


如何将 Blob 复制到另一个存储容器
可以跨存储帐户和区域异步复制 Blob。 以下示例演示如何在两个不同的存储帐户,将一个存储容器中的 Blob 复制到另一个存储容器。 该示例首先设置源和目标存储帐户的变量,并创建每个帐户的存储上下文。 接下来,该示例使用 Start-AzureStorageBlobCopy cmdlet 将 Blob 从源容器复制到目标容器。 该示例假设源存储帐户、目标存储帐户和容器已存在。
#Define the source storage account and context.
$SourceStorageAccountName = "yoursourcestorageaccount"
$SourceStorageAccountKey = "Storage key for yoursourcestorageaccount"
$SrcContainerName = "yoursrccontainername"
$SourceContext = New-AzureStorageContext -StorageAccountName $SourceStorageAccountName -StorageAccountKey $SourceStorageAccountKey

#Define the destination storage account and context.
$DestStorageAccountName = "yourdeststorageaccount"
$DestStorageAccountKey = "Storage key for yourdeststorageaccount"
$DestContainerName = "destcontainername"
$DestContext = New-AzureStorageContext -StorageAccountName $DestStorageAccountName -StorageAccountKey $DestStorageAccountKey

#Get a reference to blobs in the source container.
$blobs = Get-AzureStorageBlob -Container $SrcContainerName -Context $SourceContext

#Copy blobs from one container to another.
$blobs| Start-AzureStorageBlobCopy -DestContainer $DestContainerName -DestContext $DestContext

请注意,此示例执行异步复制。

如何从辅助位置复制 Blob
可以从一个已启用 RA-GRS 帐户的辅助位置复制 Blob。
#define secondary storage context using a connection string constructed from secondary endpoints.
$SrcContext = New-AzureStorageContext -ConnectionString "DefaultEndpointsProtocol=https;AccountName=***;AccountKey=***;BlobEndpoint=http://***-secondary.blob.core.chinacloudapi.cn;FileEndpoint=http://***-secondary.file.core.chinacloudapi.cn;QueueEndpoint=http://***-secondary.queue.core.chinacloudapi.cn; TableEndpoint=http://***-secondary.table.core.chinacloudapi.cn;"
Start-AzureStorageBlobCopy -Container *** -Blob *** -Context $SrcContext -DestContainer *** -DestBlob *** -DestContext $DestContext


如何删除 Blob
如果要删除 Blob,首先需要获取 Blob 引用,然后对该引用调用 Remove-AzureStorageBlob cmdlet。 以下示例删除给定容器中的所有 Blob。 该示例首先设置存储帐户的变量,并创建存储上下文。 接下来,该示例使用 Get-AzureStorageBlob cmdlet 检索 Blob 引用,并运行 Remove-AzureStorageBlob cmdlet 删除 Azure 存储内某个容器中的 Blob。
#Define the storage account and context.
$StorageAccountName = "yourstorageaccount"
$StorageAccountKey = "Storage key for yourstorageaccount ends with =="
$ContainerName = "containername"
$Ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

#Get a reference to all the blobs in the container.
$blobs = Get-AzureStorageBlob -Container $ContainerName -Context $Ctx

#Delete blobs in a specified container.
$blobs| Remove-AzureStorageBlob


想链接更多,欢迎点击这里继续浏览

...全文
717 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

932

社区成员

发帖
与我相关
我的任务
社区描述
云计算 云存储相关讨论
社区管理员
  • 云存储
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧