Sub topic 2
Pointing COPY INTO at your data: volumes, external locations and permissions
This chapter assumes you know basic SQL, understand Delta tables, and can run statements in a Databricks SQL warehouse or on a cluster. It also assumes you have a rough idea of what cloud object storage is. You will learn where COPY INTO can read files from, which Unity Catalog objects control access to them, and which privileges you need on both the source and the target.
What COPY INTO needs before it can load anything
A COPY INTO statement has two sides. The target is a Delta table. The source is a path that holds files. Before the statement reads a single row, Databricks checks that you are allowed to touch both sides.
On Unity Catalog workspaces, access to the source path is governed by one of two securable objects:
- A volume is a Unity Catalog object that gives you a governed folder of files. You address it with a path of the form
/Volumes/<catalog>/<schema>/<volume>/<path>. - An external location is a Unity Catalog object that links a cloud storage path, such as
s3://bucket/prefixorabfss://[email protected]/prefix, to a storage credential. The credential holds the cloud identity that Databricks uses to reach the storage.
The first is usually the easier choice. The second is what you use when files already sit in a bucket or container that you do not want to move.
Loading from a volume
A volume is the simplest source. The files live in a managed or external volume, and the path starts with /Volumes.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');The path has four parts after /Volumes:
- The catalog, here
main. - The schema, here
sales. - The volume, here
landing. - A folder path inside the volume, here
orders/.
You can create a volume with SQL. The following statement creates a managed volume, which stores its files in the managed storage location of the schema.
CREATE VOLUME IF NOT EXISTS main.sales.landing;Managed volumes suit files that you upload or that a pipeline drops for you. Files you put there are stored in the schema's managed storage location, and Unity Catalog controls access to them.
An external volume is different. It points at a path inside an existing external location, so the files stay where they are. You create one by naming the location.
CREATE EXTERNAL VOLUME IF NOT EXISTS main.sales.partner_drop
LOCATION 's3://acme-partner-drop/orders';The LOCATION must fall inside an external location that you are allowed to use. If it does not, the statement fails with a permission error.
Loading from a cloud storage URI
You can also give COPY INTO a cloud URI directly. The URI scheme depends on the cloud:
| Cloud | Example URI |
|---|---|
| AWS | `s3://acme-landing/orders/` |
| Azure | `abfss://[email protected]/orders/` |
| Google Cloud | `gs://acme-landing/orders/` |
COPY INTO main.sales.orders
FROM 's3://acme-landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');For this to work in Unity Catalog, an external location must cover the URI. An external location covers its own path and everything below it. If you ask for s3://acme-landing/orders/ and the external location is defined on s3://acme-landing/, the request is covered. If the external location is defined on s3://acme-landing/returns/, it is not.
Databricks recommends volumes over direct URIs for new work. A volume path is stable, readable, and independent of the cloud, which makes the same SQL easy to reuse across environments.
Creating the storage credential and external location
Setting up an external location is an administrator task. The usual order is:
- Create a storage credential that wraps a cloud identity, such as an IAM role on AWS or a managed identity on Azure.
- Create an external location that ties a storage path to that credential.
- Grant privileges on the external location to the users or groups who will load data.
The SQL for steps 2 and 3 looks like this. The credential acme_landing_cred must already exist.
CREATE EXTERNAL LOCATION IF NOT EXISTS acme_landing
URL 's3://acme-landing/'
WITH (STORAGE CREDENTIAL acme_landing_cred);
GRANT READ FILES ON EXTERNAL LOCATION acme_landing TO `data-engineers`;Storage credentials are usually created in Catalog Explorer or through the API, because they involve cloud-side trust setup that SQL alone cannot finish. Check your cloud's documentation for the exact trust policy.
The permissions you need
Reading and writing involve different privileges. Both sides must pass, or the statement fails before loading any data.
| Side | Object | Privileges needed |
|---|---|---|
| Source, volume path | The volume | `READ VOLUME` |
| Source, cloud URI | The external location | `READ FILES` |
| Target | The Delta table | `MODIFY` and `SELECT` |
| Target | The parent schema and catalog | `USE SCHEMA` and `USE CATALOG` |
| Target, if COPY INTO creates the table | The schema | `CREATE TABLE` |
USE CATALOG and USE SCHEMA are easy to forget. They let you "see through" the catalog and schema to reach the objects inside. Having MODIFY on a table does not help if you cannot use its schema.
Here is a complete grant set for a group that loads orders from a volume into an existing table.
GRANT USE CATALOG ON CATALOG main TO `data-engineers`;
GRANT USE SCHEMA ON SCHEMA main.sales TO `data-engineers`;
GRANT READ VOLUME ON VOLUME main.sales.landing TO `data-engineers`;
GRANT SELECT, MODIFY ON TABLE main.sales.orders TO `data-engineers`;If the load runs as a scheduled job, the privileges must belong to the identity that the job runs as. That identity is the job owner or a service principal, not necessarily you. A statement that works in your notebook can fail in a job because the job identity lacks READ VOLUME.
Checking access before you load
Checking access first costs less than debugging a failed run. You can list the files in a volume path to confirm that you can see them.
LIST '/Volumes/main/sales/landing/orders/';The result has one row per file, with columns for the path, name, size, and modification time. If the list is empty, either the folder has no files or the path is wrong. If the call fails with a permission error, you are missing READ VOLUME on the volume.
You can also inspect your grants.
SHOW GRANTS ON VOLUME main.sales.landing;Note that SHOW GRANTS lists grants for the object. Grants can be inherited from the schema or catalog, so a user may have access without a direct grant on the volume.
Common access errors
Most failures at this stage come from a small set of causes.
| Symptom | Likely cause | Fix |
|---|---|---|
| Permission denied on a `/Volumes/...` path | Missing `READ VOLUME` or `USE SCHEMA` | Grant the missing privilege to the user or the job identity |
| Permission denied on an `s3://` or `abfss://` URI | No external location covers the path, or you lack `READ FILES` | Ask an admin to create the location or grant `READ FILES` |
| Path not found, or no files loaded | Typo in the path, wrong volume name, or an empty folder | Run `LIST` on the path and compare |
| Target table not found | Wrong catalog or schema, or the table does not exist | Check the three-part name and create the table first |
| Works in a notebook, fails in a job | The job runs as a different identity | Grant privileges to that identity |
A path that exists but holds no new files does not raise an error. COPY INTO simply loads zero rows. The next chapters explain why, when we cover how COPY INTO tracks which files it has already loaded.
Putting it together
The sequence below shows an end-to-end setup for a team that receives CSV files from a partner. An administrator runs the first two statements once. The team then runs the load.
CREATE EXTERNAL VOLUME IF NOT EXISTS main.sales.partner_drop
LOCATION 's3://acme-partner-drop/orders';
GRANT READ VOLUME ON VOLUME main.sales.partner_drop TO `data-engineers`;
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/partner_drop/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');This approach has a useful property. Engineers never see the cloud credential. They only need a Unity Catalog privilege, and an administrator can revoke it in one place.
Exercise
A scheduled job runs COPY INTO main.sales.orders FROM '/Volumes/main/sales/landing/orders/'. The statement works when you run it in a notebook, but the job fails with a permission error on the volume path. The job runs as the service principal etl-sp. Which privileges should you check for etl-sp, and why does the notebook run differ?
Show answer
Check that etl-sp has USE CATALOG on main, USE SCHEMA on main.sales, READ VOLUME on main.sales.landing, and SELECT plus MODIFY on main.sales.orders. The notebook run uses your own identity, which already holds these privileges. The job uses the service principal's identity, and Unity Catalog checks privileges against whichever identity runs the statement.
Exercise
You need to load files from s3://acme-landing/orders/. An external location exists on s3://acme-landing/returns/. Will COPY INTO be allowed to read the orders path, and what is one way to fix it?
Show answer
No. An external location covers only its own path and the paths below it, and orders/ is a sibling of returns/, not a child. An administrator can create an external location on s3://acme-landing/ or s3://acme-landing/orders/ and grant you READ FILES. Alternatively, they can create an external volume on that path and grant you READ VOLUME.