﻿# 如何利用 Powershell 同步本機磁碟與 Amazon S3 的檔案

最近在協助客戶將 [Amazon S3](http://aws.amazon.com/s3/) 上面的檔案遷移到 [Windows Azure Storage](https://learn.microsoft.com/en-us/azure/storage/)，若要遷移這些存在 S3 上面的檔案，勢必要先把檔案下載回來，所以我今天打算分享一個不用錢的解決方案，讓你可以快速的同步本機與雲端上的檔案，你可以把本機磁碟中的檔案同步到 Amazon S3，也可以從 Amazon S3 同步檔案到本機磁碟中，已經同步過的檔案還會自動跳過，所以除了第一次同步外，日後每次同步檔案的速度都將非常快。

**※安裝** [**CloudBerry Explorer for Amazon S3**](http://www.cloudberrylab.com/free-amazon-s3-explorer-cloudfront-IAM.aspx) **或 CloudBerry Explorer for Amazon S3 PRO**

由於安裝了 [CloudBerry Explorer for Amazon S3](http://www.cloudberrylab.com/free-amazon-s3-explorer-cloudfront-IAM.aspx) 之後，會連帶自動安裝 Powershell 的 **CloudBerryLab.Explorer.PSSnapIn** 管理單元，在這個管理單元中包含了一組可管理 Amazon S3 的 cmdlet 命令。

不過，有點要特別注意，CloudBerryLab.Explorer.PSSnapIn 僅支援 64 位元架構下，請記得要啟動 64-bit 的 Powershell 視窗，否則會無法在執行環境中加入這個 CloudBerryLab.Explorer.PSSnapIn 管理單元。我光是這點問題，卡了我好些時間，原因就在於我的電腦有安裝 [Windows Azure PowerShell](http://azure.microsoft.com/en-us/downloads/) 套件，所以通常我如果要透過 PowerShell 管理 Windows Azure 資源的時候，都會透過這個捷徑啟動程式：

![image](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/97fbd1347c341dfe5976-image_2511.png "image")

誰知道，原來這個捷徑指向的是 32-bit 的 PowerShell 環境！ >”<

請看看這個捷徑的內容，你會看到 **[SysWOW64](http://en.wikipedia.org/wiki/WoW64)** 字樣，這就代表 PowerShell 是執行在 32-bit 環境下的。

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/8293f96ab1788e2af905-image_2512.png)

如上圖目標的內容如下：

**C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -ExecutionPolicy Bypass -File "C:\\Program Files (x86)\\Microsoft SDKs\\Windows Azure\\PowerShell\\ServiceManagement\\Azure\\Services\\ShortcutStartup.ps1"**

我做了些小調整，把 powershell.exe 的路徑改一下而已，如下指令碼：

**C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -ExecutionPolicy Bypass -File "C:\\Program Files (x86)\\Microsoft SDKs\\Windows Azure\\PowerShell\\ServiceManagement\\Azure\\Services\\ShortcutStartup.ps1"**

  
**※使用 CloudBerryLab.Explorer.PSSnapIn 管理單元提供的命令**

大致上的使用情境如下：

1\. 加入 CloudBerryLab.Explorer.PSSnapIn 到當前的 PowerShell 執行環境中 (必要動作)

```
Add-PSSnapin CloudBerryLab.Explorer.PSSnapIn
```

2\. 列出所有 CloudBerryLab.Explorer.PSSnapIn 支援的命令

```
Get-Command | Where-Object {$_.PSSnapin.Name -eq "CloudBerryLab.Explorer.PSSnapIn"}
```

3\. 先取得 S3 連線物件

```
$key = "YOUR_KEY"
$secret = "YOUR_SECRET"
$s3 = Get-CloudS3Connection -Key $key -Secret $secret -UseSSL
```

4\. 設定要同步兩端檔案的**來源**與**目的**物件

  設定來源物件 (假設我要從本機磁碟把某個目錄同步到 S3，可以用以下語法指定一個本機磁碟機)

```
$src = Get-CloudFilesystemConnection | Select-CloudFolder "G:\TEMP"
```

  設定目的物件 (假設我要從本機磁碟把某個目錄同步到 S3，可以用以下語法指定一個 Bucket 路徑)

```
$dest = $s3 | Select-CloudFolder -path "your_bucket_name/vPath"
```

5\. 將來源複製到目的 ( 這是直接從 $src 複製所有檔案到 $dest 喔，預設 Storage Class 為 STANDARD )

```
$src | Copy-CloudItem $dest -filter *
```

  如果要指定複製上去的檔案預設的 Storage Class 為 [RRS](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingRRS.html) 的話，可以改用以下指令：

```
$src | Copy-CloudItem $dest -filter * -StorageClass rrs
```

  **注意**：使用 Copy-CloudItem 來複製檔案，並不會檢查檔案是否上傳過，所以僅適合用在第一次上傳或下載檔案的時候。

6\. 同步兩個資料夾 ( 已經同步過的檔案不會再複製，同步效率極高 )

  **從本地磁碟同步到雲端**

```
$src | Copy-CloudSyncFolders $dest -IncludeSubfolders
```

  **從雲端同步到本地磁碟**

```
$dest | Copy-CloudSyncFolders $src -IncludeSubfolders
```

7\. 手動將檔案調整為 RRS

  如果你不小心上傳的檔案被標示為 STANDARD，而想改成 RRS 儲存類別 (Storage Class) 的話，可以參考以下指令：

```
$bucket = $s3 | Select-CloudFolder -Path your_bucket_name
$item = $bucket | Get-CloudItem -Filter vPath
Set-CloudItemStorageClass -Item $item -StorageClass rrs
```

**相關連結**

-   [S3 Explorer PowerShell Snap-in | CloudBerry Lab](http://www.cloudberrylab.com/default.aspx?page=amazon-s3-powershell)
-   [AWS | Amazon Simple Storage Service (S3)](http://aws.amazon.com/s3/)
-   [Using Reduced Redundancy Storage](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingRRS.html)
