gsutilでのCloud Storageの操作メモ
久しぶりのGCP。
gsutilでCloud Storageの操作方法のメモ。
バケットの一覧確認
% gsutil ls gs:// gs://hapoon-sample-0/ gs://hapoon-sample-1/
バケットの作成
バケット名だけ指定
% gsutil mb gs://hapoon-sample-1 Creating gs://hapoon-sample-1/... % gsutil ls -L -b gs://hapoon-sample-1 gs://hapoon-sample-1/ : Storage class: STANDARD Location constraint: US Versioning enabled: None Logging configuration: None Website configuration: None CORS configuration: None Lifecycle configuration: None Time created: Fri, 10 Mar 2017 03:45:02 GMT Time updated: Fri, 10 Mar 2017 03:45:02 GMT ACL: 〜 Default ACL: 〜
クラスとロケーションの指定なしだと、クラスはSTANDARD、ロケーションはUSになる。
https://cloud.google.com/storage/docs/storage-classesのドキュメントによると、
注: API でバケットを作成し、デフォルトのストレージ クラスを指定しないと、バケットは Standard Storage クラスに割り当てられます。このクラスは、バケットの場所設定に応じて Multi-Regional Storage または Regional Storage と同等になります。
とある。
ストレージクラスを指定
% gsutil mb -c nearline gs://hapoon-sample-2 Creating gs://hapoon-sample-2/... % gsutil ls -L -b gs://hapoon-sample-2 gs://hapoon-sample-2/ : Storage class: NEARLINE Location constraint: US
指定できるストレージクラスはhttps://cloud.google.com/storage/docs/storage-classesを参照。
ちなみに regional を指定した時は、ロケーションを指定しないとエラーになります。
% gsutil mb -c regional gs://hapoon-sample-2 Creating gs://hapoon-sample-2/... BadRequestException: 400 The combination of locationConstraint and storageClass you provided is not supported for your project
指定できるリージョンはhttps://cloud.google.com/storage/docs/bucket-locationsのリージョンのロケーションを参照。
ロケーションを指定
マルチリージョンを指定した場合
% gsutil mb -l asia gs://hapoon-sample-1 Creating gs://hapoon-sample-1/... % gsutil ls -L -b gs://hapoon-sample-1 gs://hapoon-sample-1/ : Storage class: STANDARD Location constraint: ASIA Versioning enabled: None Logging configuration: None Website configuration: None CORS configuration: None Lifecycle configuration: None
ストレージクラスはSTANDARDと表示されますが、これは内部的にはmulti_regionalです。
リージョンを指定した場合
% gsutil mb -l asia-east1 gs://hapoon-sample-1 Creating gs://hapoon-sample-1/... % gsutil ls -L -b gs://hapoon-sample-1 gs://hapoon-sample-1/ : Storage class: STANDARD Location constraint: ASIA-EAST1 Versioning enabled: None Logging configuration: None Website configuration: None CORS configuration: None Lifecycle configuration: None
こちらのストレージクラスは内部的にregionalです。
ファイルの転送
コピー
% gsutil cp sample.txt gs://hapoon-sample-1/dir1/ Copying file://sample.txt [Content-Type=text/plain]... / [1 files][ 16.0 B/ 16.0 B] Operation completed over 1 objects/16.0 B. % gsutil ls gs://hapoon-sample-1/dir1 gs://hapoon-sample-1/dir1/sample.txt
移動
mvコマンド同様に元のファイルはなくなります。
% gsutil mv sample.txt gs://hapoon-sample-1/dir2/ Copying file://sample.txt [Content-Type=text/plain]... Removing file://sample.txt... B] Operation completed over 1 objects/16.0 B. % gsutil ls gs://hapoon-sample-1/dir2/ gs://hapoon-sample-1/dir2/sample.txt
バケットの使用量確認
% gsutil du -ch gs://hapoon-sample-1 16 B gs://hapoon-sample-1/dir1/sample.txt 16 B gs://hapoon-sample-1/dir1/ 16 B gs://hapoon-sample-1/dir2/sample.txt 16 B gs://hapoon-sample-1/dir2/ 32 B total
トータルだけでよければこちらでも。
% gsutil du -sh gs://hapoon-sample-1 48 B gs://hapoon-sample-1
ファイルの確認
% gsutil cat gs://hapoon-sample-1/dir1/sample.txt This is sample.
ファイルの情報
% gsutil ls -L gs://hapoon-sample-1/dir1/sample.txt gs://hapoon-sample-1/dir1/sample.txt: Creation time: Fri, 10 Mar 2017 05:16:37 GMT Update time: Fri, 10 Mar 2017 05:16:37 GMT Storage class: STANDARD Content-Language: en Content-Length: 16 Content-Type: text/plain Hash (crc32c): S+gJjg== Hash (md5): 0oY8xUSLSc/Qq0ncsJNqiQ== ETag: CKrjkrqXy9ICEAE= Generation: 1489122997088682 Metageneration: 1
バケットの削除
% gsutil rb gs://hapoon-sample-1 Removing gs://hapoon-sample-1/...
ちなみにバケット内が空でないと
% gsutil rb gs://hapoon-sample-1 Removing gs://hapoon-sample-1/... NotEmptyException: 409 BucketNotEmpty (hapoon-sample-1)
とエラーが出る。
その場合は
% gsutil rm -r gs://hapoon-sample-1/ Removing gs://hapoon-sample-1/dir1/sample.txt#1489122997088682... Removing gs://hapoon-sample-1/dir1/sample2.txt#1489124267892632... Removing gs://hapoon-sample-1/dir2/sample.txt#1489123188704896... / [3 objects] Operation completed over 3 objects. Removing gs://hapoon-sample-1/...
で中身のファイルごと削除する。