使用 cURL

您可以使用 IBM Cloud® Object Storage 和 cURL 在大多數環境中充分使用指令行。

以下是 IBM Cloud® Object Storage REST API 基本 curl 指令的「提要」。 您可以在儲存區物件的 API 參考資料中找到更多詳細資料。

使用 curl 假設您對指令行和 Object Storage 具有一定程度的熟悉程度,並且具有來自服務認證端點參考資料主控台的必要資訊。 如果不熟悉任何術語或變數,可以在名詞解釋中找到它們。

附註: 個人識別資訊 (PII): 當 命名 儲存區或物件時,請勿使用任何可依名稱、位置或任何其他方法來識別任何使用者 (自然人) 的資訊。

要求 IAM 記號

您可以產生來鑑別要求的 IAM oauth 記號的兩種方法,會搭配使用 curl 指令與 API 金鑰(如稍後所述),或使用 IBM Cloud® CLI 從指令行中使用。

使用 API 金鑰要求 IAM 記號

請確定您有 API 金鑰。 您可以從 IBM Cloud® Identity and Access Management 中取得 API 金鑰。

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 參數。 若要尋找此值,請在雲端主控台中移至 Object Storage 實例的服務認證標籤。 視需要建立認證,然後使用檢視認證 功能表以查看 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)"

取得儲存區 meta 資料

配置 API 端點的使用與儲存區本身的端點不同。 使用以下指令可傳回所指定儲存區的 meta 資料。

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)"

配置靜態網站

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 相容性 API 文件配置 API 文件中找到。