IBM Cloud: ICOS(IBM Cloud Object Storage)のバージョニングされたObjectへのアクセス方法

# 1. はじめに
IBM Cloud Object Storage(ICOS)でもバージョニングをサポートしている。しかし、[IBM Cloudのdocs](https://cloud.ibm.com/docs/cloud-object-storage?topic=cloud-object-storage-curl)ではバージョニングされたObjectについてのアクセス方法に対して明示的に記載されていない（CLIのヘルプとかを見たり、APIがS3互換なので[AWS社のdocs](https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/userguide/RetrievingObjectVersions.html)も参考になるが）ので、念の為U/CLI/APIのそれぞれで簡単に動作確認してみた。

# 2. UIを利用したケース

## 2-1. バージョン表示なし（最新のObjectのみにアクセス）
- Objectのリスト ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/62127/0612b9b9-6b14-9dde-745b-b420f73171dc.png)
## 2-2. バージョン利用時
- Objectのリスト ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/62127/03ed8b39-2c7e-56c5-f0a9-c710c428c71a.png)

# 3. IBM Cloud CLIを利用したケース

## 3-1. バージョン表示なし（最新のObjectのみにアクセス）
```txt:Objectのリスト
[root@new-syasuda-tok1-vpc1 ~]# ibmcloud cos objects  --bucket mybucket106 --prefix testdata1.txt
OK
Found 1 object in bucket 'mybucket106':

Name            Last Modified              Object Size
testdata1.txt   Apr 02, 2023 at 23:51:58   36 B
```
```txt:Objectのダウンロード
[root@new-syasuda-tok1-vpc1 ~]# ibmcloud cos object-get --bucket mybucket106 --key testdata1.txt testdata1.txt
OK
Successfully downloaded 'testdata1.txt' from bucket 'mybucket106'
Version ID: 00000187-4462-8107-0c47-9f07caedafe8
36 B downloaded.

[root@new-syasuda-tok1-vpc1 ~]# cat testdata1.txt
08:51 This is updated as version 13
```

## 3-2. バージョン利用時

```txt:Objectのリスト
[root@new-syasuda-tok1-vpc1 ~]# ibmcloud cos object-versions --bucket mybucket106 --prefix testdata1.txt
OK
Found 7 object versions in bucket 'mybucket106':

Name            Version ID                             Last Modified              Object Size   Is Latest
testdata1.txt   00000187-4462-8107-0c47-9f07caedafe8   Apr 02, 2023 at 23:51:58   36 B          true
testdata1.txt   00000187-4457-d2b5-2372-4706dbab607d   Apr 02, 2023 at 23:40:18   36 B          false
testdata1.txt   00000187-4457-6638-27bf-a8bad8e3f5fb   Apr 02, 2023 at 23:39:51   35 B          false
testdata1.txt   00000182-18c0-92fb-8aaf-ad6fd50d4b93   Jul 19, 2022 at 23:17:29   25.48 KiB     false
testdata1.txt   00000181-a7a4-c9a4-fc5d-bf8c9d7bb5f8   Jun 28, 2022 at 00:10:03   25.48 KiB     false
testdata1.txt   00000181-a7a3-862f-3e9c-f630c579b6e4   Jun 28, 2022 at 00:08:40   25.48 KiB     false
testdata1.txt   00000181-a79b-439c-2324-e31d2dc1e122   Jun 27, 2022 at 23:59:39   25.48 KiB     false

Found 4 delete markers in bucket 'mybucket106':

Name            Version ID                             Last Modified              Is Latest
testdata1.txt   00000181-a7a5-ff35-0db3-52d42d20915f   Jun 28, 2022 at 00:11:22   false
testdata1.txt   00000181-a7a4-73bd-9fd9-6879565c66a7   Jun 28, 2022 at 00:09:41   false
testdata1.txt   00000181-a79e-bd55-7a39-cf4efd178446   Jun 28, 2022 at 00:03:27   false
testdata1.txt   00000181-a79b-92bf-49d6-b7b6ab05588f   Jun 27, 2022 at 23:59:59   false
```
```txt:Objectのダウンロード（バージョン指定）
[root@new-syasuda-tok1-vpc1 ~]# ibmcloud cos object-get --bucket mybucket106 --key testdata1.txt --version-id 00000187-4457-6638-27bf-a8bad8e3f5fb testdata1.txt_00000187-4457-6638-27bf-a8bad8e3f5fb
OK
Successfully downloaded 'testdata1.txt' from bucket 'mybucket106'
Version ID: 00000187-4457-6638-27bf-a8bad8e3f5fb
35 B downloaded.

[root@new-syasuda-tok1-vpc1 ~]# cat testdata1.txt_00000187-4457-6638-27bf-a8bad8e3f5fb
08:39 This is updated as version 10
```

# 4. APIを利用したケース

[ここ](https://qiita.com/testnin2/items/3a4ca8a1a2b79b713c48)を参考に、あらかじめIAM Tokenは取得済みであるという想定。

```
$ APIKEY=xxxxxx
$ iam_token=`curl -sX POST \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=${APIKEY}" \
  https://iam.cloud.ibm.com/identity/token | jq -r '.access_token' `
```

## 4-1. バージョン表示なし（最新のObjectのみにアクセス）

```
[root@new-syasuda-tok1-vpc1 ~]# curl -s -H "Authorization: bearer ${iam_token}" https://mybucket106.s3.jp-tok.cloud-object-storage.appdomain.cloud/?prefix=testdata1.txt | xmllint --format -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <Name>mybucket106</Name>
  <Prefix>testdata1.txt</Prefix>
  <Marker/>
  <MaxKeys>1000</MaxKeys>
  <Delimiter/>
  <IsTruncated>false</IsTruncated>
  <Contents>
    <Key>testdata1.txt</Key>
    <LastModified>2023-04-02T23:51:58.983Z</LastModified>
    <ETag>"b98714d5a43edd609928b3efd6026dcf"</ETag>
    <Size>36</Size>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Contents>
</ListBucketResult>

[root@new-syasuda-tok1-vpc1 ~]# curl -s -H "Authorization: bearer ${iam_token}" https://mybucket106.s3.jp-tok.cloud-object-storage.appdomain.cloud/testdata1.txt
08:51 This is updated as version 13
```

## 4-2. バージョン利用時


```
[root@new-syasuda-tok1-vpc1 ~]# curl -s -H "Authorization: bearer ${iam_token}" "https://mybucket106.s3.jp-tok.cloud-object-storage.appdomain.cloud/?prefix=testdata1.txt&versions=" | xmllint --format -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ListVersionsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <Name>mybucket106</Name>
  <Prefix>testdata1.txt</Prefix>
  <KeyMarker/>
  <VersionIdMarker/>
  <MaxKeys>1000</MaxKeys>
  <Delimiter/>
  <IsTruncated>false</IsTruncated>
  <Version>
    <Key>testdata1.txt</Key>
    <VersionId>00000187-4462-8107-0c47-9f07caedafe8</VersionId>
    <IsLatest>true</IsLatest>
    <LastModified>2023-04-02T23:51:58.983Z</LastModified>
    <ETag>"b98714d5a43edd609928b3efd6026dcf"</ETag>
    <Size>36</Size>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Version>
  <Version>
    <Key>testdata1.txt</Key>
    <VersionId>00000187-4457-d2b5-2372-4706dbab607d</VersionId>
    <IsLatest>false</IsLatest>
    <LastModified>2023-04-02T23:40:18.997Z</LastModified>
    <ETag>"041de89894c1270662188c2858135526"</ETag>
    <Size>36</Size>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Version>
  <Version>
    <Key>testdata1.txt</Key>
    <VersionId>00000187-4457-6638-27bf-a8bad8e3f5fb</VersionId>
    <IsLatest>false</IsLatest>
    <LastModified>2023-04-02T23:39:51.224Z</LastModified>
    <ETag>"a62fe106b8c1a8ce879a25ac41290049"</ETag>
    <Size>35</Size>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Version>
  <Version>
    <Key>testdata1.txt</Key>
    <VersionId>00000182-18c0-92fb-8aaf-ad6fd50d4b93</VersionId>
    <IsLatest>false</IsLatest>
    <LastModified>2022-07-19T23:17:29.979Z</LastModified>
    <ETag>"2f09e3d1d8e06f9cf1b3b80e88b80061"</ETag>
    <Size>26091</Size>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Version>
  <DeleteMarker>
    <Key>testdata1.txt</Key>
    <VersionId>00000181-a7a5-ff35-0db3-52d42d20915f</VersionId>
    <IsLatest>false</IsLatest>
    <LastModified>2022-06-28T00:11:22.805Z</LastModified>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
  </DeleteMarker>
  <Version>
    <Key>testdata1.txt</Key>
    <VersionId>00000181-a7a4-c9a4-fc5d-bf8c9d7bb5f8</VersionId>
    <IsLatest>false</IsLatest>
    <LastModified>2022-06-28T00:10:03.556Z</LastModified>
    <ETag>"2f09e3d1d8e06f9cf1b3b80e88b80061"</ETag>
    <Size>26091</Size>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Version>
  <DeleteMarker>
    <Key>testdata1.txt</Key>
    <VersionId>00000181-a7a4-73bd-9fd9-6879565c66a7</VersionId>
    <IsLatest>false</IsLatest>
    <LastModified>2022-06-28T00:09:41.565Z</LastModified>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
  </DeleteMarker>
  <Version>
    <Key>testdata1.txt</Key>
    <VersionId>00000181-a7a3-862f-3e9c-f630c579b6e4</VersionId>
    <IsLatest>false</IsLatest>
    <LastModified>2022-06-28T00:08:40.751Z</LastModified>
    <ETag>"2f09e3d1d8e06f9cf1b3b80e88b80061"</ETag>
    <Size>26091</Size>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Version>
  <DeleteMarker>
    <Key>testdata1.txt</Key>
    <VersionId>00000181-a79e-bd55-7a39-cf4efd178446</VersionId>
    <IsLatest>false</IsLatest>
    <LastModified>2022-06-28T00:03:27.189Z</LastModified>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
  </DeleteMarker>
  <DeleteMarker>
    <Key>testdata1.txt</Key>
    <VersionId>00000181-a79b-92bf-49d6-b7b6ab05588f</VersionId>
    <IsLatest>false</IsLatest>
    <LastModified>2022-06-27T23:59:59.679Z</LastModified>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
  </DeleteMarker>
  <Version>
    <Key>testdata1.txt</Key>
    <VersionId>00000181-a79b-439c-2324-e31d2dc1e122</VersionId>
    <IsLatest>false</IsLatest>
    <LastModified>2022-06-27T23:59:39.420Z</LastModified>
    <ETag>"2f09e3d1d8e06f9cf1b3b80e88b80061"</ETag>
    <Size>26091</Size>
    <Owner>
      <ID>2c66d594-86d5-4a50-ba34-cedb18fe3126</ID>
      <DisplayName>2c66d594-86d5-4a50-ba34-cedb18fe3126</DisplayName>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Version>
</ListVersionsResult>


[root@new-syasuda-tok1-vpc1 ~]# curl -s -H "Authorization: bearer ${iam_token}" https://mybucket106.s3.jp-tok.cloud-object-storage.appdomain.cloud/testdata1.txt?versionId=00000187-4457-6638-27bf-a8bad8e3f5fb
08:39 This is updated as version 10
```