本頁面說明如何使用 Cloud Tools for PowerShell 從本機將資料備份至 Cloud Storage。Cloud Tools for PowerShell 與多數資源不同,會提供兩種存取 Cloud Storage 資源的方式:cmdlet 和 PowerShell 提供者。
提供者類似檔案系統,可讓您存取 Storage 值區和物件,而且只要使用您已熟悉的檔案系統指令就能操作。不過,提供者有一些限制。並非所有法律物件名稱都會轉換成法律提供者路徑,而且您無法使用提供者管理 ACL。針對這些進階情況,您可以使用 cmdlet。如要進一步瞭解 Cloud Storage cmdlet,請參閱 Cloud Tools for PowerShell cmdlet 參考資料。
上傳資料
如要整理 Cloud Storage 中的資料,請使用值區。請按照以下說明建立新值區:
cmdlet
使用 New-GcsBucket
指令建立新值區:
$bucket = "my-gcs-bucket" New-GcsBucket $bucket
供應商
值區是位在 gs:\
磁碟根目錄的資料夾。只要在該層級建立新項目,即可建立新的值區。
cd gs:\ $bucket = "my-gcs-bucket" mkdir $bucket
將檔案上傳到值區
您可以將單一檔案或整個目錄上傳到值區:
cmdlet
使用 New-GcsObject
。這個做法需要一個目標值區和一個物件名稱做為參數。新 Storage 物件內容的來源取決於您所使用的參數集。
您可以使用 -File
參數並指定檔案路徑,將本機檔案內容上傳至 Cloud Storage。此外,您也可以透過 PowerShell 管道,將物件內容視為字串來傳送,或使用 -Value
參數。
您可以使用 -Folder
參數並指定資料夾路徑,從本機磁碟將整個目錄上傳到 Cloud Storage。如果您不想將資料夾直接上傳到 Cloud Storage 值區的根目錄,請使用 -ObjectNamePrefix
指定前置字串,這會套用到每個上傳的物件。
# Upload the folder LogFiles and its content to the root of the widget bucket. New-GcsObject -Bucket "widget" -Folder "C:\inetpub\logs\LogFiles" # Upload the folder LogFiles and its content to directory Test in the widget bucket. New-GcsObject -Bucket "widget" -Folder "C:\inetpub\logs\LogFiles" -ObjectNamePrefix "Test"
供應商
使用 New-Item
。這個做法需要有一個前往所要建立項目的路徑 (可以是絕對路徑或相對路徑)。新 Storage 物件的內容可指定為 -Value
參數的字串,或是以檔案路徑的形式指定給 -File
參數。
New-Item gs:\my-gcs-bucket\new-object -File $file
以下程式碼片段會從本機磁碟將整個目錄上傳到 Cloud Storage。
cd $folder $files = Get-ChildItem -Recurse $data = @() foreach ($file in $files) { $objectPath = $file | Resolve-Path -Relative $data += @{file = $file; objectPath = $objectPath} } cd gs:\my-gcs-bucket foreach($element in $data) { Write-Host "`t${$element.objectPath}" New-Item $element.objectPath -File $element.file }
搜尋資料
搜尋資料的方法有兩種:使用 cmdlet 或透過一般檔案搜尋 cmdlet 使用提供者。
cmdlet
您可以使用 Get-GcsObject
透過值區的物件進行搜尋。當您要合併使用 Out-GridView
指令碼將資料視覺化呈現時,這種做法就相當實用:
Get-GcsObject $bucket | Select Name, Size | Out-GridView
供應商
您可以使用 Get-ChildItem
或其任一別名:dir
、ls
或 gci
。您可以使用 -Recurse
參數查詢所有邏輯資料夾:
cd gs:\my-gcs-bucket ls -Recurse
讀取資料
如要透過提供者讀取資料,請使用標準 Get-Content
cmdlet,或者使用 Read-GcsObject
指令碼。
cmdlet
如要讀取 Cloud Storage 物件內容,請使用 Read-GcsObject
指令碼。根據預設,這個 cmdlet 會將物件內容視為字串來讀取,並將其寫入 PowerShell 管道。您可以指定 -OutFile
參數,改為將該物件的內容下載到本機磁碟:
Read-GcsObject $bucket "timestamp.txt" | Write-Host Read-GcsObject $bucket "logo.png" ` -OutFile "$Env:UserProfile\pictures\logo.png"
供應商
如要讀取 Cloud Storage 物件內容,請使用 Get-Content
cmdlet 或其任一別名:cat
、gc
或 type
。
cd gs:\my-gcs-bucket cat my-object-name
刪除資料
如要透過提供者刪除資料,請使用標準 Remove-Item
cmdlet。或者使用 Remove-GcsObject
指令程式。
cmdlet
如要移除 Cloud Storage 中的任何資料,請使用 Remove-GcsObject
cmdlet:
Get-GcsObject $bucket | Remove-GcsObject
供應商
如要移除 Cloud Storage 中的資料,請使用 Remove-Item
cmdlet 或其任一別名 del
、rm
、erase
:
cd gs:\my-gcs-bucket rm my-object-name