create_file function
Applies to: Databricks SQL
Databricks Runtime 18 LTS and above
This feature is in Beta. Workspace admins can control access to this feature from the Previews page. See Manage Databricks previews.
Uploads string or binary content to a destination path and returns a FILE reference to the uploaded file.
Syntax
create_file(content => content
[, destination_path => path ]
[, if_file_exists => mode ]
[, content_type => content_type ])
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
content: ASTRINGorBINARYexpression with the content to store as a file. ASTRINGis stored as UTF-8 encoded bytes.destination_path: An optionalSTRINGpath to upload the content to.if_file_exists: An optionalSTRINGthat sets the behavior when a file already exists at the destination path. Applies only when using thedestination_pathargument. Accepted values (case insensitive) are:'error': Raises an error. This is the default value.'overwrite': Overwrites the existing file.'skip': Skips the upload and returns aFILEreference to the existing file.
content_type: An optionalSTRINGthat specifies the content type of the file. If not provided, the content type is inferred from the input bytes.
Returns
A FILE value that references the uploaded file.
Notes
- If you use
destination_path, the file is uploaded atomically to that path. You must have write access to the target location, which is typically a Unity Catalog volume. - If a file already exists at the destination path, Databricks raises an error by default unless you set
if_file_existstooverwriteorskip.
Common error conditions
CREATE_FILE_ERROR.FILE_ALREADY_EXISTSCREATE_FILE_AUTHORIZATION_ERROR.WRITE_UNAUTHORIZEDCREATE_FILE_ILLEGAL_USAGE.CONTENT_REQUIREDCREATE_FILE_ILLEGAL_USAGE.IF_FILE_EXISTS_WITHOUT_DESTINATIONCREATE_FILE_ILLEGAL_USAGE.IF_FILE_EXISTS_MODE
For more information, see Error conditions in Databricks.
Examples
To store a string as a new file at a given path:
SELECT create_file(
content => 'hello, world',
destination_path => '/Volumes/my_catalog/my_schema/my_volume/greetings.txt'
);
To store raw bytes and set the content type explicitly:
SELECT create_file(
content => my_binary_col,
destination_path => '/Volumes/my_catalog/my_schema/my_volume/image.png',
content_type => 'image/png'
)
FROM raw_uploads;
To skip the upload if a file already exists at the destination:
SELECT create_file(
content => my_binary_col,
destination_path => '/Volumes/my_catalog/my_schema/my_volume/report.pdf',
if_file_exists => 'skip'
);
If destination_path isn't set, if_file_exists raises an error:
SELECT create_file(content => 'data', if_file_exists => 'overwrite');
Error: CREATE_FILE_ILLEGAL_USAGE.IF_FILE_EXISTS_WITHOUT_DESTINATION