S3FileSystemProvider
Namespace: Wisej.Ext.FileSystem
Assembly: Wisej.Ext.FileSystem.AmazonS3 (4.1.0.0)
Implementation of the Amazon S3 file system. Provides access to the S3 storage as a file system.
- C#
- VB.NET
public class S3FileSystemProvider : IFileSystemProvider
Public Class S3FileSystemProvider
Inherits IFileSystemProvider
Constructors
S3FileSystemProvider()
Initializes a blank instance of S3FileSystemProvider.
The IconSource is initialized to the default cloud storage icon. Root, Name, AccessKey and AccessSecret must be set before the provider can be used.
Example:
var s3 = new S3FileSystemProvider();
s3.Root = "my-bucket/documents";
s3.Name = "Cloud Documents";
s3.AccessKey = "AKIAIOSFODNN7EXAMPLE";
s3.AccessSecret = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
S3FileSystemProvider(root, name)
Creates a new instance of the S3FileSystemProvider class with the specified root and name . The root is added in front of all path arguments passed to all methods in this class.
| Name | Type | Description |
|---|---|---|
| root | String | The root path of this file system. The first segment is the S3 bucket name. |
| name | String | The name of this file system. |
The root path is also the current directory path.
Throws:
- ArgumentNullException root or name is null or empty.
Example:
var s3 = new S3FileSystemProvider("my-bucket/documents", "Cloud Documents")
{
AccessKey = "AKIAIOSFODNN7EXAMPLE",
AccessSecret = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
};
var dialog = new OpenFileDialog();
dialog.Roots.Add(s3);
dialog.ShowDialog();
Properties
AccessKey
String: Returns or sets the S3 Access Key. (Default: null)
AccessSecret
String: Returns or sets the S3 Access Secret string. (Default: null)
Icon
Image: Returns or sets the icon that represents the file system. (Default: null)
IconSource
String: Returns or sets the icon name or URL that represents the file system. (Default: "resource.wx/icon-cloud-storage.svg")
Name
String: Returns or sets the name of this root. This is the name that should be shown to the user.
Root
String: Returns or sets the root path for the file system. All file system operations in the implementation class are expected to be limited to the root.
For the S3 file system, the first name in the root path is the bucket name. i.e.: this.Root = "BUCKET_NAME\folder1\folder2";
Methods
Contains(path)
Checks whether the specified path starts with the Name of this file system provider.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path of the file to check. |
Returns: Boolean. True if the specified path starts with the name of this file system provider.
Returns false when path is null or empty.
Example:
var s3 = new S3FileSystemProvider("my-bucket/documents", "Cloud Documents");
s3.Contains(@"Cloud Documents\Reports\report.pdf"); // true
s3.Contains(@"Local Files\report.pdf"); // false
CreateDirectory(path)
Creates the specified directory and sub-directories.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path of the directory to create. |
S3 has no real directories: this method stores an empty object whose key ends with "/", which is how S3 tools represent a folder.
Throws:
- ArgumentNullException path is null.
Example:
var s3 = new S3FileSystemProvider("my-bucket/documents", "Cloud Documents");
// creates the object "documents/Reports/2026/" in "my-bucket".
s3.CreateDirectory(@"Cloud Documents\Reports\2026");
DeleteDirectory(path, recursive)
Deletes the specified directory and, optionally, sub-directories.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path of the directory to delete. |
| recursive | Boolean | Indicates whether to delete sub directories. |
The current implementation deletes only the directory marker object (the key ending with "/"). The recursive argument is not used, and objects stored under the directory prefix are not removed.
Throws:
- ArgumentNullException path is null.
Example:
var s3 = new S3FileSystemProvider("my-bucket/documents", "Cloud Documents");
// delete the files first, then the directory marker.
foreach (var file in s3.GetFiles(@"Cloud Documents\Temp", "*"))
s3.DeleteFile(file);
s3.DeleteDirectory(@"Cloud Documents\Temp", false);
DeleteFile(path)
Deletes the specified file.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path of the file to delete. |
Throws:
- ArgumentNullException path is null.
Example:
var path = @"Cloud Documents\Reports\old-report.pdf";
if (s3.Exists(path))
s3.DeleteFile(path);
Exists(path)
Returns whether the specified file or directory exists.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path of the file or directory to check. |
Returns: Boolean. True of the file or directory exists.
Throws:
- ArgumentNullException path is null.
Example:
var path = @"Cloud Documents\Reports\report.pdf";
if (!s3.Exists(path))
AlertBox.Show("The file was not found.");
GetAttributes(path)
Returns the FileAttributes for the specified path .
| Parameter | Type | Description |
|---|---|---|
| path | String | File path for which to retrieve the FileAttributes. |
Returns: FileAttributes. An instance of FileAttributes with the relevant flags set.
S3 doesn't store file attributes. This method returns Normal, and adds Directory when the mapped path ends with "/" (for example the root). S3 is not queried.
Throws:
- ArgumentNullException path is null.
Example:
var attributes = s3.GetAttributes("Cloud Documents");
var isDirectory = (attributes & FileAttributes.Directory) == FileAttributes.Directory;
GetCreationTime(path)
Returns the file's creation time.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path of the file to query. |
Returns: DateTime. A DateTime representing the timestamp of the file creation.
The Amazon S3 file system only returns the LastModified date, therefore this method returns the same value as GetLastWriteTime.
Throws:
- ArgumentNullException path is null.
Example:
var created = s3.GetCreationTime(@"Cloud Documents\Reports\report.pdf");
AlertBox.Show("Created: " + created.ToString("g"));
GetDirectories(path, pattern, searchOption)
Returns a list of directory paths that match the pattern and search options in the specified path.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path to search into. |
| pattern | String | Wild card pattern to match. |
| searchOption | SearchOption | One of the SearchOption options. |
Returns: String[]. A Array containing the full path of the directories that match the search pattern and search options.
The returned paths start with Name and use "" as the separator. The pattern is matched against the full S3 object key, so it should start with "*". The objects returned by this call are cached and reused by GetLastWriteTime and GetFileSize.
Throws:
- ArgumentNullException path is null.
Example:
// top-level directories only.
var folders = s3.GetDirectories("Cloud Documents", "*");
// all directories whose name ends with "2026", at any depth.
var yearly = s3.GetDirectories("Cloud Documents", "*2026/", SearchOption.AllDirectories);
GetFiles(path, pattern, searchOption)
Returns a list of file paths that match the pattern and search options in the specified path.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path to search into. |
| pattern | String | Wild card pattern to match. |
| searchOption | SearchOption | One of the SearchOption options. |
Returns: String[]. A Array containing the full path of the files that match the search pattern and search options.
The returned paths start with Name and use "" as the separator. The pattern is matched against the full S3 object key, so it should start with "*". The objects returned by this call are cached and reused by GetLastWriteTime and GetFileSize.
Throws:
- ArgumentNullException path is null.
Example:
// all PDF files in the Reports directory and its sub-directories.
var files = s3.GetFiles(@"Cloud Documents\Reports", "*.pdf", SearchOption.AllDirectories);
foreach (var file in files)
listBox1.Items.Add(file);
GetFileSize(path)
Returns the size of the file.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path of the file to query. |
Returns: Int64. The size of the file in bytes.
The value is taken from the objects cached by the last call to GetFiles or GetDirectories when available; otherwise the object metadata is requested from S3.
Throws:
- ArgumentNullException path is null.
Example:
long total = 0;
foreach (var file in s3.GetFiles(@"Cloud Documents\Reports", "*", SearchOption.AllDirectories))
total += s3.GetFileSize(file);
AlertBox.Show($"Total size: {total / 1024} KB");
GetLastWriteTime(path)
Returns the last write timestamp for the specified file.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path of the file to query. |
Returns: DateTime. A DateTime representing the timestamp of the last time the file was written.
The value is taken from the objects cached by the last call to GetFiles or GetDirectories when available; otherwise the object metadata is requested from S3.
Throws:
- ArgumentNullException path is null.
Example:
foreach (var file in s3.GetFiles(@"Cloud Documents\Reports", "*.pdf"))
{
var modified = s3.GetLastWriteTime(file);
if (modified < DateTime.Now.AddDays(-30))
s3.DeleteFile(file);
}
MapPath(path)
Maps the virtual path to the corresponding physical path on the specific IFileSystemProvider implementation.
| Parameter | Type | Description |
|---|---|---|
| path | String | Virtual path to map to the corresponding physical path. |
Returns: String. The physical path for the IFileSystemProvider implementation.
The Name prefix is replaced with Root and "" separators are converted to "/". Paths that already start with Root are returned unchanged, and a null or empty path returns Root.
Example:
var s3 = new S3FileSystemProvider("my-bucket/documents", "Cloud Documents");
s3.MapPath(@"Cloud Documents\Reports\report.pdf"); // "my-bucket/documents/Reports/report.pdf"
s3.MapPath("Cloud Documents"); // "my-bucket/documents/"
OpenFileStream(path, mode, access)
Opens the specified file for reading or writing.
| Parameter | Type | Description |
|---|---|---|
| path | String | The path of the file to open. |
| mode | FileMode | Specified if the file should be opened, created, overwritten or truncated. |
| access | FileAccess | Specified if the stream should be opened for reading or writing. |
Returns: Stream. A Stream that can be used to read or write the content of the file.
Supported combinations are Open with Read or Write, Create, and CreateNew with Write.
Throws:
- ArgumentNullException path is null.
- NotSupportedException The combination of mode and access is not supported.
Example:
// write a new file.
using (var stream = s3.OpenFileStream(@"Cloud Documents\notes.txt", FileMode.Create, FileAccess.Write))
using (var writer = new StreamWriter(stream))
{
writer.Write("Hello from Wisej.NET");
}
// read it back.
using (var stream = s3.OpenFileStream(@"Cloud Documents\notes.txt", FileMode.Open, FileAccess.Read))
using (var reader = new StreamReader(stream))
{
var text = reader.ReadToEnd();
}
RenameDirectory(path, newName)
Renames the specified directory.
| Parameter | Type | Description |
|---|---|---|
| path | String | Path of the directory to rename. |
| newName | String | The new directory name. |
S3 has no rename operation, so the directory marker object is moved to the new key.
Throws:
- ArgumentNullException path or newName is null.
Example:
// renames "Reports\Drafts" to "Reports\Archive".
s3.RenameDirectory(@"Cloud Documents\Reports\Drafts", "Archive");
RenameFile(path, newName)
Renames the specified file.
The file stays in the same directory. S3 has no rename operation, so the object is moved to the new key.
Throws:
- ArgumentNullException path or newName is null.
Example:
// renames "Reports\draft.docx" to "Reports\final.docx".
s3.RenameFile(@"Cloud Documents\Reports\draft.docx", "final.docx");