snowflake.snowpark.files.SnowflakeFile.open

classmethod SnowflakeFile.open(file_location: str, mode: str = 'r', is_owner_file: bool = False, *, require_scoped_url: bool = True) → SnowflakeFile[source]

Opens a file for reading and returns a SnowflakeFile stream. Use a with statement to close the stream after reading.

For example, inside a handler with a caller-provided scoped URL:

from snowflake.snowpark.files import SnowflakeFile

def read_text(url):
    with SnowflakeFile.open(url, "r") as source:
        return source.read()

This example reads the whole file into memory. Use binary mode ("rb") when the consumer expects bytes rather than text.

In UDFs and Stored Procedures, the object works like a read-only Python IOBase object and as a wrapper for an IO stream of remote files.

All files are accessed in the context of the UDF owner (with the exception of caller’s rights stored procedures which use the caller’s context). UDF callers should use scoped URLs to allow the UDF to access their files. By accepting only scoped URLs the UDF owner can ensure the UDF caller had access to the provided file. Removing the requirement that the URL is a scoped URL (require_scoped_url=False) allows the caller to provide URLs that may be only accessible by the UDF owner.

is_owner_file is marked for deprecation. For Snowflake release 7.8 and onwards please use require_scoped_url instead.

Parameters:
  • file_location – scoped URL, file URL, or string path for files located in a stage

  • mode – A string used to mark the type of an IO stream. Supported modes are “r” for text read and “rb” for binary read.

  • is_owner_file – (Deprecated) A boolean value, if True, the API is intended to access owner’s files and all URI/URL are allowed. If False, the API is intended to access files passed into the function by the caller and only scoped URL is allowed.

  • require_scoped_url – A boolean value, if True, file_location must be a scoped URL. A scoped URL ensures that the caller cannot access the UDF owners files that the caller does not have access to.