Bird
Raised Fist0
Snowflakecloud~5 mins

Loading from S3, Azure Blob, GCS in Snowflake - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Loading data into Snowflake from cloud storage services like Amazon S3, Azure Blob Storage, or Google Cloud Storage lets you bring your files into your database easily. This helps you analyze and work with your data quickly without manual uploads.
When you have data files stored in Amazon S3 that you want to analyze in Snowflake.
When your data is saved in Azure Blob Storage and you need to load it into Snowflake tables.
When you want to import CSV or JSON files from Google Cloud Storage into Snowflake.
When you want to automate data loading from cloud storage into Snowflake for regular updates.
When you want to keep your data in cloud storage but query it efficiently inside Snowflake.
Config File - load_data.sql
load_data.sql
CREATE OR REPLACE STAGE my_s3_stage
  URL='s3://my-bucket/data/'
  STORAGE_INTEGRATION = my_s3_integration;

CREATE OR REPLACE STAGE my_azure_stage
  URL='azure://myaccount.blob.core.windows.net/mycontainer/data/'
  STORAGE_INTEGRATION = my_azure_integration;

CREATE OR REPLACE STAGE my_gcs_stage
  URL='gcs://my-gcs-bucket/data/'
  STORAGE_INTEGRATION = my_gcs_integration;

COPY INTO my_table
  FROM @my_s3_stage
  FILE_FORMAT = (TYPE = 'CSV' FIELD_DELIMITER = ',' SKIP_HEADER = 1);

COPY INTO my_table
  FROM @my_azure_stage
  FILE_FORMAT = (TYPE = 'CSV' FIELD_DELIMITER = ',' SKIP_HEADER = 1);

COPY INTO my_table
  FROM @my_gcs_stage
  FILE_FORMAT = (TYPE = 'CSV' FIELD_DELIMITER = ',' SKIP_HEADER = 1);

This SQL script creates three external stages in Snowflake, each pointing to a different cloud storage location: Amazon S3, Azure Blob Storage, and Google Cloud Storage. The STORAGE_INTEGRATION links Snowflake to the cloud storage securely.

Then, it runs COPY INTO commands to load CSV files from each stage into the Snowflake table my_table. The file format specifies CSV with commas and skips the header row.

Commands
This command creates a Snowflake stage named 'my_s3_stage' that points to the Amazon S3 bucket folder where your data files are stored. The storage integration handles secure access.
Terminal
snowsql -q "CREATE OR REPLACE STAGE my_s3_stage URL='s3://my-bucket/data/' STORAGE_INTEGRATION = my_s3_integration;"
Expected OutputExpected
Done. Snowflake stage 'my_s3_stage' created.
This command loads data from the S3 stage into the Snowflake table 'my_table' using the specified CSV file format. It skips the header row to avoid loading column names as data.
Terminal
snowsql -q "COPY INTO my_table FROM @my_s3_stage FILE_FORMAT = (TYPE = 'CSV' FIELD_DELIMITER = ',' SKIP_HEADER = 1);"
Expected OutputExpected
COPY INTO my_table executed successfully. Loaded 1000 rows.
This command creates a Snowflake stage named 'my_azure_stage' that points to your Azure Blob Storage container folder with data files.
Terminal
snowsql -q "CREATE OR REPLACE STAGE my_azure_stage URL='azure://myaccount.blob.core.windows.net/mycontainer/data/' STORAGE_INTEGRATION = my_azure_integration;"
Expected OutputExpected
Done. Snowflake stage 'my_azure_stage' created.
This command loads data from the Azure Blob Storage stage into the Snowflake table 'my_table' using the CSV format.
Terminal
snowsql -q "COPY INTO my_table FROM @my_azure_stage FILE_FORMAT = (TYPE = 'CSV' FIELD_DELIMITER = ',' SKIP_HEADER = 1);"
Expected OutputExpected
COPY INTO my_table executed successfully. Loaded 800 rows.
This command creates a Snowflake stage named 'my_gcs_stage' that points to your Google Cloud Storage bucket folder.
Terminal
snowsql -q "CREATE OR REPLACE STAGE my_gcs_stage URL='gcs://my-gcs-bucket/data/' STORAGE_INTEGRATION = my_gcs_integration;"
Expected OutputExpected
Done. Snowflake stage 'my_gcs_stage' created.
This command loads data from the Google Cloud Storage stage into the Snowflake table 'my_table' using the CSV file format.
Terminal
snowsql -q "COPY INTO my_table FROM @my_gcs_stage FILE_FORMAT = (TYPE = 'CSV' FIELD_DELIMITER = ',' SKIP_HEADER = 1);"
Expected OutputExpected
COPY INTO my_table executed successfully. Loaded 1200 rows.
Key Concept

If you remember nothing else from this pattern, remember: Snowflake stages link your cloud storage to your database, and COPY INTO loads the data from those stages into your tables.

Common Mistakes
Not creating or configuring the STORAGE_INTEGRATION before creating the stage.
Without STORAGE_INTEGRATION, Snowflake cannot securely access the cloud storage, so the stage creation or data loading will fail.
Always create and configure the STORAGE_INTEGRATION object with proper permissions before creating stages.
Using incorrect URL format for the cloud storage location in the stage definition.
Snowflake requires exact URL formats for each cloud provider; wrong URLs cause stage creation errors or inability to find files.
Use the correct URL prefix and path format: s3://bucket/path/, azure://account.blob.core.windows.net/container/path/, gcs://bucket/path/.
Not specifying the correct FILE_FORMAT or skipping the header row when loading CSV files.
If the file format is wrong, data may load incorrectly or cause errors; including headers as data can corrupt your table.
Define a FILE_FORMAT with the right type and options, and use SKIP_HEADER=1 if your files have headers.
Summary
Create Snowflake stages to connect to your cloud storage locations using STORAGE_INTEGRATION for secure access.
Use COPY INTO commands to load data from these stages into your Snowflake tables with the correct file format.
Verify each step by checking the success messages and row counts after loading data.

Practice

(1/5)
1. What is the main purpose of using COPY INTO in Snowflake when loading data from S3, Azure Blob, or GCS?
easy
A. To load data files from cloud storage into Snowflake tables
B. To export data from Snowflake to cloud storage
C. To create a new cloud storage bucket
D. To delete files from cloud storage

Solution

  1. Step 1: Understand the role of COPY INTO

    The COPY INTO command is used in Snowflake to load data from external cloud storage into Snowflake tables.
  2. Step 2: Differentiate from other operations

    Exporting data, creating buckets, or deleting files are not done by COPY INTO. It specifically loads data into tables.
  3. Final Answer:

    To load data files from cloud storage into Snowflake tables -> Option A
  4. Quick Check:

    Loading data = COPY INTO [OK]
Hint: COPY INTO loads data from cloud storage to tables [OK]
Common Mistakes:
  • Confusing COPY INTO with export commands
  • Thinking COPY INTO manages cloud storage buckets
  • Assuming COPY INTO deletes files
2. Which of the following is the correct syntax to load data from an S3 bucket into a Snowflake table named my_table?
easy
A. COPY INTO my_table FROM @my_s3_stage FILE_FORMAT = (TYPE = 'CSV');
B. LOAD DATA INTO my_table FROM 's3://mybucket/data.csv';
C. INSERT INTO my_table SELECT * FROM s3://mybucket/data.csv;
D. IMPORT INTO my_table FROM @my_s3_stage FORMAT = CSV;

Solution

  1. Step 1: Identify correct Snowflake COPY INTO syntax

    Snowflake uses COPY INTO table_name FROM @stage FILE_FORMAT = (TYPE = 'format') to load data.
  2. Step 2: Eliminate incorrect options

    LOAD DATA INTO is not valid Snowflake syntax. Direct INSERT INTO ... FROM s3:// paths are not supported. IMPORT INTO does not exist. The correct syntax is COPY INTO my_table FROM @my_s3_stage FILE_FORMAT = (TYPE = 'CSV');.
  3. Final Answer:

    COPY INTO my_table FROM @my_s3_stage FILE_FORMAT = (TYPE = 'CSV'); -> Option A
  4. Quick Check:

    COPY INTO + stage + file format = correct syntax [OK]
Hint: COPY INTO + @stage + FILE_FORMAT is the right pattern [OK]
Common Mistakes:
  • Using LOAD DATA instead of COPY INTO
  • Trying to SELECT directly from S3 path
  • Using IMPORT INTO which is invalid
3. Given the following Snowflake command:
COPY INTO sales FROM @azure_blob_stage FILE_FORMAT = (TYPE = 'JSON') ON_ERROR = 'CONTINUE';

What happens if one file in the Azure Blob storage has invalid JSON data?
medium
A. The entire load fails and no data is loaded
B. Snowflake automatically fixes the invalid JSON and loads all data
C. Only the invalid file is skipped, and loading continues for others
D. The command ignores the error and loads all files including invalid data

Solution

  1. Step 1: Understand ON_ERROR = 'CONTINUE'

    This option tells Snowflake to skip files or rows with errors and continue loading the rest.
  2. Step 2: Apply to invalid JSON file

    The invalid JSON file will be skipped, but other valid files will load successfully.
  3. Final Answer:

    Only the invalid file is skipped, and loading continues for others -> Option C
  4. Quick Check:

    ON_ERROR = CONTINUE skips errors, loads rest [OK]
Hint: ON_ERROR = CONTINUE skips bad files, loads others [OK]
Common Mistakes:
  • Assuming entire load fails on one bad file
  • Thinking Snowflake auto-fixes JSON errors
  • Believing invalid data is loaded anyway
4. You run this command to load data from Google Cloud Storage:
COPY INTO customers FROM @gcs_stage FILE_FORMAT = (TYPE = 'CSV');

But you get an error saying 'Storage integration not authorized'. What is the most likely cause?
medium
A. The GCS bucket is empty
B. The CSV file format is incorrect
C. The Snowflake table does not exist
D. The storage integration lacks permission to access the GCS bucket

Solution

  1. Step 1: Analyze the error message

    'Storage integration not authorized' means Snowflake cannot access the cloud storage due to permission issues.
  2. Step 2: Identify cause

    The storage integration must have proper permissions to read from the GCS bucket. Other options do not cause authorization errors.
  3. Final Answer:

    The storage integration lacks permission to access the GCS bucket -> Option D
  4. Quick Check:

    Authorization error = permission issue [OK]
Hint: Authorization errors usually mean permission problems [OK]
Common Mistakes:
  • Blaming file format for authorization errors
  • Assuming table existence causes storage errors
  • Ignoring permission setup for storage integration
5. You want to load multiple CSV files from an S3 bucket into Snowflake, but only files with the prefix 2024/. Which COPY INTO command correctly filters these files?
hard
A. COPY INTO sales FROM @s3_stage FILE_FORMAT = (TYPE = 'CSV') WHERE filename LIKE '2024/%';
B. COPY INTO sales FROM @s3_stage FILE_FORMAT = (TYPE = 'CSV') PATTERN = '^2024/.*';
C. COPY INTO sales FROM @s3_stage FILE_FORMAT = (TYPE = 'CSV') FILES = ('2024/');
D. COPY INTO sales FROM @s3_stage FILE_FORMAT = (TYPE = 'CSV') PATTERN = '2024/.*';

Solution

  1. Step 1: Understand file filtering in COPY INTO

    Snowflake uses the PATTERN parameter with a regular expression to filter files by name or prefix.
  2. Step 2: Check regex correctness

    PATTERN = '^2024/.*' matches files starting exactly with '2024/'. PATTERN = '2024/.*' without the ^ may match files where '2024/' appears elsewhere in the path. The other options use invalid parameters like WHERE or FILES.
  3. Final Answer:

    COPY INTO sales FROM @s3_stage FILE_FORMAT = (TYPE = 'CSV') PATTERN = '^2024/.*'; -> Option B
  4. Quick Check:

    PATTERN with ^ prefix filters files correctly [OK]
Hint: Use PATTERN with ^ prefix to filter file names [OK]
Common Mistakes:
  • Omitting ^ in regex causing wrong files to load
  • Using WHERE or FILES incorrectly for filtering
  • Confusing file prefix with file list