# AWS S3 Bucket

AWS S3 で画像データを保存し Roboflow にアップロードする場合、一般的には 2 つの方法があります。署名付き URL を使う方法と、AWS CLI 経由で画像を手動でローカルにダウンロードしてからアップロードする方法です。どちらを選ぶかは、データ処理と管理に関する具体的な要件によります。

* **署名付き URL**: この方法は、画像をローカルマシンにダウンロードする際に発生する追加の手順や時間を避けたい場合に特に有利です。署名付き URL を使えば、画像データをローカルに保存することなく、S3 から Roboflow API に直接アップロードできます。その結果、処理が高速になり、ローカルシステムへの負荷も軽減されます。
* **CLI でローカルに**：画像をまずローカル環境にダウンロードしたい、というケースもあるでしょう。たとえば、Roboflow にアップロードする前に画像を前処理したり、手動で確認したりする必要がある場合は、ローカルコピーがあると便利です。

適切な方法の選択は、データ転送速度、前処理の必要性、画像の手動確認など、特定のユースケース要件によって決まります。

### AWS CLI のセットアップ

スクリプトを使用する前に、必要な認証情報を使って AWS CLI をセットアップしていることを確認してください。これにより、目的の S3 バケットにアクセスして管理できるようになります。

* [AWS CLI version 2 のインストール](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)

#### AWS CLI の設定

1. AWS CLI をインストールしたら、ターミナルまたはコマンドプロンプトを開きます。
2. 次のコマンドを実行します:

   ```bash
   aws configure
   ```
3. AWS の認証情報の入力を求められます:

   ```
   AWS Access Key ID [None]: YOUR_ACCESS_KEY
   AWS Secret Access Key [None]: YOUR_SECRET_ACCESS_KEY
   Default region name [None]: YOUR_PREFERRED_REGION (例: us-west-1)
   Default output format [None]: json
   ```

### オプション 1: 署名付き URL 経由でアップロード：

Python の boto3 を使用して、S3 バケット内の画像の署名付き URL を生成できます。

```python
def generate_presigned_url(bucket_name: str, object_name: str, region: str = 'us-east-2') -> str:
    """S3 オブジェクトの presigned URL を生成します。"""
    s3 = boto3.client('s3', region_name=region, config=Config(signature_version='s3v4'))
    url = s3.generate_presigned_url('get_object',
                                    Params={'Bucket': bucket_name, 'Key': object_name},
                                    ExpiresIn=3600)
    return url
```

上記のコードスニペットでは、S3 バケット名、S3 バケット内の画像のオブジェクト名、そして aws region が必要です。画像の署名付き URL が生成され、返されます。

これを基に、S3 バケット内で利用可能なすべてのオブジェクトを取得し、それらを API 経由で Roboflow にアップロードする完全なソリューションを作成できます。このソリューションの概要は以下のとおりです:

```python
import boto3
import requests
import urllib.parse
from botocore.config import Config

# ************* これらの変数を設定してください *************
S3_BUCKET_NAME = "YOUR_S3_BUCKET_NAME"
ROBOFLOW_API_KEY = "YOUR_ROBOFLOW_API_KEY"
ROBOFLOW_PROJECT_NAME = "YOUR_ROBOFLOW_PROJECT_NAME"
# ***********************************************

def generate_presigned_url(bucket_name: str, object_name: str, region: str = 'us-east-2') -> str:
    """S3 オブジェクトの presigned URL を生成します。"""
    s3 = boto3.client('s3', region_name=region, config=Config(signature_version='s3v4'))
    url = s3.generate_presigned_url('get_object',
                                    Params={'Bucket': bucket_name, 'Key': object_name},
                                    ExpiresIn=3600)
    return url

def get_s3_objects(bucket_name: str) -> list:
    """指定された S3 バケット内のオブジェクトキーの一覧を取得します。"""
    s3 = boto3.client('s3')
    objects = []
    response = s3.list_objects_v2(Bucket=bucket_name)
    for obj in response['Contents']:
        objects.append(obj['Key'])
    return objects

def upload_to_roboflow(api_key: str, project_name: str, presigned_url: str, img_name='', split="train"):
    """画像を Roboflow にアップロードします。"""
    API_URL = "https://api.roboflow.com"
    if img_name == '':
        img_name = presigned_url.split("/")[-1]

    upload_url = "".join([
        API_URL + "/dataset/" + project_name + "/upload",
        "?api_key=" + api_key,
        "&name=" + img_name,
        "&split=" + split,
        "&image=" + urllib.parse.quote_plus(presigned_url),
    ])
    response = requests.post(upload_url)

    # レスポンスコードを確認
    if response.status_code == 200:
        print(f"Successfully uploaded {img_name} to {project_name}")
        return True
    else:
        print(f"Failed to upload {img_name}. Error: {response.content.decode('utf-8')}")
        return False

if __name__ == "__main__":
    # 利用可能な画像の一覧を取得
    available_images = get_s3_objects(S3_BUCKET_NAME)
    
    # 任意: ここで画像をフィルタリング
    # 例: available_images = [img for img in available_images if "some_condition"]
    
    # 画像を Roboflow にアップロード
    for image in available_images:
        presigned_url = generate_presigned_url(S3_BUCKET_NAME, image)
        upload_to_roboflow(ROBOFLOW_API_KEY, ROBOFLOW_PROJECT_NAME, presigned_url)

```

### オプション 2: AWS からデータをローカルにダウンロード

AWS からデータをアップロードするには、まず `awscli` [コマンドラインツール](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)をインストールします。このツールを使うと、コマンドラインから AWS アカウントを操作できます。コマンドラインツールをインストールしたら、次のコマンドを実行します:

```bash
aws s3 sync s3://mybucket/folder_path .
```

置き換えてください `mybucket` バケット名と `folder_path` には、エクスポートしたいフォルダまたはファイルの名前を指定します。このコマンドは、AWS から現在の作業ディレクトリ（`.`).

### Roboflow にデータをアップロード

データをダウンロードしたので、Roboflow には次のいずれかを使ってアップロードできます。 [Upload Web Interface](https://docs.roboflow.com/roboflow/roboflow-jp/datasets/adding-data/..#upload-data-with-the-web-interface) または [Roboflow CLI](https://app.gitbook.com/s/e5GEiPeDoFksvZv1vH3A/command-line-interface/upload-a-dataset).

### も参照

* [Roboflow のプロジェクト ID を取得する](https://docs.roboflow.com/api-reference/workspace-and-project-ids)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.roboflow.com/roboflow/roboflow-jp/datasets/adding-data/upload-data-from-aws-gcp-and-azure/aws-s3-bucket.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
