Creates a new SharePointFileStorage instance
This constructor reads required configuration from environment variables and initializes the Microsoft Graph client.
Error if required environment variables are missing
Private _clientMicrosoft Graph API client
Private _driveThe ID of the SharePoint document library (drive)
Private Optional _rootOptional ID of a subfolder to use as the root folder (if specified)
Private _siteThe ID of the SharePoint site
Protected Readonly providerThe name of this storage provider
Checks if SharePoint provider is properly configured. Returns true if all required Microsoft Graph credentials are present.
Copies a file from one location to another
This method creates a copy of a file at a new location. The original file remains unchanged.
Path to the source file (e.g., 'templates/report-template.docx')
Path where the copy should be created (e.g., 'documents/new-report.docx')
A Promise that resolves to true if successful, false if an error occurs
// Copy a file to a new location with a different name
const copyResult = await storage.CopyObject(
'templates/financial-report.xlsx',
'reports/2024/q1-financial-report.xlsx'
);
if (copyResult) {
console.log('File copied successfully');
} else {
console.error('Failed to copy file');
}
Creates a directory (folder) in SharePoint
This method creates a new folder at the specified path. The parent directory must already exist.
Path where the directory should be created (e.g., 'documents/new-folder')
A Promise that resolves to true if successful, false if an error occurs
// Create a new folder
const createResult = await storage.CreateDirectory('documents/2024-reports');
if (createResult) {
console.log('Folder created successfully');
// Now we can put files in this folder
await storage.PutObject(
'documents/2024-reports/q1-results.xlsx',
fileContent,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
);
} else {
console.error('Failed to create folder');
}
Creates a pre-authenticated download URL for an object
This method generates a time-limited, publicly accessible URL that can be used to download a file without authentication. The URL expires after 10 minutes.
Path to the object to create a download URL for (e.g., 'documents/report.pdf')
A Promise that resolves to the pre-authenticated download URL
Error if the object doesn't exist or the URL creation fails
// Generate a pre-authenticated download URL that will work for 10 minutes
const downloadUrl = await storage.CreatePreAuthDownloadUrl('presentations/quarterly-update.pptx');
console.log(`Download the file using this URL: ${downloadUrl}`);
// You can share this URL with users who don't have SharePoint access
// The URL will expire after 10 minutes
Creates a pre-authenticated upload URL (not supported in SharePoint)
This method is not supported for SharePoint storage as SharePoint doesn't provide a way to generate pre-authenticated upload URLs like object storage services. Instead, use the PutObject method for file uploads.
The object name (path) to create a pre-auth URL for
UnsupportedOperationError always, as this operation is not supported
// This will throw an UnsupportedOperationError
try {
await storage.CreatePreAuthUploadUrl('documents/report.docx');
} catch (error) {
if (error instanceof UnsupportedOperationError) {
console.log('Pre-authenticated upload URLs are not supported in SharePoint.');
// Use PutObject instead
await storage.PutObject('documents/report.docx', fileContent, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
}
}
Deletes a directory (folder) and optionally its contents
This method deletes a folder from SharePoint. By default, it will only delete empty folders unless the recursive parameter is set to true.
Path to the directory to delete (e.g., 'archive/old-reports')
If true, delete the directory and all its contents; if false, only delete if empty
A Promise that resolves to true if successful, false if an error occurs
// Attempt to delete an empty folder
const deleteResult = await storage.DeleteDirectory('temp/empty-folder');
// Delete a folder and all its contents
const recursiveDeleteResult = await storage.DeleteDirectory('archive/old-data', true);
if (recursiveDeleteResult) {
console.log('Folder and all its contents deleted successfully');
} else {
console.error('Failed to delete folder');
}
Deletes an object (file) from SharePoint
This method permanently deletes a file from SharePoint storage. Note that deleted files may be recoverable from the SharePoint recycle bin depending on your SharePoint configuration.
Path to the object to delete (e.g., 'documents/old-report.docx')
A Promise that resolves to true if successful, false if an error occurs
// Delete a file
const deleteResult = await storage.DeleteObject('temp/draft-document.docx');
if (deleteResult) {
console.log('File deleted successfully or already didn\'t exist');
} else {
console.error('Failed to delete file');
}
Checks if a directory exists
This method verifies whether a folder exists at the specified path. Unlike ObjectExists, this method also checks that the item is a folder.
Path to check (e.g., 'documents/reports')
A Promise that resolves to true if the directory exists, false otherwise
// Check if a directory exists before creating a file in it
const dirExists = await storage.DirectoryExists('documents/reports');
if (!dirExists) {
// Create the directory first
await storage.CreateDirectory('documents/reports');
}
// Now we can safely put a file in this directory
await storage.PutObject('documents/reports/annual-summary.pdf', fileContent, 'application/pdf');
Downloads a file's contents
This method retrieves the raw content of a file as a Buffer.
Object identifier (prefer objectId for performance, fallback to fullPath)
A Promise that resolves to a Buffer containing the file's contents
Error if the file doesn't exist or cannot be downloaded
try {
// Fast path: Use objectId (SharePoint item ID)
const fileContent = await storage.GetObject({ objectId: '01BYE5RZ6QN3VYRVNHHFDK2QJODWDDFR4E' });
// Slow path: Use path
const fileContent2 = await storage.GetObject({ fullPath: 'documents/notes.txt' });
// Convert Buffer to string for text files
const textContent = fileContent.toString('utf8');
console.log('File content:', textContent);
// For binary files, you can write the buffer to a local file
// or process it as needed
} catch (error) {
console.error('Error downloading file:', error.message);
}
Gets metadata for a file or folder
This method retrieves metadata information about a file or folder, such as its name, size, content type, and last modified date.
Object identifier (prefer objectId for performance, fallback to fullPath)
A Promise that resolves to a StorageObjectMetadata object
Error if the object doesn't exist or cannot be accessed
try {
// Fast path: Use objectId (SharePoint item ID)
const metadata = await storage.GetObjectMetadata({ objectId: '01BYE5RZ6QN3VYRVNHHFDK2QJODWDDFR4E' });
// Slow path: Use path
const metadata2 = await storage.GetObjectMetadata({ fullPath: 'presentations/quarterly-update.pptx' });
console.log(`Name: ${metadata.name}`);
console.log(`Size: ${metadata.size} bytes`);
console.log(`Content Type: ${metadata.contentType}`);
console.log(`Last Modified: ${metadata.lastModified}`);
console.log(`Is Directory: ${metadata.isDirectory}`);
} catch (error) {
console.error('Error getting metadata:', error.message);
}
Lists objects in a given directory (folder)
This method retrieves all files and subfolders in the specified directory. It returns both a list of object metadata and a list of directory prefixes.
Path to the directory to list (e.g., 'documents/reports')
Optional delimiter: stringOptional delimiter character (not used in this implementation)
A Promise that resolves to a StorageListResult containing objects and prefixes
objects array in the result includes both files and foldersprefixes array includes only folder paths (with trailing slashes)// List all files and folders in the 'documents' directory
const result = await storage.ListObjects('documents');
// Process files
for (const obj of result.objects) {
console.log(`Name: ${obj.name}, Size: ${obj.size}, Type: ${obj.isDirectory ? 'Folder' : 'File'}`);
}
// Process subfolders
for (const prefix of result.prefixes) {
console.log(`Subfolder: ${prefix}`);
}
Moves an object from one location to another
This method moves a file or folder from one location in SharePoint to another. It handles both renaming and changing the parent folder.
Current path of the object (e.g., 'old-folder/document.docx')
New path for the object (e.g., 'new-folder/renamed-document.docx')
A Promise that resolves to true if successful, false otherwise
// Move a file to a different folder
const moveResult = await storage.MoveObject(
'documents/old-report.pdf',
'archive/2023/annual-report.pdf'
);
if (moveResult) {
console.log('File moved successfully');
} else {
console.error('Failed to move file');
}
Checks if a file or folder exists
This method verifies whether an object (file or folder) exists at the specified path.
Path to check (e.g., 'documents/report.pdf')
A Promise that resolves to true if the object exists, false otherwise
// Check if a file exists before attempting to download it
const exists = await storage.ObjectExists('presentations/quarterly-update.pptx');
if (exists) {
// File exists, proceed with download
const fileContent = await storage.GetObject('presentations/quarterly-update.pptx');
// Process the file...
} else {
console.log('File does not exist');
}
Uploads a file to SharePoint
This method uploads a file to SharePoint at the specified path. It automatically determines whether to use a simple upload or a chunked upload based on file size.
Path where the file should be uploaded (e.g., 'documents/report.pdf')
Buffer containing the file content
Optional contentType: stringOptional MIME type of the file (if not provided, it will be guessed from the filename)
Optional metadata: Record<string, string>Optional metadata to associate with the file (not used in SharePoint implementation)
A Promise that resolves to true if successful, false if an error occurs
// Create a text file
const textContent = Buffer.from('This is a sample document', 'utf8');
const uploadResult = await storage.PutObject(
'documents/sample.txt',
textContent,
'text/plain'
);
// Upload a large file using chunked upload
const largeFileBuffer = fs.readFileSync('/path/to/large-presentation.pptx');
const largeUploadResult = await storage.PutObject(
'presentations/quarterly-results.pptx',
largeFileBuffer,
'application/vnd.openxmlformats-officedocument.presentationml.presentation'
);
if (largeUploadResult) {
console.log('Large file uploaded successfully');
} else {
console.error('Failed to upload large file');
}
Search files in SharePoint using Microsoft Graph Search API.
This method provides powerful search capabilities using KQL (Keyword Query Language), SharePoint's native query language. The search can target file names, metadata, and optionally file contents.
The search query string. Can be plain text or use KQL syntax for advanced queries.
Optional options: FileSearchOptionsOptional search configuration including filters, limits, and content search
A Promise resolving to FileSearchResultSet with matched files and pagination info
KQL Query Syntax Examples:
"quarterly report" - searches for files containing these terms"budget AND 2024", "draft OR final", "report NOT internal""proj*" matches "project", "projection", etc."FileType:pdf", "Author:John Smith", "Size>1000000""Created>=2024-01-01", "LastModifiedTime<2024-12-31""project NEAR report" - finds terms near each other"\"annual budget report\"" - exact phrase matchAdditional Filtering: The method automatically adds KQL filters based on the provided options:
fileTypes: Adds FileType filters (e.g., FileType:pdf OR FileType:docx)modifiedAfter/modifiedBefore: Adds LastModifiedTime filterspathPrefix: Adds Path filter to restrict search to a directorysearchContent: When false, restricts search to filename only// Simple text search in filenames
const results = await storage.SearchFiles('quarterly report', {
maxResults: 20
});
// Search for PDFs only
const pdfResults = await storage.SearchFiles('budget', {
fileTypes: ['pdf'],
maxResults: 50
});
// Search with date range
const recentResults = await storage.SearchFiles('meeting notes', {
modifiedAfter: new Date('2024-01-01'),
modifiedBefore: new Date('2024-12-31'),
searchContent: true
});
// Search within specific directory
const folderResults = await storage.SearchFiles('presentation', {
pathPrefix: 'documents/reports',
fileTypes: ['pptx', 'pdf']
});
// Advanced KQL query
const advancedResults = await storage.SearchFiles(
'FileType:xlsx AND Created>=2024-01-01 AND Author:"John Smith"',
{ maxResults: 100 }
);
Private _getPrivate Gets a SharePoint item by its path
This helper method retrieves a SharePoint item (file or folder) using its path. It handles path normalization and root folder redirection.
The path of the item to retrieve (e.g., 'documents/reports/report.docx')
A Promise that resolves to the SharePoint item
Error if the item doesn't exist or cannot be accessed
Private _getPrivate Gets the SharePoint item ID for a folder at the specified path
This helper method navigates the folder hierarchy in SharePoint to find the folder specified by the path, returning its item ID.
The path to get the parent folder for (e.g., 'documents/reports')
A Promise that resolves to the parent folder ID
Error if any folder in the path doesn't exist
Private _itemPrivate Converts a SharePoint item to a StorageObjectMetadata object
This helper method transforms the Microsoft Graph API item representation into the standard StorageObjectMetadata format used by the FileStorageBase interface.
The SharePoint item from the Microsoft Graph API
A StorageObjectMetadata object representing the item
Private buildKQLQueryPrivate Builds a KQL (Keyword Query Language) query string from the base query and search options.
This helper method constructs a properly formatted KQL query by combining the user's search query with filters derived from FileSearchOptions. It handles file type filters, date range filters, path restrictions, and content search options.
The user's search query (plain text or KQL)
Optional options: FileSearchOptionsOptional search options to convert into KQL filters
A complete KQL query string
Private determinePrivate Determines whether the search match was in the filename or content.
This helper method analyzes the hit metadata to determine if the search term was found in the filename versus the file content.
The search hit object from Graph API
True if match was in filename, false if in content, undefined if unknown
Private extractPrivate Extracts the file path from a SharePoint resource object.
This helper method processes the path information from a Graph API resource, removing the drive and site prefixes to return just the file path relative to the configured root folder.
The resource object from Graph API search results
The relative file path
Optional initialization method for storage providers that require async setup.
This method can be overridden by subclasses that need to perform async initialization after construction, such as setting up access tokens, establishing connections, or verifying permissions.
The default implementation does nothing and resolves immediately. Storage provider implementations should override this method if they need to perform async setup.
A Promise that resolves when initialization is complete.
// In a specific provider implementation:
public async initialize(): Promise<void> {
// Set up OAuth tokens or other async initialization
await this.refreshAccessToken();
await this.verifyBucketAccess();
}
// Usage:
const storage = new MyStorageProvider();
await storage.initialize();
// Now the provider is ready to use
Protected throwHelper method to throw an UnsupportedOperationError with appropriate context. This method simplifies implementation of methods not supported by specific providers.
The name of the method that is not supported
UnsupportedOperationError with information about the unsupported method and provider
Private transformPrivate Transforms Microsoft Graph Search API response into FileSearchResultSet format.
This helper method processes the raw search response from the Graph API, extracting relevant file information and converting it to the standard FileSearchResult format. It handles pagination info and calculates relevance scores.
The raw response from Microsoft Graph Search API
The maximum number of results requested
A FileSearchResultSet with transformed results
FileStorageBase implementation for Microsoft SharePoint using the Microsoft Graph API
This provider allows working with files stored in SharePoint document libraries. It uses the Microsoft Graph API and client credentials authentication flow to securely access and manipulate SharePoint files and folders.
Remarks
This implementation requires the following environment variables:
To use this provider, you need to:
Example