Skip to main content

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.

public class S3FileSystemProvider : IFileSystemProvider

Constructors​

Instance member 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";

Instance member 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.

NameTypeDescription
rootStringThe root path of this file system. The first segment is the S3 bucket name.
nameStringThe name of this file system.

The root path is also the current directory path.

Throws:

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​

Instance member AccessKey​

String: Returns or sets the S3 Access Key. (Default: null)

Instance member AccessSecret​

String: Returns or sets the S3 Access Secret string. (Default: null)

Instance member Icon​

Image: Returns or sets the icon that represents the file system. (Default: null)

Instance member IconSource​

String: Returns or sets the icon name or URL that represents the file system. (Default: "resource.wx/icon-cloud-storage.svg")

Instance member Name​

String: Returns or sets the name of this root. This is the name that should be shown to the user.

Instance member 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​

Instance member Contains(path)​

Checks whether the specified path starts with the Name of this file system provider.

ParameterTypeDescription
pathStringPath 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

Instance member CreateDirectory(path)​

Creates the specified directory and sub-directories.

ParameterTypeDescription
pathStringPath 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:

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");

Instance member DeleteDirectory(path, recursive)​

Deletes the specified directory and, optionally, sub-directories.

ParameterTypeDescription
pathStringPath of the directory to delete.
recursiveBooleanIndicates 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:

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);

Instance member DeleteFile(path)​

Deletes the specified file.

ParameterTypeDescription
pathStringPath of the file to delete.

Throws:

Example:

var path = @"Cloud Documents\Reports\old-report.pdf";

if (s3.Exists(path))
s3.DeleteFile(path);

Instance member Exists(path)​

Returns whether the specified file or directory exists.

ParameterTypeDescription
pathStringPath of the file or directory to check.

Returns: Boolean. True of the file or directory exists.

Throws:

Example:

var path = @"Cloud Documents\Reports\report.pdf";

if (!s3.Exists(path))
AlertBox.Show("The file was not found.");

Instance member GetAttributes(path)​

Returns the FileAttributes for the specified path .

ParameterTypeDescription
pathStringFile 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:

Example:

var attributes = s3.GetAttributes("Cloud Documents");
var isDirectory = (attributes & FileAttributes.Directory) == FileAttributes.Directory;

Instance member GetCreationTime(path)​

Returns the file's creation time.

ParameterTypeDescription
pathStringPath 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:

Example:

var created = s3.GetCreationTime(@"Cloud Documents\Reports\report.pdf");
AlertBox.Show("Created: " + created.ToString("g"));

Instance member GetDirectories(path, pattern, searchOption)​

Returns a list of directory paths that match the pattern and search options in the specified path.

ParameterTypeDescription
pathStringPath to search into.
patternStringWild card pattern to match.
searchOption optionalSearchOptionOne 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:

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);

Instance member GetFiles(path, pattern, searchOption)​

Returns a list of file paths that match the pattern and search options in the specified path.

ParameterTypeDescription
pathStringPath to search into.
patternStringWild card pattern to match.
searchOption optionalSearchOptionOne 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:

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);

Instance member GetFileSize(path)​

Returns the size of the file.

ParameterTypeDescription
pathStringPath 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:

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");

Instance member GetLastWriteTime(path)​

Returns the last write timestamp for the specified file.

ParameterTypeDescription
pathStringPath 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:

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);
}

Instance member MapPath(path)​

Maps the virtual path to the corresponding physical path on the specific IFileSystemProvider implementation.

ParameterTypeDescription
pathStringVirtual 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/"

Instance member OpenFileStream(path, mode, access)​

Opens the specified file for reading or writing.

ParameterTypeDescription
pathStringThe path of the file to open.
modeFileModeSpecified if the file should be opened, created, overwritten or truncated.
accessFileAccessSpecified 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:

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();
}

Instance member RenameDirectory(path, newName)​

Renames the specified directory.

ParameterTypeDescription
pathStringPath of the directory to rename.
newNameStringThe new directory name.

S3 has no rename operation, so the directory marker object is moved to the new key.

Throws:

Example:

// renames "Reports\Drafts" to "Reports\Archive".
s3.RenameDirectory(@"Cloud Documents\Reports\Drafts", "Archive");

Instance member RenameFile(path, newName)​

Renames the specified file.

ParameterTypeDescription
pathStringPath of the file to rename.
newNameStringThe new file name.

The file stays in the same directory. S3 has no rename operation, so the object is moved to the new key.

Throws:

Example:

// renames "Reports\draft.docx" to "Reports\final.docx".
s3.RenameFile(@"Cloud Documents\Reports\draft.docx", "final.docx");