Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
Databricks SQL
Databricks Runtime 18 LTS and above
Important
This feature is in Beta. Workspace admins can control access to this feature from the Previews page. See Manage Azure Databricks previews.
Copies a file to a destination path and returns a FILE reference to the copied file. Omit destination to copy the file into Unity Catalog-managed storage, which converts a FILE EXTERNAL reference to a FILE MANAGED reference.
Syntax
copy_file(file => file
[, destination => destination ]
[, if_file_exists_mode => mode ])
You can pass arguments positionally or by name. After you pass an argument by name, all following arguments must also be passed by name. For more information, see named parameter invocation.
Arguments
file: AFILEvalue to copy.destination: An optionalSTRINGwith the full destination file path, not just a directory. When omitted, the file is copied into Unity Catalog-managed storage, converting aFILE EXTERNALreference to aFILE MANAGEDreference.if_file_exists_mode: An optionalSTRINGthat sets the behavior when a file already exists at the destination path. Applies only when using thedestinationargument. Accepted values (case insensitive) are:'error': Raises an error. This is the default value.'overwrite': Overwrites the existing file.'skip': Skips copying and returns aFILEreference to the existing file.
Returns
A FILE value that references the copied file.
Notes
- Omitting
destinationconverts aFILE EXTERNALreference toFILE MANAGED. Azure Databricks also applies this conversion automatically when you insert aFILE EXTERNALvalue into aFILE MANAGEDcolumn. - If a file already exists at the destination path, Azure Databricks raises an error by default unless you set
if_file_exists_modetooverwriteorskip. - If the source file doesn't exist, Azure Databricks raises an error. To return
NULLinstead of raising an error in either case, usetry_copy_filefunction.
Common error conditions
COPY_FILE_ERROR.FILE_NOT_EXISTSCOPY_FILE_ERROR.FILE_ALREADY_EXISTSCOPY_FILE_AUTHORIZATION_ERROR.READ_UNAUTHORIZEDCOPY_FILE_AUTHORIZATION_ERROR.WRITE_UNAUTHORIZED
For more information, see Error conditions in Azure Databricks.
Examples
To copy a file from one volume to another:
SELECT copy_file(
to_file('/Volumes/source/data/input.csv'),
destination => '/Volumes/target/data/output.csv'
);
To copy files to a target volume using a per-row file name:
SELECT copy_file(
source_file,
destination => '/Volumes/my_catalog/my_schema/my_volume/processed/' || file_name,
if_file_exists_mode => 'skip'
)
FROM staging_files;
To overwrite the destination if a file already exists:
SELECT copy_file(
to_file('/Volumes/source/reports/report.pdf'),
destination => '/Volumes/archive/reports/report.pdf',
if_file_exists_mode => 'overwrite'
);
If the source file doesn't exist, copy_file raises an error:
SELECT copy_file(deleted_file, destination => '/Volumes/archive/reports/report.pdf');
Error: COPY_FILE_ERROR.FILE_NOT_EXISTS