Documentation
¶
Overview ¶
Package drivefs provides a file system-like interface for Google Drive operations.
This package wraps the google.golang.org/api/drive/v3 package and offers familiar filesystem operations such as creating, reading, writing, copying, renaming, moving, and deleting files and directories. The package fully supports both My Drive and Shared Drives.
Index ¶
- Variables
- type DriveFS
- func (s *DriveFS) Copy(fileID, newParentID FileID, newName string) (info FileInfo, err error)
- func (s *DriveFS) Create(parentID FileID, name string) (info FileInfo, err error)
- func (s *DriveFS) FindByPath(rootID FileID, path Path) (info []FileInfo, err error)
- func (s *DriveFS) Info(fileID FileID) (info FileInfo, err error)
- func (s *DriveFS) Mkdir(parentID FileID, name string) (info FileInfo, err error)
- func (s *DriveFS) MkdirAll(rootID FileID, path Path) (info FileInfo, err error)
- func (s *DriveFS) Move(fileID, newParentID FileID) (err error)
- func (s *DriveFS) PermDel(fileID FileID, grantee Grantee) (permissions []Permission, err error)
- func (s *DriveFS) PermList(fileID FileID) (permissions []Permission, err error)
- func (s *DriveFS) PermSet(fileID FileID, permission Permission) (permissions []Permission, err error)
- func (s *DriveFS) Query(query string) (results []FileInfo, err error)
- func (s *DriveFS) ReadDir(fileID FileID) (children []FileInfo, err error)
- func (s *DriveFS) ReadFile(fileID FileID) (data []byte, err error)
- func (s *DriveFS) Remove(fileID FileID, moveToTrash bool) (err error)
- func (s *DriveFS) RemoveAll(fileID FileID, moveToTrash bool) (err error)
- func (s *DriveFS) Rename(fileID FileID, newName string) (info FileInfo, err error)
- func (s *DriveFS) ResolvePath(fileID FileID) (path Path, err error)
- func (s *DriveFS) Shortcut(parentID FileID, name string, targetID FileID) (info FileInfo, err error)
- func (s *DriveFS) Walk(rootID FileID, f func(Path, FileInfo) error) (err error)
- func (s *DriveFS) WriteFile(fileID FileID, data []byte) (err error)
- type FileID
- type FileInfo
- type Grantee
- type GranteeAnyone
- type GranteeDomain
- type GranteeGroup
- type GranteeUser
- type Path
- type Permission
- type PermissionID
- type Role
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidPath is returned when a path is malformed or uses relative path components. ErrInvalidPath = errors.New("invalid path") // ErrDriveError is the underlying error for all Google Drive API errors. ErrDriveError = errors.New("drive error") // ErrIOError is the underlying error for all I/O errors. ErrIOError = errors.New("io error") // ErrNotFound is returned when a requested file or directory does not exist. ErrNotFound = errors.New("not found") // ErrAlreadyExists is returned when attempting to create a file or directory that already exists. ErrAlreadyExists = errors.New("already exists") // ErrMultiParentsNotSupported is returned when an operation encounters a file with multiple parents. ErrMultiParentsNotSupported = errors.New("multi parents not supported") // ErrNotReadable is returned when attempting to read a file that cannot be downloaded (e.g., Google Apps files). ErrNotReadable = errors.New("not readable") // ErrNotRemovable is returned when attempting to remove a non-empty directory. ErrNotRemovable = errors.New("not removable") )
Common errors returned by DriveFS operations.
Functions ¶
This section is empty.
Types ¶
type DriveFS ¶
type DriveFS struct {
// contains filtered or unexported fields
}
DriveFS provides file system-like operations for Google Drive. It wraps a drive.Service and provides high-level methods for managing files and directories.
func New ¶
New creates a new DriveFS instance with the given drive.Service. The service should be properly authenticated before being passed to this function.
func (*DriveFS) Copy ¶
Copy creates a copy of the file with the given fileID. The copy is placed in the specified parent directory with the given name. Returns the FileInfo of the copied file.
func (*DriveFS) Create ¶
Create creates a new empty file with the given name in the specified parent directory. Returns the FileInfo of the created file.
func (*DriveFS) FindByPath ¶
FindByPath resolves the given absolute path from the specified root directory. Returns all files matching the path (multiple results if duplicates exist at any level). The path must be absolute (starting with '/').
func (*DriveFS) Info ¶
Info retrieves metadata for the file or directory with the given fileID. Returns ErrNotFound if the file does not exist.
func (*DriveFS) Mkdir ¶
Mkdir creates a single directory with the given name in the specified parent directory. Returns the FileInfo of the created directory.
func (*DriveFS) MkdirAll ¶
MkdirAll creates all directories along the given path if they do not already exist. The path must be absolute (starting with '/') and is resolved from the specified rootID. Returns the FileInfo of the final directory in the path. If two or more directories with the same name exist at any level, returns ErrAlreadyExists.
func (*DriveFS) Move ¶
Move moves the file or directory with the given fileID to a new parent directory. Returns ErrNotFound if the file does not exist.
func (*DriveFS) PermDel ¶
func (s *DriveFS) PermDel(fileID FileID, grantee Grantee) (permissions []Permission, err error)
PermDel deletes all permissions matching the given grantee for the file or directory with the given fileID. Returns all remaining permissions after the operation.
func (*DriveFS) PermList ¶
func (s *DriveFS) PermList(fileID FileID) (permissions []Permission, err error)
PermList lists all permissions for the file or directory with the given fileID. Returns a slice of Permission objects representing the access permissions.
func (*DriveFS) PermSet ¶
func (s *DriveFS) PermSet(fileID FileID, permission Permission) (permissions []Permission, err error)
PermSet sets a permission for the file or directory with the given fileID. If a permission for the same grantee already exists, it will be updated. Otherwise, a new permission will be created. Returns all permissions after the operation.
func (*DriveFS) Query ¶ added in v0.0.5
Query executes a Google Drive API search query and returns matching files. The query uses Google Drive's query syntax. See https://developers.google.com/drive/api/guides/search-files for query syntax.
func (*DriveFS) ReadDir ¶
ReadDir reads the directory with the given fileID and returns a slice of FileInfo for all files and subdirectories within it. Does not include trashed items.
func (*DriveFS) ReadFile ¶
ReadFile reads the entire contents of the file with the given fileID. Returns the file data as a byte slice. Returns ErrNotReadable for Google Apps files (Docs, Sheets, etc.) that cannot be directly downloaded.
func (*DriveFS) Remove ¶
Remove deletes the file or directory with the given fileID. For directories, only empty directories can be removed; otherwise returns ErrNotRemovable. If moveToTrash is true, the file is moved to trash; otherwise it is permanently deleted.
func (*DriveFS) RemoveAll ¶
RemoveAll deletes the file or directory with the given fileID, including all children if it's a directory. If moveToTrash is true, the file is moved to trash; otherwise it is permanently deleted.
func (*DriveFS) Rename ¶
Rename changes the name of the file or directory with the given fileID. Returns the updated FileInfo.
func (*DriveFS) ResolvePath ¶
ResolvePath returns the absolute path from the root to the file with the given fileID. The returned path is a slash-separated string (e.g., "/folder/subfolder/file"). Returns ErrMultiParentsNotSupported if the file has multiple parents.
func (*DriveFS) Shortcut ¶
func (s *DriveFS) Shortcut(parentID FileID, name string, targetID FileID) (info FileInfo, err error)
Shortcut creates a new shortcut with the given name that points to the target file. The shortcut is created in the specified parent directory. Returns the FileInfo of the created shortcut.
type FileID ¶
type FileID string
FileID represents a unique identifier for a file or directory in Google Drive.
type FileInfo ¶
type FileInfo struct {
// Name is the file or directory name.
Name string
// ID is the unique identifier for this file or directory.
ID FileID
// Size is the file size in bytes. For directories and Google Apps files, this is 0.
Size int64
// Mime is the MIME type of the file.
Mime string
// ModTime is the last modification time.
ModTime time.Time
// ShortcutTarget is the ID of the target file if this is a shortcut, empty otherwise.
ShortcutTarget FileID
// WebViewLink is the URL to view the file in a web browser.
WebViewLink string
}
FileInfo contains metadata about a file or directory in Google Drive.
func (FileInfo) IsAppFile ¶
IsAppFile returns true if this FileInfo represents a Google Apps file (e.g., Google Docs, Sheets, Slides).
func (FileInfo) IsShortcut ¶
IsShortcut returns true if this FileInfo represents a shortcut.
type Grantee ¶
type Grantee interface {
// contains filtered or unexported methods
}
Grantee represents an entity that can be granted permission to access a file or directory. This is a sealed interface - use the constructor functions User, Group, Domain, or Anyone.
func Anyone ¶
func Anyone() Grantee
Anyone creates a Grantee representing all users (public access).
type GranteeAnyone ¶
type GranteeAnyone struct{}
GranteeAnyone represents all users (public access).
type GranteeDomain ¶
type GranteeDomain struct {
Domain string
}
GranteeDomain represents all users in a Google Workspace domain.
type GranteeGroup ¶
type GranteeGroup struct {
Email string
}
GranteeGroup represents a Google Group identified by email address.
type GranteeUser ¶
type GranteeUser struct {
Email string
}
GranteeUser represents a specific user identified by email address.
type Path ¶
type Path string
Path represents an absolute path in Google Drive. Paths must start with '/' and use forward slashes as separators (e.g., "/folder/subfolder/file"). Relative path components like "." and ".." are not allowed.
type Permission ¶
type Permission interface {
// ID returns the unique identifier for this permission.
ID() PermissionID
// Grantee returns the entity that has been granted this permission.
Grantee() Grantee
// Role returns the access level granted by this permission.
Role() Role
// AllowFileDiscovery returns true if the file can be discovered through search
// by users who have this permission (applicable to domain and anyone permissions).
AllowFileDiscovery() bool
// contains filtered or unexported methods
}
Permission represents access permissions for a file or directory. This is a sealed interface - use the constructor functions UserPermission, GroupPermission, DomainPermission, or AnyonePermission.
func AnyonePermission ¶
func AnyonePermission(role Role, allowFileDiscovery bool) Permission
AnyonePermission creates a Permission for all users (public access). The allowFileDiscovery parameter controls whether anyone can find the file through search.
func DomainPermission ¶
func DomainPermission(domain string, role Role, allowFileDiscovery bool) Permission
DomainPermission creates a Permission for all users in a Google Workspace domain. The allowFileDiscovery parameter controls whether users can find the file through search.
func GroupPermission ¶
func GroupPermission(email string, role Role) Permission
GroupPermission creates a Permission for a Google Group identified by email.
func UserPermission ¶
func UserPermission(email string, role Role) Permission
UserPermission creates a Permission for a specific user identified by email.
type PermissionID ¶
type PermissionID string
PermissionID represents a unique identifier for a permission.
type Role ¶
type Role string
Role represents the level of access granted by a permission.
const ( // RoleOwner grants full ownership including the ability to delete the file and modify permissions. RoleOwner Role = "owner" // RoleOrganizer grants the ability to organize files in shared drives (shared drives only). RoleOrganizer Role = "organizer" // RoleFileOrganizer grants the ability to organize files in shared drives (shared drives only). RoleFileOrganizer Role = "fileOrganizer" // RoleWriter grants the ability to read and modify the file. RoleWriter Role = "writer" // RoleCommenter grants the ability to read and comment on the file. RoleCommenter Role = "commenter" // RoleReader grants read-only access to the file. RoleReader Role = "reader" )
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package drivefsmust wraps the drivefs package with panic-based error handling.
|
Package drivefsmust wraps the drivefs package with panic-based error handling. |