Documentation
¶
Overview ¶
Package drive provides a client for interacting with the Google Drive API.
This package enables comprehensive Google Drive file management operations including:
- Uploading files with metadata
- Listing and searching files and folders
- Downloading file content
- Deleting files
- Creating folders
- Moving and renaming files
- Managing file sharing and permissions
The client supports multi-account functionality, allowing management of multiple Google accounts simultaneously. Each client instance is bound to a specific account.
OAuth Authentication: This package uses the unified Google OAuth token from the google package. The OAuth scope includes full Google Drive access (drive scope), allowing read and write operations on all files in the user's Drive.
Example usage:
ctx := context.Background()
client, err := drive.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
// Upload a file
file, err := client.UploadFile(ctx, "document.pdf", bytes.NewReader(content), "application/pdf", nil)
if err != nil {
log.Fatal(err)
}
// List files
files, err := client.ListFiles(ctx, &drive.ListOptions{
Query: "mimeType='application/pdf'",
MaxResults: 10,
})
Index ¶
- Constants
- func HasToken() bool
- func HasTokenForAccount(account string) bool
- func HasTokenForAccountWithProvider(account string, provider google.TokenProvider) bool
- type Client
- func NewClient(ctx context.Context) (*Client, error)
- func NewClientForAccount(ctx context.Context, account string) (*Client, error)
- func NewClientForAccountWithProvider(ctx context.Context, account string, tokenProvider google.TokenProvider) (*Client, error)
- func NewClientWithProvider(ctx context.Context, provider google.TokenProvider) (*Client, error)
- func (c *Client) Account() string
- func (c *Client) CreateFolder(ctx context.Context, name string, parentFolders []string) (*FileInfo, error)
- func (c *Client) DeleteFile(ctx context.Context, fileID string) error
- func (c *Client) DownloadFile(ctx context.Context, fileID string) (io.ReadCloser, error)
- func (c *Client) GetFile(ctx context.Context, fileID string) (*FileInfo, error)
- func (c *Client) ListFiles(ctx context.Context, options *ListOptions) ([]*FileInfo, string, error)
- func (c *Client) ListPermissions(ctx context.Context, fileID string) ([]*Permission, error)
- func (c *Client) MoveFile(ctx context.Context, fileID string, options *MoveOptions) (*FileInfo, error)
- func (c *Client) RemovePermission(ctx context.Context, fileID, permissionID string) error
- func (c *Client) ShareFile(ctx context.Context, fileID string, options *ShareOptions) (*Permission, error)
- func (c *Client) UploadFile(ctx context.Context, name string, content io.Reader, options *UploadOptions) (*FileInfo, error)
- type FileInfo
- type ListOptions
- type MoveOptions
- type Permission
- type ShareOptions
- type UploadOptions
- type User
Constants ¶
const (
// FolderMimeType is the MIME type for Google Drive folders
FolderMimeType = "application/vnd.google-apps.folder"
)
Variables ¶
This section is empty.
Functions ¶
func HasToken ¶
func HasToken() bool
HasToken checks if a valid OAuth token exists for the default account
func HasTokenForAccount ¶
HasTokenForAccount checks if a valid OAuth token exists for the specified account
func HasTokenForAccountWithProvider ¶ added in v0.0.27
func HasTokenForAccountWithProvider(account string, provider google.TokenProvider) bool
HasTokenForAccountWithProvider checks if a valid OAuth token exists for the specified account
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps the Google Drive API service
func NewClient ¶
NewClient creates a new Google Drive client with OAuth2 authentication for the default account Returns an error if no valid token exists - use HasToken() to check first
func NewClientForAccount ¶
NewClientForAccount creates a new Google Drive client with OAuth2 authentication for a specific account Uses the default file-based token provider for backward compatibility
func NewClientForAccountWithProvider ¶ added in v0.0.27
func NewClientForAccountWithProvider(ctx context.Context, account string, tokenProvider google.TokenProvider) (*Client, error)
NewClientForAccountWithProvider creates a new Google Drive client with OAuth2 authentication for a specific account The OAuth token is retrieved from the provided token provider
func NewClientWithProvider ¶ added in v0.0.27
NewClientWithProvider creates a new Google Drive client with OAuth2 authentication for the default account using the provided token provider
func (*Client) CreateFolder ¶
func (c *Client) CreateFolder(ctx context.Context, name string, parentFolders []string) (*FileInfo, error)
CreateFolder creates a new folder in Google Drive
func (*Client) DeleteFile ¶
DeleteFile deletes a file from Google Drive
func (*Client) DownloadFile ¶
DownloadFile downloads the content of a file
func (*Client) ListPermissions ¶
ListPermissions lists all permissions for a file
func (*Client) MoveFile ¶
func (c *Client) MoveFile(ctx context.Context, fileID string, options *MoveOptions) (*FileInfo, error)
MoveFile moves or renames a file
func (*Client) RemovePermission ¶
RemovePermission removes a permission from a file
func (*Client) ShareFile ¶
func (c *Client) ShareFile(ctx context.Context, fileID string, options *ShareOptions) (*Permission, error)
ShareFile creates a permission on a file to share it
type FileInfo ¶
type FileInfo struct {
// ID is the unique identifier for the file
ID string `json:"id"`
// Name is the name of the file
Name string `json:"name"`
// MimeType is the MIME type of the file
MimeType string `json:"mimeType"`
// Size is the size of the file in bytes (not populated for folders)
Size int64 `json:"size,omitempty"`
// CreatedTime is when the file was created
CreatedTime time.Time `json:"createdTime"`
// ModifiedTime is when the file was last modified
ModifiedTime time.Time `json:"modifiedTime"`
// WebViewLink is a link for opening the file in a relevant Google editor or viewer
WebViewLink string `json:"webViewLink,omitempty"`
// WebContentLink is a link for downloading the file content (not available for folders)
WebContentLink string `json:"webContentLink,omitempty"`
// Parents are the IDs of the parent folders
Parents []string `json:"parents,omitempty"`
// Owners are the owners of the file
Owners []User `json:"owners,omitempty"`
Shared bool `json:"shared"`
// Permissions are the access permissions for the file
Permissions []Permission `json:"permissions,omitempty"`
// TrashedTime is when the file was trashed (if trashed)
TrashedTime *time.Time `json:"trashedTime,omitempty"`
// Trashed indicates whether the file is in the trash
Trashed bool `json:"trashed"`
}
FileInfo represents metadata about a file or folder in Google Drive
type ListOptions ¶
type ListOptions struct {
// Query is a query for filtering the file results using Google Drive's query language
// See https://developers.google.com/drive/api/guides/search-files
// Examples:
// "name contains 'report'"
// "mimeType='application/pdf'"
// "'me' in owners"
// "trashed=false and 'root' in parents"
Query string
// MaxResults is the maximum number of files to return (max: 1000)
MaxResults int
// OrderBy specifies the sort order of the result set
// Examples: "folder,modifiedTime desc,name"
OrderBy string
// PageToken is a token for retrieving the next page of results
PageToken string
// IncludeTrashed includes trashed files in results
IncludeTrashed bool
// Spaces is a comma-separated list of spaces to query (drive, appDataFolder, photos)
Spaces string
}
ListOptions contains options for listing files
type MoveOptions ¶
type MoveOptions struct {
// NewName is the new name for the file (leave empty to keep current name)
NewName string
// AddParents are folder IDs to add as parents
AddParents []string
// RemoveParents are folder IDs to remove as parents
RemoveParents []string
}
MoveOptions contains options for moving or renaming a file
type Permission ¶
type Permission struct {
// ID is the unique identifier for the permission
ID string `json:"id"`
// Type is the type of grantee (user, group, domain, anyone)
Type string `json:"type"`
// Role is the role granted by this permission (owner, organizer, fileOrganizer, writer, commenter, reader)
Role string `json:"role"`
// EmailAddress is the email address of the user or group (if type is user or group)
EmailAddress string `json:"emailAddress,omitempty"`
// Domain is the domain to which this permission refers (if type is domain)
Domain string `json:"domain,omitempty"`
// DisplayName is the display name of the user or group
DisplayName string `json:"displayName,omitempty"`
}
Permission represents access permissions for a file
type ShareOptions ¶
type ShareOptions struct {
Type string
Role string
EmailAddress string
Domain string
SendNotificationEmail bool
EmailMessage string
}
ShareOptions contains options for sharing a file
type UploadOptions ¶
type UploadOptions struct {
// ParentFolders are the IDs of parent folders where the file should be placed
ParentFolders []string
// Description is a short description of the file
Description string
// MimeType is the MIME type of the file (e.g., "application/pdf", "image/png")
// If not specified, Drive will attempt to detect it automatically
MimeType string
// ModifiedTime allows setting a custom modification time
ModifiedTime *time.Time
}
UploadOptions contains options for uploading a file
type User ¶
type User struct {
// DisplayName is the display name of the user
DisplayName string `json:"displayName"`
// EmailAddress is the email address of the user
EmailAddress string `json:"emailAddress"`
// PhotoLink is a link to the user's profile photo
PhotoLink string `json:"photoLink,omitempty"`
}
User represents a Google Drive user (owner, permission holder, etc.)