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.
For example, to create a stage called new_stage, enter the following command:
The following example shows what happens if you try to create a stage, packages, that already exists.
If you want to specify the type of encryption to use for all files stored on the stage, add the --encryption option to specify whether you want to full encryption (SNOWFLAKE_FULL) or only server-side encryption (SNOWFLAKE_SSE).
Copy files to and from a stage¶
The snow stage copy command copies a file from the local machine to a stage, from a stage to a local machine, or between named stages.
Note the following guidelines:
The stage path must start with
@, as shown in the following examples.When you copy a single file,
<destination_path>must identify a directory, not a file. If the specified directory does not exist, the command creates it.By default, when you copy a local directory to a stage, the local directory must contain only files. You can use the
--recursiveoption to upload sub-directories in the local directory. You can use glob patterns with the--recursiveoption.When you copy 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:
After copying the directory from the stage, the local filesystem directory contains the following:
Note
If you want to maintain the file structure from the source directory, you can include the
--recursiveoption.
Copy files to a stage¶
To copy files from the local machine to a stage, enter a command similar to the following:
You can use the snow stage list-files command to verify the command copied the files successfully:
Copy files from a stage¶
To copy files from a stage to a directory on the local machine, enter a command similar to the following:
You can list the directory contents to verify the command copied the files correctly:
Note that the local directory must exist.
You can copy from a user stage (@~):
Copy files between stages¶
You can copy files directly between two named stages without downloading them to your local machine first. This can be useful for organizing files across different stages or creating backups.
To copy files from one stage to another, use the following syntax:
The following example copies all files from the production_stage to the backup_stage:
Note
When you copy between stages, the destination cannot be a user stage (@~). You must specify named stages for both source and destination.
Use glob patterns to specify files¶
You can specify multiple files matching a regular expression by using a glob pattern for the source_path argument. You must enclose the glob pattern in single or double quotes.
The following example copies all .txt files in a directory to a stage.
List the contents of a stage¶
The snow stage list-files command lists the stage contents.
For example, to list the packages in a stage, enter the following command:
Execute files from a stage¶
Note
Snowflake CLI does not support executing Python files for Python versions 3.12 and above.
The snow stage execute command executes SQL or Python files from a stage.
For
.sqlfiles, the it performs an EXECUTE IMMEDIATE FROM command on.sqlfiles from a stage.For
.pyfiles, it executes a session-scoped Snowpark Python procedure.Snowflake CLI executes the procedure in Snowflake to guarantee a consistent execution environment. If your Python scripts require additional requirements, you should specify them in a
requirements.txtfile that resides in the same directory as the files on the stage. Thesnow stage executecommand only supports packages from the Snowflake Anaconda channel.By default, the command looks for the
requirements.txtfile in the following precedence:Stage path specified in the command’s
stage_pathparameter.Parent directories of the specified stage path hierarchy, until it reaches the stage.
If you don’t specify a
requirements.txtfile, the command assumes no additional packages are necessary.
For example, if you run
snow stage execute @my_stage/ml/app1/scripts, the command looks for the file as follows:my_stage/ml/app1/scripts/requirements.txtmy_stage/ml/app1/requirements.txtmy_stage/ml/requirements.txtmy_stage/ml/requirements.txt
The following examples illustrate ways to execute different sets of .sql files from a stage:
Specify only a stage name to execute all
.sqlfiles in the stage:Specify a user stage (
@~) to execute thescript.sqlfiles in the user stage:
Use glob patterns to select subsets of files¶
Specify a glob-like pattern to execute all
.sqlfiles in thedirdirectory:Specify a glob-like pattern to execute only
.sqlfiles in thedirdirectory that begin with “script”, followed by one character:Specify a direct file path with the
--silentoption:
Remove a file from a stage¶
The snow stage remove command removes a file from a stage.
For example, to remove a file from a stage, enter a command similar to the following: