# storage\_read

### storage\_read

Reads a file from DataJet storage. The file content is returned in a result object for further processing.

```liquid
{% storage_read filename: "data.csv" as result %}

{% if result.ok %}
  {% log result.file.content %}
{% else %}
  {% log result.error %}
{% endif %}
```

#### Syntax

```liquid
{% storage_read filename: "filename" as result_variable %}
```

#### Parameters

| Parameter  | Required | Description                                   |
| ---------- | -------- | --------------------------------------------- |
| `filename` | Yes      | Name of the file to read from DataJet storage |

#### Result Object

| Property       | Type        | Description                                         |
| -------------- | ----------- | --------------------------------------------------- |
| `ok`           | boolean     | `true` if read succeeded                            |
| `error`        | string/null | Error message if `ok` is `false`, `null` on success |
| `file.name`    | string      | The filename                                        |
| `file.content` | string      | The file content as text                            |
| `file.size`    | number      | File size in bytes                                  |

Example result:

```json
{
  "ok": true,
  "error": null,
  "file": {
    "name": "data.csv",
    "content": "id,name,price\n1,Product A,29.99\n2,Product B,49.99",
    "size": 52
  }
}
```

#### Examples

**Read and log file content:**

```liquid
{% storage_read filename: "report.txt" as result %}

{% if result.ok %}
  {% log "File content:" %}
  {% log result.file.content %}
{% else %}
  {% log "Error reading file: " | append: result.error %}
{% endif %}
```

**Read and parse CSV file:**

```liquid
{% storage_read filename: "products.csv" as result %}

{% if result.ok %}
  {% assign rows = result.file.content | parse_csv %}

  {% for row in rows %}
    {% log "Product: " | append: row.name | append: " - $" | append: row.price %}
  {% endfor %}
{% endif %}
```

**Read and parse JSON file:**

```liquid
{% storage_read filename: "config.json" as result %}

{% if result.ok %}
  {% assign config = result.file.content | parse_json %}

  {% log "API Endpoint: " | append: config.api_endpoint %}
  {% log "Max retries: " | append: config.max_retries %}
{% endif %}
```

**Read file with dynamic filename:**

```liquid
{% assign today = "now" | date: "%Y-%m-%d" %}
{% assign filename = "export_" | append: today | append: ".csv" %}

{% storage_read filename: filename as result %}

{% if result.ok %}
  {% log "Successfully read: " | append: result.file.name %}
  {% log "File size: " | append: result.file.size | append: " bytes" %}
{% endif %}
```

**Process file line by line:**

```liquid
{% storage_read filename: "orders.txt" as result %}

{% if result.ok %}
  {% assign lines = result.file.content | split: "\n" %}

  {% for line in lines %}
    {% if line != blank %}
      {% log "Processing: " | append: line %}
    {% endif %}
  {% endfor %}
{% endif %}
```

**Read and send to external API:**

```liquid
{% storage_read filename: "payload.json" as result %}

{% if result.ok %}
  {% assign body = result.file.content | parse_json %}

  {% json headers %}
    {
      "Content-Type": "application/json",
      "Authorization": "Bearer {{ API_KEY }}"
    }
  {% endjson %}

  {% http url: "https://api.example.com/import", method: "POST", headers: headers, body: body as response %}

  {% if response.ok %}
    {% log "Data sent successfully" %}
  {% endif %}
{% endif %}
```

**Check file size before processing:**

```liquid
{% storage_read filename: "large_file.csv" as result %}

{% if result.ok %}
  {% if result.file.size > 1000000 %}
    {% log "Warning: Large file (" | append: result.file.size | append: " bytes)" %}
  {% endif %}

  {% comment %} Process file... {% endcomment %}
{% endif %}
```

**Read configuration and use in script:**

```liquid
{% storage_read filename: "settings.json" as settings_result %}

{% if settings_result.ok %}
  {% assign settings = settings_result.file.content | parse_json %}

  {% if settings.feature_enabled %}
    {% log "Feature is enabled, processing..." %}
    {% comment %} Feature logic here {% endcomment %}
  {% else %}
    {% log "Feature is disabled" %}
  {% endif %}
{% else %}
  {% log "Could not load settings, using defaults" %}
  {% assign settings = nil %}
{% endif %}
```

**Read and upload to FTP:**

```liquid
{% storage_read filename: "export.csv" as result %}

{% if result.ok %}
  {% ftp_session host: FTP_HOST, user: FTP_USER, password: FTP_PASSWORD %}
    {% ftp_upload to: "/imports/export.csv", content: result.file.content as upload %}

    {% if upload.ok %}
      {% log "File uploaded to FTP" %}
    {% endif %}
  {% endftp_session %}
{% endif %}
```

**Error handling pattern:**

```liquid
{% storage_read filename: "required_data.json" as result %}

{% unless result.ok %}
  {% log "CRITICAL: Failed to read required file" %}
  {% log result.error %}

  {% json error_payload %}
    {
      "error": "{{ result.error }}",
      "filename": "required_data.json",
      "timestamp": "{{ 'now' | date: '%Y-%m-%dT%H:%M:%SZ' }}"
    }
  {% endjson %}

  {% run "error_handler", error_payload %}
  {% exit %}
{% endunless %}

{% comment %} Continue processing... {% endcomment %}
{% assign data = result.file.content | parse_json %}
```

#### Related Tags

| Tag             | Description                       |
| --------------- | --------------------------------- |
| `storage_write` | Writes content to DataJet storage |

#### Notes

* Consumes 1 credit per read operation
* Maximum file size: 10MB.
* File content is returned as a string - use `| parse_csv` or `| parse_json` for structured data
* Use `storage_write` to create or update files in storage
* If the file doesn't exist, `ok` will be `false` with an error message
