# AWS S3 Bucket

AWS S3 में इमेज डेटा स्टोरेज को संभालते समय और Roboflow पर अपलोड करते समय, आपके पास आम तौर पर दो विकल्प होते हैं: signed URLs का उपयोग करना या images को manually locally डाउनलोड करना (AWS CLI के माध्यम से) ताकि उन्हें locally अपलोड किया जा सके। इन तरीकों के बीच चुनाव data processing और management के लिए आपकी विशिष्ट जरूरतों पर निर्भर करता है।

* **Signed URLs**: यदि आप images को अपनी local machine पर डाउनलोड करने से जुड़ा अतिरिक्त चरण और समय-खपत से बचना चाहते हैं, तो यह तरीका विशेष रूप से लाभदायक है। एक signed URL के साथ, आप image data को S3 से सीधे Roboflow API पर बिना कभी उसे locally store किए अपलोड कर सकते हैं। इससे processing तेज़ होती है और आपकी local system पर कम load पड़ता है।
* **CLI Locally**: ऐसे scenarios हो सकते हैं जहाँ आप पहले images को अपने local environment में download करना पसंद करेंगे। उदाहरण के लिए, यदि आपको Roboflow पर upload करने से पहले images को preprocess करना है या manually check करना है, तो local copies होना लाभदायक होगा।

सही method चुनना आपकी specific use-case requirements पर निर्भर करेगा, जैसे data transfer की speed, preprocessing की आवश्यकता, या images की manual inspection।

### AWS CLI सेटअप

स्क्रिप्ट का उपयोग करने से पहले, सुनिश्चित करें कि आपने आवश्यक authentication credentials के साथ AWS CLI सेट अप कर लिया है। इससे आप इच्छित S3 bucket तक पहुंच और उसे manage कर सकेंगे।

* [AWS CLI version 2 इंस्टॉल करना](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)

#### AWS CLI को कॉन्फ़िगर करना

1. AWS CLI इंस्टॉल करने के बाद, एक terminal या command prompt खोलें।
2. निम्नलिखित command चलाएँ:

   ```bash
   aws configure
   ```
3. आपसे अपने AWS credentials दर्ज करने के लिए कहा जाएगा:

   ```
   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
   ```

### Option 1: Upload Via Signed URL:

आप Python में boto3 का उपयोग करके S3 bucket में अपनी images के लिए signed URLs generate कर सकते हैं।

```python
def generate_presigned_url(bucket_name: str, object_name: str, region: str = 'us-east-2') -> str:
    """एक S3 object के लिए presigned URL generate करें."""
    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
```

ऊपर दिए गए code snippet में, आपको अपने S3 bucket का नाम, S3 bucket में image के लिए object name, और aws region की आवश्यकता होगी। image का signed URL generate होकर वापस किया जाता है।

इसी आधार पर, हम एक complete solution बना सकते हैं जो S3 bucket में उपलब्ध सभी objects को pull करता है, और फिर उन्हें API के माध्यम से Roboflow पर upload करता है। इस solution की एक outline नीचे देखी जा सकती है:

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

# ************* इन variables को सेट करें *************
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 object के लिए presigned URL generate करें."""
    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 bucket में object keys की सूची प्राप्त करें."""
    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 पर एक image upload करें।"""
    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)

    # response code जांचें
    if response.status_code == 200:
        print(f"{img_name} को {project_name} में सफलतापूर्वक upload किया गया")
        return True
    else:
        print(f"{img_name} को upload करने में विफल। Error: {response.content.decode('utf-8')}")
        return False

if __name__ == "__main__":
    # उपलब्ध images की सूची प्राप्त करें
    available_images = get_s3_objects(S3_BUCKET_NAME)
    
    # वैकल्पिक: यहाँ images को filter करें
    # उदाहरण: available_images = [img for img in available_images if "some_condition"]
    
    # Roboflow पर images upload करें
    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 से डेटा upload करने के लिए, पहले `awscli` [command line tool](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)इंस्टॉल करें। यह tool आपको command line पर अपने AWS account के साथ interact करने की सुविधा देता है। command line tool इंस्टॉल करने के बाद, निम्नलिखित command चलाएँ:

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

इसे बदलें `mybucket` अपने bucket के नाम के साथ और `folder_path` उस folder या file के नाम के साथ जिसे आप export करना चाहते हैं। यह comamnd AWS से एक asset को आपकी current working directory में डाउनलोड करेगा (`.`).

### Roboflow पर Data Upload करें

अब जब हमने data download कर लिया है, हम इसे Roboflow पर या तो निम्न का उपयोग करके upload कर सकते हैं [Upload Web Interface](/roboflow/roboflow-hi/datasets/adding-data.md#upload-data-with-the-web-interface) या [Roboflow CLI](/developer/command-line-interface/upload-a-dataset.md).

### यह भी देखें

* [Roboflow project 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-hi/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.
