cURL
の使用
IBM Cloud® Object Storage および cURL
を使用して、ほとんどの環境でコマンド・ラインを活用できます。
以下に、IBM Cloud® Object Storage REST API の基本的な curl
コマンドの「虎の巻」を示します。 追加の詳細については、バケットまたはオブジェクトの
API リファレンスを参照してください。
curl
の使用では、コマンド・ラインおよびオブジェクト・ストレージに関するある程度の知識があることと、サービス資格情報、エンドポイント参照、または
コンソールからの必要な情報があることが前提となっています。 分からない用語や変数については、用語集を参照してください。
注: 個人情報 (PII): バケットまたはオブジェクトに 名前を付ける 場合は、名前、場所、またはその他の方法でユーザー (個人) を識別できる情報を使用しないでください。
IAM トークンの要求
要求を認証するための IAM oauth
トークンを生成できる 2 つの方法は、API キーを指定して curl
コマンドを使用する方法 (以下で説明) と、IBM Cloud® CLI を使用してコマンド・ラインから行う方法です。
API キーを使用した IAM トークンの要求
API キーがあることを確認します。 これは IBM Cloud® Identity and Access Management から入手できます。
curl -X "POST" "https://iam.cloud.ibm.com/oidc/token" \
-H 'Accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode "apikey={api-key}" \
--data-urlencode "response_type=cloud_iam" \
--data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey"
リソース・インスタンス ID の取得
以下のコマンドの一部には、ibm-service-instance-id
パラメーターが必要です。 この値を見つけるには、クラウド・コンソールでオブジェクト・ストレージ・インスタンスの**「サービス資格情報」**タブに移動します。 必要に応じて資格情報を作成し、*「資格情報の表示」*メニューを使用して JSON 形式を表示します。 resource_instance_id
の値を使用します。
curl API で使用する場合は、最後の単一コロンの後から始まり、最後の二重コロンの前に終わる UUID のみが必要です。 例えば、ID crn:v1:bluemix:public:cloud-object-storage:global:a/81caa0254631ce5f9330ae427618f209:39d8d161-22c4-4b77-a856-f11db5130d7d::
は、39d8d161-22c4-4b77-a856-f11db5130d7d
に短縮できます。
バケットのリスト
curl "https://(endpoint)/"
-H "Authorization: bearer (token)"
-H "ibm-service-instance-id: (resource-instance-id)"
バケットの追加
curl -X "PUT" "https://(endpoint)/(bucket-name)"
-H "Authorization: Bearer (token)"
-H "ibm-service-instance-id: (resource-instance-id)"
バケットの追加 (ストレージ・クラス)
curl -X "PUT" "https://(endpoint)/(bucket-name)"
-H "Content-Type: text/plain; charset=utf-8"
-H "Authorization: Bearer (token)"
-H "ibm-service-instance-id: (resource-instance-id)"
-d "<CreateBucketConfiguration>
<LocationConstraint>(provisioning-code)</LocationConstraint>
</CreateBucketConfiguration>"
LocationConstraint
の有効なコードのリストについては、ストレージ・クラス・ガイドを参照してください。
バケット CORS の作成
curl -X "PUT" "https://(endpoint)/(bucket-name)/?cors"
-H "Content-MD5: (md5-hash)"
-H "Authorization: bearer (token)"
-H "Content-Type: text/plain; charset=utf-8"
-d "<CORSConfiguration>
<CORSRule>
<AllowedOrigin>(url)</AllowedOrigin>
<AllowedMethod>(request-type)</AllowedMethod>
<AllowedHeader>(url)</AllowedHeader>
</CORSRule>
</CORSConfiguration>"
Content-MD5
ヘッダーは、base64 エンコードの MD5 ハッシュのバイナリー表記である必要があります。
echo -n (XML block) | openssl dgst -md5 -binary | openssl enc -base64
バケット CORS の取得
curl "https://(endpoint)/(bucket-name)/?cors"
-H "Authorization: bearer (token)"
バケット CORS の削除
curl -X "DELETE" "https://(endpoint)/(bucket-name)/?cors"
-H "Authorization: bearer (token)"
オブジェクトのリスト表示
curl "https://(endpoint)/(bucket-name)"
-H "Authorization: bearer (token)"
バケット・ヘッダーの取得
curl --head "https://(endpoint)/(bucket-name)/"
-H "Authorization: bearer (token)"
バケット・メタデータの取得
構成 API エンドポイントの使用は、バケット自体のエンドポイントと同じではありません。 次のコマンドを使用すると、指定したバケットのメタデータが返されます。
curl https://config.cloud-object-storage.cloud.ibm.com/v1/b/{my-bucket} \
-H 'authorization: bearer <IAM_token>'
バケットの削除
curl -X "DELETE" "https://(endpoint)/(bucket-name)/"
-H "Authorization: bearer (token)"
オブジェクトのアップロード
curl -X "PUT" "https://(endpoint)/(bucket-name)/(object-key)" \
-H "Authorization: bearer (token)" \
-H "Content-Type: (content-type)" \
-d "(object-contents)"
オブジェクトのヘッダーの取得
curl --head "https://(endpoint)/(bucket-name)/(object-key)"
-H "Authorization: bearer (token)"
オブジェクトのコピー
curl -X "PUT" "https://(endpoint)/(bucket-name)/(object-key)"
-H "Authorization: bearer (token)"
-H "x-amz-copy-source: /(bucket-name)/(object-key)"
CORS 情報の確認
curl -X "OPTIONS" "https://(endpoint)/(bucket-name)/(object-key)"
-H "Access-Control-Request-Method: PUT"
-H "Origin: http://(url)"
オブジェクトのダウンロード
curl "https://(endpoint)/(bucket-name)/(object-key)"
-H "Authorization: bearer (token)"
オブジェクトの ACL の確認
curl "https://(endpoint)/(bucket-name)/(object-key)?acl"
-H "Authorization: bearer (token)"
ファイアウォールの有効化
構成 API エンドポイントの使用は、バケット自体のエンドポイントと同じではありません。 次のコマンドを使用すると、指定したバケットに対してファイアウォールが有効になります。 ファイアウォールがアクティブな場合、他の IBM Cloud® サービスはバケットにアクセスできません。
curl -X PATCH https://config.cloud-object-storage.cloud.ibm.com/v1/b/{my-bucket} \
-H 'authorization: bearer $IAM_TOKEN' \
-d '{"firewall": {"allowed_ip": ["10.142.175.0/22", "10.198.243.79"]}}'
アクティビティー・トラッキングの有効化
構成 API エンドポイントの使用は、バケット自体のエンドポイントと同じではないことに注意してください。 次のコマンドを使用すると、指定したバケットに対してアクティビティー・トラッキングが有効になります。
curl -X PATCH https://config.cloud-object-storage.cloud.ibm.com/v1/b/{my-bucket} \
-H 'authorization: bearer <IAM_token>' \
-d '{"activity_tracking": { \
"read_data_events": True, \
"write_data_events": True}'
オブジェクトへの匿名アクセスの許可
curl -X "PUT" "https://(endpoint)/(bucket-name)/(object-key)?acl"
-H "Content-Type: (content-type)"
-H "Authorization: bearer (token)"
-H "x-amz-acl: public-read"
オブジェクトの削除
curl -X "DELETE" "https://(endpoint)/(bucket-name)/(object-key)"
-H "Authorization: bearer (token)"
多数のオブジェクトの削除
curl -X "POST" "https://(endpoint)/(bucket-name)?delete"
-H "Content-MD5: (md5-hash)"
-H "Authorization: bearer (token)"
-H "Content-Type: text/plain; charset=utf-8"
-d "<?xml version="1.0" encoding="UTF-8"?>
<Delete>
<Object>
<Key>(first-object)</Key>
</Object>
<Object>
<Key>(second-object)</Key>
</Object>
</Delete>"
Content-MD5
ヘッダーは、base64 エンコードの MD5 ハッシュのバイナリー表記である必要があります。
echo -n (XML block) | openssl dgst -md5 -binary | openssl enc -base64
マルチパート・アップロードの開始
curl -X "POST" "https://(endpoint)/(bucket-name)/(object-key)?uploads"
-H "Authorization: bearer (token)"
パーツをアップロードします
curl -X "PUT" "https://(endpoint)/(bucket-name)/(object-key)?partNumber=(sequential-integer)&uploadId=(upload-id)"
-H "Authorization: bearer (token)"
-H "Content-Type: (content-type)"
マルチパート・アップロードの完了
curl -X "POST" "https://(endpoint)/(bucket-name)/(object-key)?uploadId=(upload-id)"
-H "Authorization: bearer (token)"
-H "Content-Type: text/plain; charset=utf-8"
-d "<CompleteMultipartUpload>
<Part>
<PartNumber>1</PartNumber>
<ETag>(etag)</ETag>
</Part>
<Part>
<PartNumber>2</PartNumber>
<ETag>(etag)</ETag>
</Part>
</CompleteMultipartUpload>"
不完全なマルチパート・アップロードの取得
curl "https://(endpoint)/(bucket-name)/?uploads"
-H "Authorization: bearer (token)"
未完了のマルチパート・アップロードの停止
curl -X "DELETE" "https://(endpoint)/(bucket-name)/(object-key)?uploadId"
-H "Authorization: bearer (token)"
静的 Web サイトの構成
curl --location --request PUT 'https://<endpoint>/<bucketname>?website' \
--header 'Authorization: bearer <token>' --header 'ibm-service-instance-id: <resource_instance_id> \
--header 'Content-MD5: <hashed-output>' --header 'Content-Type: text/plain' \
--data-raw '<WebsiteConfiguration>
<IndexDocument>
<Suffix>index.html</Suffix>
</IndexDocument>
<ErrorDocument>
<Key>error.html</Key>
</ErrorDocument>
</WebsiteConfiguration>'
注意として、 Content-MD5
ヘッダーは、 base64-encoded MD5 ハッシュのバイナリー表記でなければなりません。
echo -n (XML block) | openssl dgst -md5 -binary | openssl enc -base64
次のステップ
IBM Cloud Object Storage の RESTful API の詳細な説明については、 S3 Compatibility API Documentation または Configuration API Documentationを参照してください。