2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS CLIでS3 Vectors を操作する

Posted at

背景・目的

以前、S3 Vectorsを試しました。今回はCLIで操作し理解を深めてみます。

実践

CLI のセットアップ

  1. 現状を確認します
    % aws --version
    aws-cli/2.23.11 Python/3.12.6 Darwin/24.5.0 exe/x86_64
    % 
    
  2. パスを確認します
    % which aws
    /usr/local/bin/aws
    % 
    
  3. 削除します
    % sudo rm /usr/local/bin/aws
    % 
    
  4. インストールします
    % curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
    
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 37.7M  100 37.7M    0     0  24.9M      0  0:00:01  0:00:01 --:--:-- 24.9M
    % 
    
  5. mac OS installerプログラムを実行します
    % sudo installer -pkg ./AWSCLIV2.pkg -target /
    
  6. バージョンを確認します。 2.23.11→2.27.56へバージョンアップされました
    % which aws
    /usr/local/bin/aws
    % aws --version
    aws-cli/2.27.56 Python/3.13.4 Darwin/24.5.0 exe/x86_64
    % 
    

CLIで操作する

バケットの作成

  1. 下記でバケットを作成します
    % aws s3vectors create-vector-bucket \
    --vector-bucket-name $VECTOR_BUCKET
    % 
    
  2. 作成されました
    % aws s3vectors get-vector-bucket \   
    --vector-bucket-name $VECTOR_BUCKET  
    {
        "vectorBucket": {
            "vectorBucketName": "XXXXXX",
            "vectorBucketArn": "arn:aws:s3vectors:us-west-2:XXXXX:bucket/XXXXXX",
            "creationTime": "2025-07-22T14:01:51+09:00",
            "encryptionConfiguration": {
                "sseType": "AES256"
            }
        }
    }
    % 
    

インデックスの作成

  1. 下記でインデックスを作成します
    % aws s3vectors \
    > create-index \
    > --vector-bucket-name $VECTOR_BUCKET \
    > --index-name $VECTOR_INDEX \
    > --data-type float32 \
    > --dimension 1024 \
    > --distance-metric cosine
    % 
    
  2. 作成されました
    % aws s3vectors \
    > get-index \
    > --vector-bucket-name $VECTOR_BUCKET \
    > --index-name $VECTOR_INDEX  
    {
        "index": {
            "vectorBucketName": "XXXXXX",
            "indexName": "XXXXXX",
            "indexArn": "arn:aws:s3vectors:us-west-2:XXXXXXXX:bucket/XXXXXX/index/XXXXXX",
            "creationTime": "2025-07-22T14:08:39+09:00",
            "dataType": "float32",
            "dimension": 1024,
            "distanceMetric": "cosine"
        }
    }
    % 
    

ベクトルの登録

ベクトルの生成

JSONの作成
  1. 入力文章のJSONを生成します

    % cat <<EOF > input.json
    heredoc> {
    heredoc>   "inputText": "S3 Vectors is a scalable solution for vector storage in the cloud."
    heredoc> }
    heredoc> EOF
    % 
    
  2. 確認します

    % cat input.json 
    {
      "inputText": "S3 Vectors is a scalable solution for vector storage in the cloud."
    }
    % 
    
Titan Embeddingsでベクトル生成
  1. Bedrockを使ってベクトルを生成します
    % aws bedrock-runtime invoke-model \
      --region us-west-2 \
    >   --model-id amazon.titan-embed-text-v2:0 \
    >   --body fileb://input.json \
    >   --content-type application/json \
    >   --accept application/json \
    >   output.json
    {
        "contentType": "application/json"
    }
    % 
    
  2. ベクトルだけ抽出します
    % jq -c '.embedding' output.json > vector_data.json
    
put-vectors 用のJSONを作成
  1. put-vectors用のJSONを作成します(keyやmetadataを付与します)

    % cat <<EOF > vector_upload.json
    [
      {
        "key": "doc1",
        "data": {
          "float32": $(cat vector_data.json)
        },
        "metadata": {
          "source_text": "S3 Vectors is a scalable solution for vector storage in the cloud.",
          "category": "tutorial"
        }
      }
    ]
    EOF
    
    % 
    
  2. できました

    % ls -ltr                    
    total 208
    -rw-r--r--@ 1 XXXX  XXXX     88  7 22 14:25 input.json
    -rw-r--r--@ 1 XXXX  XXXX  43429  7 22 14:29 output.json
    -rw-r--r--@ 1 XXXX  XXXX  21716  7 22 14:31 vector_data.json
    -rw-r--r--@ 1 XXXX  XXXX  21929  7 22 14:33 vector_upload.json
    % 
    
S3 Vectorsにベクトルを登録
  1. 下記のコマンドでベクトルを登録します
    % aws s3vectors put-vectors \
    > --vector-bucket-name $VECTOR_BUCKET \
    > --index-name $VECTOR_INDEX \
    > --vectors file://vector_upload.json
    % 
    

検索(一覧)

  1. 一覧を確認します。登録した内容が確認できました
    % aws s3vectors \            
    > list-vectors \
    > --vector-bucket-name $VECTOR_BUCKET \
    >  --index-name $VECTOR_INDEX  
    {
        "vectors": [
            {
                "key": "doc1"
            }
        ]
    }
    % 
    

ID検索

  1. 下記のコマンドで結果を確認します
    % aws s3vectors get-vectors \
    --vector-bucket-name $VECTOR_BUCKET \
    --index-name $VECTOR_INDEX \
    --keys doc1 \
    --return-data \
    --return-metadata > id-output.json
    % 
    
  2. 中身を確認します
    % head id-output.json
    {
        "vectors": [
            {
                "key": "doc1",
                "data": {
                    "float32": [
                        -0.06158860772848129,
                        0.029729574918746948,
                        0.026248633861541748,
                        0.020681647583842278,
                        ・・・
    % tail id-output.json 
                        -0.010364331305027008
                    ]
                },
                "metadata": {
                    "category": "tutorial",
                    "source_text": "S3 Vectors is a scalable solution for vector storage in the cloud."
                }
            }
        ]
    }
    % 
    

類似検索

  1. クエリを作成します

    % cat <<EOF > input.json
    heredoc> {
      "inputText": "What is S3 Vectors used for?"
    }
    EOF
    
    % cat input.json 
    {
      "inputText": "What is S3 Vectors used for?"
    }
    % 
    
  2. Titaln Embeddingsでベクトル化します

    % aws bedrock-runtime invoke-model \
    > --region us-west-2 \
    > --model-id amazon.titan-embed-text-v2:0 \
    > --body fileb://input.json \
    > --content-type application/json \
    > --accept application/json \
    > output.json
    {
        "contentType": "application/json"
    }
    % 
    
  3. ベクトル値を取り出して検索用に使う

    jq '{float32: .embedding}' output.json > embedding.json
    
  4. query-vectorで検索します

    aws s3vectors query-vectors \
      --vector-bucket-name $VECTOR_BUCKET \
      --index-name $VECTOR_INDEX \
      --top-k 3 \
      --query-vector file://embedding.json \
      --return-metadata \
      > query-result.json
    
    
  5. 確認します

    % cat query-result.json 
    {
        "vectors": [
            {
                "key": "doc1",
                "metadata": {
                    "source_text": "S3 Vectors is a scalable solution for vector storage in the cloud.",
                    "category": "tutorial"
                }
            }
        ]
    }
    % 
    

考察

今回は、S3 VectorsをCLIで操作してみました。次回以降、OpenSearchやBedrock Knowledgebaseと統合を試してみます。

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?