如何管理 Azure blob
Azure Blob 存盤是用于存盤大量非結構化資料(例如文本或二進制資料)的服務,這些資料可通過 HTTP 或 HTTPS 從世界各地進行訪問。 本部分假設用戶已熟悉了 Azure Blob 存盤服務的概念。
如何創建容器
Azure 存盤中的每個 Blob 都必須在容器中。 可以使用 New-AzureStorageContainer cmdlet 創建專用容器:
$StorageContainerName = "yourcontainername"
New-AzureStorageContainer -Name $StorageContainerName -Permission Off
如何將 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
想鏈接更多,歡迎點擊這里繼續瀏覽。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/58905.html
標籤:云存儲
下一篇:現在哪種分布式計算框架好用?
