# Google Drive

## Setup

The following credentials keys are accepted:

### Authentication Options

**Option 1: Service Account Key File**

* `key_file` (optional) -> Path to the service account JSON key file.

**Option 2: Service Account Key Body**

* `key_body` (optional) -> The service account JSON key content as a string. Or as Environment Variable `KEY_BODY`.

**Option 3: OAuth2 Credentials**

* `client_id` (optional) -> OAuth2 client ID.
* `client_secret` (optional) -> OAuth2 client secret.
* `access_token` (optional) -> OAuth2 access token.
* `refresh_token` (optional) -> OAuth2 refresh token.
* `token_expiry` (optional) -> Access token expiry time in RFC3339 format.

### Optional Parameters

* `folder_id` (recommended) -> Use a specific folder as the root directory instead of the actual Google Drive root.
* `file_id` (optional) -> Direct access to a specific file by its Google Drive ID. When set, only read operations are supported.
* `scopes` (optional) -> JSON array of Google Drive API scopes. Defaults to `["https://www.googleapis.com/auth/drive"]`.

### Using `sling conns`

Here are examples of setting a connection named `GDRIVE`. We must provide the `type=gdrive` property:

{% code overflow="wrap" %}

```bash
# Using service account key file
$ sling conns set GDRIVE type=gdrive key_file="/path/to/service-account-key.json"

# Using OAuth2 credentials
$ sling conns set GDRIVE type=gdrive client_id="your-client-id" client_secret="your-client-secret" access_token="your-access-token" refresh_token="your-refresh-token"

# Using a specific folder as root
$ sling conns set GDRIVE type=gdrive key_file="/path/to/key.json" folder_id="1abc2def3ghi4jkl5mno6pqr7stu8vwx"

# Direct file access by ID (read-only)
$ sling conns set GDRIVE type=gdrive key_file="/path/to/key.json" file_id="1xyz2abc3def4ghi5jkl6mno7pqr8stu"
```

{% endcode %}

### Environment Variable

See [here](https://docs.slingdata.io/sling-cli/environment#dot-env-file-.env.sling) to learn more about the `.env.sling` file.

In JSON/YAML format:

{% code overflow="wrap" %}

```bash
# Service account
export GDRIVE='{"type": "gdrive", "key_file": "/path/to/service-account-key.json"}'

# OAuth2
export GDRIVE='{"type": "gdrive", "client_id": "your-client-id", "client_secret": "your-client-secret", "access_token": "your-access-token", "refresh_token": "your-refresh-token"}'∂
```

{% endcode %}

### Sling Env File YAML

See [here](https://docs.slingdata.io/sling-cli/environment#sling-env-file-env.yaml) to learn more about the sling `env.yaml` file.

```yaml
connections:
  GDRIVE:
    type: gdrive
    key_file: /path/to/service-account-key.json
    folder_id: 1abc2def3ghi4jkl5mno6pqr7stu8vwx

  GDRIVE_OAUTH:
    type: gdrive
    client_id: your-client-id
    folder_id: 1abc2def3ghi4jkl5mno6pqr7stu8vwx
    client_secret: your-client-secret
    access_token: your-access-token
    refresh_token: your-refresh-token

  GDRIVE_FOLDER:
    type: gdrive
    folder_id: 1abc2def3ghi4jkl5mno6pqr7stu8vwx  # Use specific folder as root
    key_body: |
      { "type": "service_account", ... }

  GDRIVE_FILE:
    type: gdrive
    key_file: /path/to/service-account-key.json
    file_id: 1xyz2abc3def4ghi5jkl6mno7pqr8stu  # Direct file access (read-only)
```

## Usage Examples

```bash
# List files in Google Drive
sling run --src-conn GDRIVE --src-stream "folder/file.csv" --stdout

# Copy from local to Google Drive
sling run --src-stream file:///path/to/local.csv --tgt-conn GDRIVE --tgt-object "folder/remote.csv"

# Copy from Google Drive to database
sling run --src-conn GDRIVE --src-stream "data/file.csv" --tgt-conn MYDB --tgt-object "schema.table"

# Use wildcard patterns
sling run --src-conn GDRIVE --src-stream "reports/*.json" --stdout
```

If you are facing issues connecting, please reach out to us at <support@slingdata.io>, on [discord](https://discord.gg/q5xtaSNDvp) or open a Github Issue [here](https://github.com/slingdata-io/sling-cli/issues).
