# Google Cloud Storage

Google Cloud Storage में image data storage को handle करते समय और Roboflow पर upload करते समय, आपके पास सामान्यतः दो विकल्प होते हैं: signed URLs का उपयोग करना या images को manually local रूप से download करना (gsutil CLI के माध्यम से) ताकि उन्हें locally upload किया जा सके। इन तरीकों के बीच चुनाव आपकी data processing और management की विशिष्ट आवश्यकताओं पर निर्भर करता है।

* **Signed URLs**: यह तरीका विशेष रूप से लाभकारी है यदि आप images को अपनी local machine पर download करने से जुड़ी अतिरिक्त step और समय-खपत से बचना चाहते हैं। Signed URL के साथ, आप image data को Google Cloud Storage से सीधे Roboflow API पर upload कर सकते हैं, बिना इसे 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।

### Google Cloud JSON Key

Bucket के लिए उपयुक्त permissions के साथ एक service account बनाएं और JSON key file download करें। इस file में credentials होंगे जिनका उपयोग आपके application को authenticate करने के लिए किया जाएगा।

### Option 1: Upload Via Signed URL:

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

```python
def get_gcs_signed_url(bucket_name: str, blob_name: str) -> str:
    """एक GCS object के लिए signed URL generate करें।"""
    storage_client = storage.Client.from_service_account_json(GOOGLE_APPLICATION_CREDENTIALS)
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.get_blob(blob_name)
    
    url = blob.generate_signed_url(
        version="v4",
        expiration=3600,  # 1 घंटे सेकंड में
        method="GET"
    )
    return url
```

ऊपर दिए गए code snippet में, आपको अपने Google Cloud Storage bucket का नाम और blob name चाहिए। Image का signed URL generate होकर return किया जाता है।

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

```python
from google.cloud import storage
import requests
import urllib.parse

# ************* इन variables को सेट करें *************
GCS_BUCKET_NAME = "YOUR_GCS_BUCKET_NAME"
ROBOFLOW_API_KEY = "YOUR_ROBOFLOW_API_KEY"
ROBOFLOW_PROJECT_NAME = "YOUR_ROBOFLOW_PROJECT_NAME"
GOOGLE_APPLICATION_CREDENTIALS = "path/to/your-service-account-file.json"
# ***********************************************

def get_gcs_signed_url(bucket_name: str, blob_name: str) -> str:
    """एक GCS object के लिए signed URL generate करें।"""
    storage_client = storage.Client.from_service_account_json(GOOGLE_APPLICATION_CREDENTIALS)
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.get_blob(blob_name)
    
    url = blob.generate_signed_url(
        version="v4",
        expiration=3600,  # 1 घंटे सेकंड में
        method="GET"
    )
    return url

def get_gcs_objects(bucket_name: str) -> list:
    """दिए गए GCS bucket में object keys की सूची प्राप्त करें।"""
    storage_client = storage.Client.from_service_account_json(GOOGLE_APPLICATION_CREDENTIALS)
    bucket = storage_client.get_bucket(bucket_name)
    blobs = bucket.list_blobs()

    object_names = []
    for blob in blobs:
        object_names.append(blob.name)
    return object_names

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__":
    # उपलब्ध blobs की सूची fetch करें
    available_blobs = get_gcs_objects(GCS_BUCKET_NAME)
    
    # वैकल्पिक: यहाँ blobs को filter करें
    # उदाहरण: available_blobs = [blob for blob in available_blobs if "some_condition"]
    
    # blobs को Roboflow पर upload करें
    for blob in available_blobs:
        blob_url = get_gcs_signed_url(GCS_BUCKET_NAME, blob)
        upload_to_roboflow(ROBOFLOW_API_KEY, ROBOFLOW_PROJECT_NAME, blob_url)

```

### Option 2: GCP से Data को Local रूप से Download करें

GCP से data download करने के लिए, पहले GCP CLI install करें। फिर, अपने GCP user account के साथ authenticate करें।

किसी image या images के folder को download करने के लिए, निम्न command का उपयोग करें:

```bash
gsutil cp -r gs://mybucket/folder .
```

इसे बदलें `mybucket` अपने GCP storage bucket के नाम से और `folder` उस file या folder के destination से जिसे आप copy करना चाहते हैं। यह command target file या folder को आपकी 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/google-cloud-storage.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.
