[Yandex Cloud documentation](../../index.md) > [Yandex Data Transfer](../index.md) > [Tutorials](index.md) > Uploading data to data marts > Object Storage to YDB

# Loading data from Object Storage to a YDB data mart

# Loading data from Yandex Object Storage to Yandex Managed Service for YDB using Yandex Data Transfer


You can migrate data from Object Storage to a Managed Service for YDB table using Data Transfer. Proceed as follows:

1. [Prepare your test data](#prepare-data).
1. [Prepare and activate the transfer](#prepare-transfer).
1. [Test the transfer](#verify-transfer).

If you no longer need the resources you created, [delete them](#clear-out).


## Required paid resources {#paid-resources}

* Object Storage bucket: use of storage, data operations (see [Object Storage pricing](../../storage/pricing.md)).
* Managed Service for YDB database (see [Managed Service for YDB pricing](../../ydb/pricing/index.md)). Its cost depends on the deployment mode:

	* In serverless mode, you pay for data operations as well as the amount of stored data and backups.
  	* In dedicated instance mode, you pay for the use of computing resources allocated to the database, storage size, and backups.


## Getting started {#before-you-begin}


Set up your infrastructure:

{% list tabs group=instructions %}

- Manually {#manual}

    1. [Create a Managed Service for YDB database](../../ydb/operations/manage-databases.md) of your preferred configuration.

    1. If you selected Dedicated database mode, [create](../../vpc/operations/security-group-create.md) and [configure](../../ydb/operations/connection.md#configuring-security-groups) a security group in the network hosting your database.

    1. [Create an Object Storage bucket](../../storage/operations/buckets/create.md).

    
    1. [Create a service account](../../iam/operations/sa/create.md#create-sa) named `s3-ydb-account` with the `storage.editor` and `ydb.editor` roles. The transfer will use it to access the bucket and database.

    1. [Create a static access key](../../iam/operations/authentication/manage-access-keys.md#create-access-key) for the `s3-ydb-account` service account.


- Using Terraform {#tf}

    1. If you do not have Terraform yet, [install it](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
    1. [Get the authentication credentials](../../tutorials/infrastructure-management/terraform-quickstart.md#get-credentials). You can add them to environment variables or specify them later in the provider configuration file.
    1. [Configure and initialize a provider](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). There is no need to create a provider configuration file manually, you can [download it](https://github.com/yandex-cloud-examples/yc-terraform-provider-settings/blob/main/provider.tf).
    1. Place the configuration file in a separate working directory and [specify the parameter values](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). If you did not add the authentication credentials to environment variables, specify them in the configuration file.

    1. Download the [object-storage-to-ydb.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-object-storage-to-ydb/blob/main/object-storage-to-ydb.tf) configuration file to the same working directory.

        This file describes:

        * Service account to use for accessing the Managed Service for YDB bucket and database.
        * Yandex Lockbox secret for the service account static key required to configure the source endpoint.
        * Object Storage source bucket.
        * Managed Service for YDB target cluster.
        * Target endpoint.
        * Transfer.

    1. In the `object-storage-to-ydb.tf` file, specify the values of the following variables:

        * `folder_id`: Cloud folder ID, same as in the provider settings.
        * `bucket_name`: Bucket name consistent with the [naming conventions](../../storage/concepts/bucket.md#naming).

    1. Make sure the Terraform configuration files are correct using this command:

        ```bash
        terraform validate
        ```

        Terraform will display any configuration errors detected in your files.

    1. Create the required infrastructure:

        1. Run this command to view the planned changes:
        
           ```bash
           terraform plan
           ```
        
           If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
        
        1. If everything looks correct, apply the changes:
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

        All the required resources will be created in the specified folder. You can check resource availability and their settings in the [management console](https://console.yandex.cloud).

{% endlist %}

## Prepare your test data {#prepare-data}

1. Create two CSV files with test data:

    * `demo_data1.csv`:

        ```csv
        1,Anna
        2,Robert
        3,Umar
        4,Algul
        5,Viktor
        ```

    * `demo_data2.csv`:

        ```csv
        6,Maria
        7,Alex
        ```

1. [Upload](../../storage/operations/objects/upload.md#simple) the `demo_data1.csv` file to the Object Storage bucket.

## Prepare and activate the transfer {#prepare-transfer}

1. [Create a source endpoint](../operations/endpoint/source/object-storage.md#endpoint-settings) of the `Object Storage` type with the following settings:

    * **Database type**: `Object Storage`.
    * **Bucket**: Object Storage bucket name.

        
    * **Access Key ID**: Service account’s static access key ID. If you created your infrastructure using Terraform, [copy the key’s value from the Yandex Lockbox secret](../../lockbox/operations/secret-get-info.md#secret-contents).
    * **Secret Access Key**: Service account’s secret access key. If you created your infrastructure using Terraform, [copy the key’s value from the Yandex Lockbox secret](../../lockbox/operations/secret-get-info.md#secret-contents).


    * **Endpoint**: `https://storage.yandexcloud.net`.
    * **Region**: `ru-central1`.
    * **Data format**: `CSV`.
    * **Delimiter**: Comma (`,`).
    * **Table**: `table1`.
    * **Result table schema**: Select `Manual` and specify the following field names and data types:

        * `Id`: `Int64`
        * `Name`: `UTF8`

    Leave the other settings at their defaults.

1. Create a target endpoint and a transfer:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Create a target endpoint](../operations/endpoint/target/yandex-database.md#endpoint-settings) of the `YDB` type and specify the cluster connection settings in it:

            * **Database**: Select the YDB database from the list.

            
            * **Service account ID**: Select the `s3-ydb-account` service account.


        1. [Create a transfer](../operations/transfer.md#create) of the **_Snapshot and replication_** type that will use the endpoints you created.

        1. [Activate the transfer](../operations/transfer.md#activate) and wait for its status to change to **Replicating**.

    - Using Terraform {#tf}

        1. In the `object-storage-to-ydb.tf` file, specify the following settings:

            * `source_endpoint_id`: Source endpoint ID.
            * `transfer_enabled`: Set to `1` to create a transfer.

        1. Make sure the Terraform configuration files are correct using this command:

            ```bash
            terraform validate
            ```

            Terraform will display any configuration errors detected in your files.

        1. Create the required infrastructure:

            1. Run this command to view the planned changes:
            
               ```bash
               terraform plan
               ```
            
               If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
            
            1. If everything looks correct, apply the changes:
               1. Run this command:
            
                  ```bash
                  terraform apply
                  ```
            
               1. Confirm updating the resources.
               1. Wait for the operation to complete.

        1. The transfer will be activated automatically. Wait for its status to change to **Replicating**.

    {% endlist %}

## Test the transfer {#verify-transfer}

Make sure the transfer works correctly by testing copying and replication.

### Test copying {#verify-copy}

{% list tabs group=instructions %}

- Management console {#console}

    1. In the [management console](https://console.yandex.cloud), select the folder containing your database.
    1. Navigate to **Managed Service for&nbsp;YDB**.
    1. Select your database from the list.
    1. Navigate to the **Navigation** tab.
    1. Check that the Managed Service for YDB database contains the `table1` table with test data.

- CLI {#cli}

    1. [Connect to the Managed Service for YDB database](../../ydb/operations/connection.md).

    1. Run this query:

        ```sql
        SELECT * FROM table1;
        ```

        {% cut "Response example" %}

        ```sql
        Id |   Name   |   __file_name    | __row_index 
        ---+----------+------------------+-------------
         1 | "Anna"   | "demo_data1.csv" | 1 
         2 | "Robert" | "demo_data1.csv" | 2 
         3 | "Umar"   | "demo_data1.csv" | 3 
         4 | "Algul"  | "demo_data1.csv" | 4 
         5 | "Viktor" | "demo_data1.csv" | 5 
        ```

        {% endcut %}

{% endlist %}

### Test replication {#verify-replication}

1. [Upload](../../storage/operations/objects/upload.md#simple) the `demo_data2.csv` file to the Object Storage bucket.

1. Make sure the data from `demo_data2.csv` has been added to the target database:

    {% list tabs group=instructions %}

    - Management console {#console}

        1. In the [management console](https://console.yandex.cloud), select the folder containing your database.
        1. Navigate to **Managed Service for&nbsp;YDB**.
        1. Select your database from the list.
        1. Navigate to the **Navigation** tab.
        1. Check that `table1` now contains the new data.

    - CLI {#cli}

        1. [Connect to the Managed Service for YDB database](../../ydb/operations/connection.md).

        1. Run this query:

            ```sql
                SELECT * FROM table1;
            ```

            {% cut "Response example" %}

            ```sql
            Id |   Name   |   __file_name    | __row_index 
            ---+----------+------------------+-------------
             1 | "Anna"   | "demo_data1.csv" | 1 
             2 | "Robert" | "demo_data1.csv" | 2 
             3 | "Umar"   | "demo_data1.csv" | 3 
             4 | "Algul"  | "demo_data1.csv" | 4 
             5 | "Viktor" | "demo_data1.csv" | 5 
             6 | "Maria"  | "demo_data2.csv" | 1 
             7 | "Alex"   | "demo_data2.csv" | 2 
            ```

            {% endcut %}

    {% endlist %}

## Delete the resources you created {#clear-out}

{% note info %}

Before deleting any resources, [deactivate the transfer](../operations/transfer.md#deactivate).

{% endnote %}

To minimize resource consumption, delete the resources you no longer need:

1. [Delete the transfer](../operations/transfer.md#delete).
1. [Delete the source endpoint](../operations/endpoint/index.md#delete).
1. [Delete the objects](../../storage/operations/objects/delete.md) from the bucket.
1. Delete the rest of the resources depending on how you created them:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Delete the target endpoint](../operations/endpoint/index.md#delete).
        1. [Delete the Managed Service for YDB database](../../ydb/operations/manage-databases.md#delete-db).
        1. [Delete the Object Storage bucket](../../storage/operations/buckets/delete.md).

    - Using Terraform {#tf}

        1. In the terminal window, go to the directory containing the infrastructure plan.
        
            {% note warning %}
        
            Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.
        
            {% endnote %}
        
        1. Delete resources:
        
            1. Run this command:
        
                ```bash
                terraform destroy
                ```
        
            1. Confirm deleting the resources and wait for the operation to complete.
        
            All the resources described in the Terraform manifests will be deleted.

    {% endlist %}