# Copy

Copy hooks allow you to transfer files between storage locations. This is particularly useful for moving files between different storage systems, making backups, or archiving data.

## Configuration

```yaml
- type: copy
  from: "connection1/path/to/source"   # Required: Source Location
  to: "connection2/path/to/dest"       # Required: Destination Location
  single_file: true       # Optional: true/false. Force treat source as single file (no need to list the source location)
  on_failure: abort       # Optional: abort/warn/quiet/skip
  id: my_id      # Optional. Will be generated. Use `log` hook with {runtime_state} to view state.
```

## Properties

| Property     | Required | Description                                                                                                                                                                                                                  |
| ------------ | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| from         | Yes      | The source [location](/sling-cli/environment.md#location-string) string. Contains connection name and path.                                                                                                                  |
| to           | Yes      | The destination [location](/sling-cli/environment.md#location-string) string. Contains connection name and path.                                                                                                             |
| single\_file | No       | Boolean flag to specify whether to treat the source as a single file. If `true`, copies as a single file. If `false`, uses recursive copy for directories. If not specified, automatically detects based on the source path. |
| on\_failure  | No       | What to do if the copy fails (abort/warn/quiet/skip)                                                                                                                                                                         |

## Output

When the copy hook executes successfully, it returns the following output that can be accessed in subsequent hooks:

```yaml
status: success  # Status of the hook execution
from_uri: "s3://bucket/path/to/file"  # The normalized URI of the source file
from_path: "/path/to/source/file"  # The source path
to_uri: "gcs://bucket/path/to/file"  # The normalized URI of the destination file
to_path: "/path/to/dest/file"  # The destination path
bytes_written: 1024  # Number of bytes written
```

You can access these values in subsequent hooks using the following syntax (`jmespath`):

* `{state.hook_id.status}` - Status of the hook execution
* `{state.hook_id.from_uri}` - The normalized URI of the source file
* `{state.hook_id.from_path}` - The source path
* `{state.hook_id.to_uri}` - The normalized URI of the destination file
* `{state.hook_id.to_path}` - The destination path
* `{state.hook_id.bytes_written}` - Number of bytes written

## Examples

### Archive Files Between Cloud Storage

Archive files between different cloud storage providers:

```yaml
hooks:
  post:
    - type: copy
      if: run.status == "success"
      from: "aws_s3/{run.object.file_path}"
      to: "gcs/archives/{target.name}/{timestamp.YYYY}/{timestamp.MM}/{run.object.file_path}"
      on_failure: warn
```

### Upload A Local DuckDB Database into S3

Copy a local database file into Amazon S3 after writing to it:

```yaml
hooks:
  end:
    - type: copy
      from: "{target.instance}"
      to: "aws_s3/duckdb/{env.FOLDER}/backup.db"
      on_failure: warn
```

### Copy Multiple Files Pattern

Copy multiple files matching a pattern:

```yaml
hooks:
  post:
    - type: copy
      from: "local//tmp/exports/*.parquet"
      to: "gcs/data-lake/raw/{run.stream.name}/"
      on_failure: abort
```

### Force Single File Copy

Force treating the source as a single file, even if it could be interpreted as a pattern:

```yaml
hooks:
  post:
    - type: copy
      from: "s3/data/file.json"
      to: "local//backup/file.json"
      single_file: true
      on_failure: abort
```

### Force Directory Copy

Force recursive directory copying:

```yaml
hooks:
  post:
    - type: copy
      from: "local//data/exports"
      to: "s3/backup/exports"
      single_file: false
      on_failure: warn
```


---

# 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.slingdata.io/concepts/hooks/copy.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.
