2020-12-18 09:46:50 +00:00
---
toc_priority: 45
toc_title: s3
---
2021-03-05 08:00:49 +00:00
# S3 Table Function {#s3-table-function}
2020-12-18 09:46:50 +00:00
2021-03-05 08:00:49 +00:00
Provides table-like interface to select/insert files in [Amazon S3 ](https://aws.amazon.com/s3/ ). This table function is similar to [hdfs ](../../sql-reference/table-functions/hdfs.md ), but provides S3-specific features.
2021-03-05 08:31:16 +00:00
**Syntax**
2020-12-18 09:46:50 +00:00
``` sql
s3(path, [aws_access_key_id, aws_secret_access_key,] format, structure, [compression])
```
2021-03-05 08:31:16 +00:00
**Arguments**
2020-12-18 09:46:50 +00:00
2021-03-21 14:23:16 +00:00
- `path` — Bucket url with path to file. Supports following wildcards in readonly mode: `*` , `?` , `{abc,def}` and `{N..M}` where `N` , `M` — numbers, `'abc'` , `'def'` — strings. For more information see [here ](../../engines/table-engines/integrations/s3.md#wildcards-in-path ).
2020-12-18 09:46:50 +00:00
- `format` — The [format ](../../interfaces/formats.md#formats ) of the file.
- `structure` — Structure of the table. Format `'column1_name column1_type, column2_name column2_type, ...'` .
2021-03-25 14:24:42 +00:00
- `compression` — Parameter is optional. Supported values: `none` , `gzip/gz` , `brotli/br` , `xz/LZMA` , `zstd/zst` . By default, it will autodetect compression by file extension.
2020-12-18 09:46:50 +00:00
**Returned value**
A table with the specified structure for reading or writing data in the specified file.
2021-03-05 08:00:49 +00:00
**Examples**
2020-12-18 09:46:50 +00:00
2021-03-10 05:48:31 +00:00
Selecting the first two rows from the table from S3 file `https://storage.yandexcloud.net/my-test-bucket-768/data.csv` :
2020-12-18 09:46:50 +00:00
``` sql
SELECT *
FROM s3('https://storage.yandexcloud.net/my-test-bucket-768/data.csv', 'CSV', 'column1 UInt32, column2 UInt32, column3 UInt32')
2021-03-05 09:22:15 +00:00
LIMIT 2;
2020-12-18 09:46:50 +00:00
```
``` text
┌─column1─┬─column2─┬─column3─┐
│ 1 │ 2 │ 3 │
│ 3 │ 2 │ 1 │
└─────────┴─────────┴─────────┘
```
The similar but from file with `gzip` compression:
``` sql
SELECT *
FROM s3('https://storage.yandexcloud.net/my-test-bucket-768/data.csv.gz', 'CSV', 'column1 UInt32, column2 UInt32, column3 UInt32', 'gzip')
2021-03-05 09:22:15 +00:00
LIMIT 2;
2020-12-18 09:46:50 +00:00
```
``` text
┌─column1─┬─column2─┬─column3─┐
│ 1 │ 2 │ 3 │
│ 3 │ 2 │ 1 │
└─────────┴─────────┴─────────┘
```
2021-03-05 08:00:49 +00:00
## Usage {#usage-examples}
2020-12-18 09:46:50 +00:00
2021-03-05 08:00:49 +00:00
Suppose that we have several files with following URIs on S3:
2020-12-18 09:46:50 +00:00
2021-03-05 09:10:35 +00:00
- 'https://storage.yandexcloud.net/my-test-bucket-768/some_prefix/some_file_1.csv'
- 'https://storage.yandexcloud.net/my-test-bucket-768/some_prefix/some_file_2.csv'
- 'https://storage.yandexcloud.net/my-test-bucket-768/some_prefix/some_file_3.csv'
- 'https://storage.yandexcloud.net/my-test-bucket-768/some_prefix/some_file_4.csv'
- 'https://storage.yandexcloud.net/my-test-bucket-768/another_prefix/some_file_1.csv'
- 'https://storage.yandexcloud.net/my-test-bucket-768/another_prefix/some_file_2.csv'
- 'https://storage.yandexcloud.net/my-test-bucket-768/another_prefix/some_file_3.csv'
- 'https://storage.yandexcloud.net/my-test-bucket-768/another_prefix/some_file_4.csv'
2020-12-18 09:46:50 +00:00
2021-03-10 06:13:46 +00:00
Count the amount of rows in files ending with numbers from 1 to 3:
2020-12-18 09:46:50 +00:00
``` sql
SELECT count(*)
FROM s3('https://storage.yandexcloud.net/my-test-bucket-768/{some,another}_prefix/some_file_{1..3}.csv', 'CSV', 'name String, value UInt32')
```
``` text
┌─count()─┐
│ 18 │
└─────────┘
```
2021-03-10 05:48:52 +00:00
Count the total amount of rows in all files in these two directories:
2020-12-18 09:46:50 +00:00
``` sql
SELECT count(*)
FROM s3('https://storage.yandexcloud.net/my-test-bucket-768/{some,another}_prefix/*', 'CSV', 'name String, value UInt32')
```
``` text
┌─count()─┐
│ 24 │
└─────────┘
```
!!! warning "Warning"
If your listing of files contains number ranges with leading zeros, use the construction with braces for each digit separately or use `?` .
2021-03-10 06:13:40 +00:00
Count the total amount of rows in files named `file-000.csv` , `file-001.csv` , … , `file-999.csv` :
2020-12-18 09:46:50 +00:00
``` sql
SELECT count(*)
2021-03-05 09:22:15 +00:00
FROM s3('https://storage.yandexcloud.net/my-test-bucket-768/big_prefix/file-{000..999}.csv', 'CSV', 'name String, value UInt32');
2020-12-18 09:46:50 +00:00
```
``` text
┌─count()─┐
│ 12 │
└─────────┘
```
2021-03-10 05:49:05 +00:00
Insert data into file `test-data.csv.gz` :
2020-12-18 09:46:50 +00:00
``` sql
2021-06-07 14:08:20 +00:00
INSERT INTO FUNCTION s3('https://storage.yandexcloud.net/my-test-bucket-768/test-data.csv.gz', 'CSV', 'name String, value UInt32', 'gzip')
2021-03-05 09:22:15 +00:00
VALUES ('test-data', 1), ('test-data-2', 2);
2020-12-18 09:46:50 +00:00
```
2021-03-10 06:13:31 +00:00
Insert data into file `test-data.csv.gz` from existing table:
2020-12-18 09:46:50 +00:00
``` sql
2021-06-07 14:08:20 +00:00
INSERT INTO FUNCTION s3('https://storage.yandexcloud.net/my-test-bucket-768/test-data.csv.gz', 'CSV', 'name String, value UInt32', 'gzip')
2021-03-05 09:22:15 +00:00
SELECT name, value FROM existing_table;
2020-12-18 09:46:50 +00:00
```
**See Also**
2021-03-05 08:00:49 +00:00
- [S3 engine ](../../engines/table-engines/integrations/s3.md )
2020-12-18 09:46:50 +00:00
2021-03-05 08:00:49 +00:00
[Original article ](https://clickhouse.tech/docs/en/sql-reference/table-functions/s3/ ) <!--hide-->