LoginSignup
2
1

More than 1 year has passed since last update.

[Oracle Cloud] AWS コマンドライン・インターフェイス (AWS CLI) で Object Storage へアクセスしてみてみた

Last updated at Posted at 2023-03-19

Oracle Cloud Infrastructure (OCI) の Object Storage は、Amazon S3互換 API で操作できます。
OCI には、次の API および対応するエンドポイントがあります。
 ・API参照エンドポイント: Amazon S3 Compatibility API
ということで、AWS コマンドライン・インターフェイス (AWS CLI) の aws s3 コマンド で OCI Object Storageを操作してみてみます。

■ AWS コマンドラインインターフェイス (AWS CLI) インストール

AWS コマンドラインインターフェイス (AWS CLI) は、AWS のサービスを管理するための統合ツールです。ダウンロードおよび設定用の単一のツールのみを使用して、コマンドラインから AWS の複数のサービスを制御し、スクリプトを使用してこれらを自動化することができます。
今回 Linux オペレーションシステムへインストールしてみてみます。
その他のOSはここを参照: AWS CLI のインストールと更新の手順

1) Downpload awscliv2.zip

curl コマンドで awscliv2.zip をダウンロード

[ec2-user@linux-inst ~]$ curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
	  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
	                                 Dload  Upload   Total   Spent    Left  Speed
	100 54.7M  100 54.7M    0     0  22.1M      0  0:00:02  0:00:02 --:--:-- 22.1M


[ec2-user@linux-inst ~]$ ls -l
	-rw-rw-r-- 1 opc opc 57397774 Mar 17 15:07 awscliv2.zip

2) unzip awscliv2.zip

[ec2-user@linux-inst ~]$ unzip awscliv2.zip
	Archive:  awscliv2.zip
	   creating: aws/
	   creating: aws/dist/
	  inflating: aws/THIRD_PARTY_LICENSES
	  inflating: aws/install
	  inflating: aws/README.md
	   creating: aws/dist/awscli/

	・・・
	  inflating: aws/dist/docutils/writers/pep_html/pep.css
	  inflating: aws/dist/docutils/writers/pep_html/template.txt


[ec2-user@linux-inst ~]$ ls -l
	total 56064
	drwxr-xr-x 3 opc opc     4096 Mar 15 21:25 aws
	-rw-rw-r-- 1 opc opc 57397774 Mar 17 15:07 awscliv2.zip

[ec2-user@linux-inst ~]$ ls -l ./aws
	total 80
	drwxr-xr-x 6 opc opc  4096 Mar 15 21:25 dist
	-rwxr-xr-x 1 opc opc  4047 Mar 15 20:33 install
	-rw-r--r-- 1 opc opc  1465 Mar 15 20:33 README.md
	-rw-r--r-- 1 opc opc 68282 Mar 15 20:33 THIRD_PARTY_LICENSES

3) Install

[ec2-user@linux-inst ~]$ sudo ./aws/install
	You can now run: /usr/local/bin/aws --version

4) Install 確認

[ec2-user@linux-inst ~]$ aws --version
	aws-cli/2.11.3 Python/3.11.2 Linux/4.14.35-2047.510.5.5.el7uek.x86_64 exe/x86_64.oracle.7 prompt/off

■ OCI 顧客秘密キー作成

Object Storage バケットは、顧客管理キーで暗号化されるため、対象バケットへアクセス権限のあるユーザーの顧客秘密キーを使用して、CLIでアクセスできるようにします。

1) OCI ユーザー詳細画面 

[アイデンティティとセキュリティ] > [ユーザー] から 対象ユーザーを選択
01_ユーザー詳細画面01.jpg

2)顧客秘密キー

リソース > [顧客秘密キー] を選択し、[秘密キーの生成]をクリック
01_顧客秘密キー作成01.jpg

3) 秘密キーの生成画面

任意の名前を設定し、[秘密キーの生成]をクリック
01_顧客秘密キー作成02.jpg

4) 生成されたキー画面

生成されたキーが表示されます。これは再度表示されません。
コピーし、[閉じる]をクリック
01_顧客秘密キー作成03.jpg

生成されたキー: ZVKhaKtYdAOPjYZkmh8BGRWqWdlML=

5) 顧客秘密キー画面

顧客秘密キーが作成され、アクセス・キーをコピーします。
01_顧客秘密キー作成04.jpg

アクセス・キー: ab9d16674e763dbd319f15a2b814a2

■ AWS CLI 設定

● config ファイル作成

aws cliをインストールしたユーザーの $HOME/.aws/config ファイルへ [default]の OCI リージョンを設定します。
awsコマンド実行時、--region オプション省略すると [default]値が使用されます。

[ec2-user@linux-inst ~]$ cat $HOME/.aws/config
	[default]
	region = ap-tokyo-1

	[oci-ashburn]
	region = us-ashburn-1

● credentials ファイル作成

aws cliをインストールしたユーザーの $HOME/.aws/credentials へ OCI画面で作成した 顧客秘密キーとアクセス・キーを設定
awsコマンド実行時、 --profile オプション省略すると [default] 値が使用されます。

構文
[<Profile名>]
aws_secret_access_key = <顧客秘密キー>
aws_access_key_id = <アクセス・キー>
設定例
[ec2-user@linux-inst ~]$ cat $HOME/.aws/credentials
	[default]
	aws_access_key_id = ab9d16674e763dbd319f15a2b814a2
	aws_secret_access_key = ZVKhaKtYdAOPjYZkmh8BGRWqWdlML=

	[oci-ashburn]
	aws_access_key_id = 4b906d3b388694ee26a04f4235533e
	aws_secret_access_key = ItzAc1mE4jrhOcdiPWJGLHysSLiIg=

■ AWS CLI で Object Storage操作

● Object Storage Endpoint確認

Object Storage の Amazon S3 Compatibility API Endpointは次を参考に設定
・ 参考: Endpoint

Tokyoリージョンの Object Storage Endpoint
https://<object_storage_namespace>.compat.objectstorage.ap-tokyo-1.oraclecloud.com

● File List

構文
aws s3 --region <Region> --endpoint-url <OBject-Sotrage-Endpoint-URL> ls s3://<Bucket-Name>/
  1. ファイル一覧表示
[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com ls s3://bucket-shirok/
	2023-03-19 12:42:24          0 test-file1
	2023-03-19 12:42:39          0 test-file2
	2023-03-19 12:42:45          0 test-file3

● File Upload

構文
aws s3 --region <Region> --endpoint-url <OBject-Sotrage-Endpoint-URL> cp <ファイル・パス> s3://<Bucket-Name>/

1) ファイル・アップロード

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com cp ./awscliv2.zip s3://bucket-shirok/
	upload: ./awscliv2.zip to s3://bucket-shirok/awscliv2.zip

2) ファイル・アップロード確認

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com ls s3://bucket-shirok/
	2023-03-19 12:46:42   57397774 awscliv2.zip
	2023-03-19 12:42:24          0 test-file1
	2023-03-19 12:42:39          0 test-file2
	2023-03-19 12:42:45          0 test-file3

● File Download

構文
aws s3 --region <Region> --endpoint-url <OBject-Sotrage-Endpoint-URL> cp  s3://<Bucket-Name>/<ファイル名> <コピー先・ファイル・パス>

1) ファイル・ダウンロード前確認

[ec2-user@linux-inst ~]$ ls -la
	total 8
	drwxrwxr-x 2 opc opc 4096 Mar 19 12:48 .
	drwx------ 7 opc opc 4096 Mar 19 12:44 ..

2) ファイル・ダウンロード

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com cp s3://bucket-shirok/test-file1 ./
	download: s3://bucket-shirok/test-file1 to ./test-file1

3) ファイル・ダウンロード確認

[ec2-user@linux-inst ~]$ ls -la
	total 8
	drwxrwxr-x 2 opc opc 4096 Mar 19 12:50 .
	drwx------ 7 opc opc 4096 Mar 19 12:44 ..
	-rw-rw-r-- 1 opc opc    0 Mar 19 12:42 test-file1

● SYNC Directory (Compute -> Object Storage)

1) Sync

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com sync ./tmp s3://bucket-oci/
	upload: tmp/test/test-file2 to s3://bucket-oci/test/test-file2
	upload: tmp/test/test-file1 to s3://bucket-oci/test/test-file1
	upload: tmp/test/test-file3 to s3://bucket-oci/test/test-file3
	upload: tmp/test/test-file4 to s3://bucket-oci/test/test-file4
	upload: tmp/awscliv2.zip to s3://bucket-oci/awscliv2.zip

2) Sync 確認

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com ls s3://bucket-oci/
	                           PRE test/
	2023-03-19 14:16:13   57397774 awscliv2.zip

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com ls s3://bucket-oci/test/
	2023-03-19 14:16:13          0 test-file1
	2023-03-19 14:16:13          0 test-file2
	2023-03-19 14:16:13          0 test-file3
	2023-03-19 14:16:13          0 test-file4

● Remove File

構文:ファイル削除
aws s3 --region <Region> --endpoint-url <OBject-Sotrage-Endpoint-URL> rm s3://<Bucket-Name>/<ファイル名> 
構文:ディレクトリ削除
aws s3 --region <Region> --endpoint-url <OBject-Sotrage-Endpoint-URL> rm s3://<Bucket-Name>/<ディレクトリ名> (--recursive)

1) File 削除前確認

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com ls s3://bucket-oci/test/
	2023-03-19 14:16:13          0 test-file1
	2023-03-19 14:16:13          0 test-file2
	2023-03-19 14:16:13          0 test-file3
	2023-03-19 14:16:13          0 test-file4

2) File 削除

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com rm s3://bucket-oci/test/test-file*
	delete: s3://bucket-oci/test/test-file4

3) File 削除確認

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com ls s3://bucket-oci/test/
	2023-03-19 14:16:13          0 test-file1
	2023-03-19 14:16:13          0 test-file2
	2023-03-19 14:16:13          0 test-file3

4) ディレクトリ内全て削除

--recursive オプジョンを付与すると全てのファイルが削除されます

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com rm s3://bucket-oci/test/ --recursive
	delete: s3://bucket-oci/test/test-file3
	delete: s3://bucket-oci/test/test-file1
	delete: s3://bucket-oci/test/test-file2

5) ディレクトリ内全て削除確認

[ec2-user@linux-inst ~]$ aws s3 --endpoint-url https://shirok.compat.objectstorage.ap-tokyo-1.oraclecloud.com ls s3://bucket-oci/test/
 <-- Null

■ 参考

 ・ AWS コマンドラインインターフェイス
 ・ AWS CLI コマンドリファレンス: s3
 ・ OCI Documentation: Amazon S3互換API
 ・ OCI Documentation: API Reference and Endpoints
 ・ OCI Documentation:オブジェクト・ストレージAPIとAmazon S3 Compatibility APIの間の相違点

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1