使用 cURL
您可以通过 IBM Cloud® Object Storage 和 cURL
在大多数环境中充分使用命令行。
下面是 IBM Cloud® Object Storage REST API 的基本 curl
命令的“备忘单”。 在 API 参考中可以找到有关存储区或对象的更多详细信息。
使用 curl
即假定您在一定程度上熟悉命令行和对象存储器,并且已有服务凭证、端点引用或控制台中的必要信息。
如果遇到任何不熟悉的术语或变量,可以在词汇表中找到这些术语或变量。
注: 个人可标识信息 (PII): 在 命名 存储区或对象时,请勿使用任何可通过名称,位置或任何其他方法来标识任何用户 (自然人) 的信息。
请求 IAM 令牌
有两种方法可生成用于认证请求的 IAM oauth
令牌:使用 curl
命令和 API 密钥(如后文所述),或通过使用 IBM Cloud®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"
获取资源实例标识
以下某些命令需要 ibm-service-instance-id
参数。 要查找此值,请在云控制台中,转至 Object Storage 实例的服务凭证选项卡。 根据需要创建凭证,然后使用查看凭证菜单来查看 JSON 格式。 请使用 resource_instance_id
的值。
要用于 curl API,您只需要 UUID,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
后续步骤
可在 S3 兼容性 API 文档 或 配置 API 文档中找到 IBM Cloud Object Storage 的 RESTful API 的详细描述。