PowerShellからS3を操作してみた

2015.11.10

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

WindowsServerからS3を操作する必要があり、AWS Tools for Windows PowerShellを試してみました。

検証環境

Windows2012R2の日本語AMIを利用しました。 また、EC2インスタンスにIAM Roleを設定しました。 AMI:Windows_Server-2012-R2_RTM-Japanese-64Bit-Base-2015.10.26 (ami-6a92ff6a)

AWS Tools for Windows PowerShellのバージョンアップ

バージョン確認

PowerShellを起動して、「Get-AWSPowerShellVersion」コマンドを実行します。 今回利用したAMIにはVersion 3.1.20.0がインストールされていました。

> Get-AWSPowerShellVersion

AWS Tools for Windows PowerShell
Version 3.1.20.0
Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Amazon Web Services SDK for .NET
Version 3.1.3.2
Copyright 2009-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Release notes: https://aws.amazon.com/releasenotes/PowerShell

This software includes third party software subject to the following copyrights:
- Logging from log4net, Apache License
[http://logging.apache.org/log4net/license.html]
>

バージョンアップ

http://aws.amazon.com/powershell/ページから、[AWS Tools for Windows]を選択します。 [AWSToolsAndSDKForNet_sdk-3.1.26.1_ps-3.1.26.0_tk-1.9.6.7.msi]がダウンロードされました。

0

msiファイルをインスタンスに配置し実行します。 デフォルト設定で[Next]を選択していきました。 _1

_2_3

_5

_6

_7

PowerShellを起動して、「Get-AWSPowerShellVersion」コマンドを実行しましょう。 Version 3.1.26.0になりました。

> Get-AWSPowerShellVersion

AWS Tools for Windows PowerShell
Version 3.1.26.0
Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Amazon Web Services SDK for .NET
Version 3.1.3.5
Copyright 2009-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Release notes: https://aws.amazon.com/releasenotes/PowerShell

This software includes third party software subject to the following copyrights:
- Logging from log4net, Apache License
[http://logging.apache.org/log4net/license.html]
>

S3の操作

PowerShellからコマンドを実行します。

リージョンの指定

Tokyoリージョンを指定します。

> Initialize-AWSDefaults -Region ap-northeast-1

Administratorユーザで「Initialize-AWSDefaults」を実行した場合、 C:\Users\Administrator\AppData\Local\AWSToolkit\RegisteredAccounts.jsonが作成されます。 jsonファイルではTokyoリージョンがセットされています。

{
    "*******-****-****-****-*******" : {
        "DisplayName" : "default",
        "CredentialsType" : "Amazon.Runtime.InstanceProfileAWSCredentials",
        "Region"          : "ap-northeast-1"
    }
}

バケットの作成

> New-S3Bucket bukectname

CreationDate                                                BucketName
------------                                                ----------
2015/11/** **:**:**                                          bukectname

>

オブジェクトのアップロード

デスクトップ上のText1.txtをs3://bukectname/folder1/Text1.txtとしてアップロードします。

> Write-S3Object -BucketName bukectname -File .\Desktop\Text1.txt -Key "folder1/Text1.txt"

8

オブジェクトのダウンロード

> Copy-S3Object -BucketName bukectname -Key folder1/Text1.txt -LocalFile .\Desktop\Text1.txt

オブジェクトの削除

アップロードしたファイルを削除します。

> Remove-S3Object -BucketName bukectname -Key "folder1/Text1.txt"

おわりに

AWS Tools for Windows PowerShellを使ってみました。 他の操作については、コマンドレットリファレンスをご参照ください。

参考

AWS Documentation(Amazon S3 from the AWS Tools for Windows PowerShell) AWS Tools for Windows PowerShell Cmdlet Reference