Managing Snowflake stages

The snow stage commands let you perform additional stage-specific tasks:

Create a named stage

The snow stage create command creates a named stage if it does not already exist.

snow stage create <stage_name>
Copy

For example, to create a stage called new_stage, enter the following command:

snow stage create new_stage
Copy
+-----------------------------------------------------+
| key    | value                                      |
|--------+--------------------------------------------|
| status | Stage area NEW_STAGE successfully created. |
+-----------------------------------------------------+

The following example shows what happens if you try to create a stage, packages, that already exists.

# stage that already exists
snow stage create packages
Copy
+--------------------------------------------------------+
| key    | value                                         |
|--------+-----------------------------------------------|
| status | PACKAGES already exists, statement succeeded. |
+--------------------------------------------------------+

Copy files to and from a stage

The snow stage copy command copies a file from the local machine to a stage or from a stage to a local machine.

snow stage copy <source_path> <destination_path>
Copy

One of <source_path> or <destination_path> must be a local path, while the other must a path to the Snowflake stage. Note the following:

  • The stage path must start with @, as shown in the following examples.

  • When copying a single file, the <destination_path> must identify a directory, not a file. If the specified directory does not exist, the command creates it.

  • When copying a local directory to a stage, the local directory must contain only files; directories containing sub-directories are not supported.

  • When copying a directory from a stage to a local filesystem, the command currently flattens its internal tree structure. To illustrate, assume your local directory contains the following:

    test_case.py
    tests/abc.py
    tests/test1/x1.txt
    tests/test1/x2.txt
    

    After copying the directory from the stage, the local filesystem directory contains the following:

    test_case.py
    abc.py
    x1.txt
    x2.txt
    

    Note

    If you want to maintain the file structure from the source directory, you can include the --recursive option.

To copy files from the local machine to a stage, enter a commands similar to the following:

snow stage copy local_example_app @example_app_stage/app
Copy
put file:///.../local_example_app/* @example_app_stage/app4 auto_compress=false parallel=4 overwrite=False
+--------------------------------------------------------------------------------------
| source           | target           | source_size | target_size | source_compression...
|------------------+------------------+-------------+-------------+--------------------
| environment.yml  | environment.yml  | 62          | 0           | NONE             ...
| snowflake.yml    | snowflake.yml    | 252         | 0           | NONE             ...
| streamlit_app.py | streamlit_app.py | 109         | 0           | NONE             ...
+--------------------------------------------------------------------------------------

You can use the snow stage list-files command to verify the command copied the files successfully:

snow stage list-files example_app_stage
Copy
ls @example_app_stage
​+------------------------------------------------------------------------------------
| name                                   | size | md5                              | ...
|----------------------------------------+------+----------------------------------+-
| example_app_stage/app/environment.yml  | 64   | 45409c8da098125440bfb7ffbcd900f5 | ...
| example_app_stage/app/snowflake.yml    | 256  | a510b1d59fa04f451b679d43c703b6d4 | ...
| example_app_stage/app/streamlit_app.py | 112  | e6c2a89c5a164e34a0faf60b086bbdfc | ...
+------------------------------------------------------------------------------------

The following example copies files from a stage to a directory on the local machine:

mkdir local_app_backup
snow stage copy @example_app_stage/app local_app_backup
Copy
get @example_app_stage/app file:///.../local_app_backup/ parallel=4
+------------------------------------------------+
| file             | size | status     | message |
|------------------+------+------------+---------|
| environment.yml  | 62   | DOWNLOADED |         |
| snowflake.yml    | 252  | DOWNLOADED |         |
| streamlit_app.py | 109  | DOWNLOADED |         |
+------------------------------------------------+

You can list the directory contents to verify the command copied the files correctly:

ls local_app_backup
Copy
environment.yml  snowflake.yml    streamlit_app.py

Note that the local directory must exist.

List the contents of a stage

The snow stage list-files command lists the stage contents.

snow stage list-files <stage_path>
Copy

For example, to list the packages in a stage, enter the following command:

snow stage list-files packages
Copy
ls @packages
+-------------------------------------------------------------------------------------
| name                 | size     | md5                              | last_modified
|----------------------+----------+----------------------------------+----------------
| packages/plp.Ada.zip | 824736   | 90639175a0ac7735e67525118b81047c | Tue, 16 Jan ...
| packages/samrand.zip | 13721024 | 648f0bae2f65fd4c9f178b17c23de7e5 | Tue, 16 Jan ...
+-------------------------------------------------------------------------------------

Execute SQL files from a stage

The snow stage execute command performs an EXECUTE IMMEDIATE command on .sql files from a stage.

snow stage execute <stage_path>
Copy

The following examples illustrate ways to execute different sets of .sql files from a stage:

  • Specify only a stage name to execute all .sql files in the stage:

    snow stage execute "@scripts"
    
    Copy
    SUCCESS - scripts/script1.sql
    SUCCESS - scripts/script2.sql
    SUCCESS - scripts/dir/script.sql
    +------------------------------------------+
    | File                   | Status  | Error |
    |------------------------+---------+-------|
    | scripts/script1.sql    | SUCCESS | None  |
    | scripts/script2.sql    | SUCCESS | None  |
    | scripts/dir/script.sql | SUCCESS | None  |
    +------------------------------------------+
    
  • Specify a glob-like pattern to execute all .sql files in the dir directory:

    snow stage execute "@scripts/dir/*"
    
    Copy
    SUCCESS - scripts/dir/script.sql
    +------------------------------------------+
    | File                   | Status  | Error |
    |------------------------+---------+-------|
    | scripts/dir/script.sql | SUCCESS | None  |
    +------------------------------------------+
    
  • Specify a glob-like pattern to execute only .sql files in the dir directory that begin with “script”, followed by one character:

    snow stage execute "@scripts/script?.sql"
    
    Copy
    SUCCESS - scripts/script1.sql
    SUCCESS - scripts/script2.sql
    +---------------------------------------+
    | File                | Status  | Error |
    |---------------------+---------+-------|
    | scripts/script1.sql | SUCCESS | None  |
    | scripts/script2.sql | SUCCESS | None  |
    +---------------------------------------+
    
  • Specify a direct file path with the --silent option:

    snow stage execute "@scripts/script1.sql" --silent
    
    Copy
    +---------------------------------------+
    | File                | Status  | Error |
    |---------------------+---------+-------|
    | scripts/script1.sql | SUCCESS | None  |
    +---------------------------------------+
    

Remove a file from a stage

The snow stage remove command removes a file from a stage.

snow stage remove <stage_name> <file_name>
Copy

For example, to remove a file from a stage, enter a command similar to the following:

snow stage remove example_app_stage app/pages/my_page.py
Copy
+-------------------------------------------------+
| key    | value                                  |
|--------+----------------------------------------|
| name   | example_app_stage/app/pages/my_page.py |
| result | removed                                |
+-------------------------------------------------+