A very simple .NET interface to blob storage. Supports these providers:
- The file system
- Azure Storage blob containers
Available as NuGet packages:
Versions:
- 0.1.0 has been used in production for several years with no issues reported.
- 1.0.0 is in development.
First, add the appriate using
directives:
using Sharp.BlobStorage; // always
using Sharp.BlobStorage.File; // to use the filesystem
using Sharp.BlobStorage.Azure; // to use Azure Storage
Next, create the appropriate blob storage client. To store blobs as files in the computer's file system:
private IBlobStorage CreateBlobStorage()
{
var configuration = new FileBlobStorageConfiguration
{
Path = @"C:\Blobs"
};
return new FileBlobStorage(configuration);
}
To store blobs in an Azure Storage blob container:
private IBlobStorage CreateBlobStorage()
{
var configuration = new AzureBlobStorageConfiguration
{
ConnectionString = @"...an Azure Storage connection string...",
ContainerName = @"blobs"
};
return new AzureBlobStorage(configuration);
}
And, to create the client:
var storage = CreateBlobStorage();
The Sharp.BlobStorage IBlobStorage
interface exposes a minimal set of
operations, each with synchronous and asynchronous variants.
Synchronous | Asynchronous | Description |
---|---|---|
Put |
PutAsync |
Creates (uploads) a blob |
Get |
GetAsync |
Gets (downloads) a blob |
Delete |
DeleteAsync |
Deletes a blob |
IBlobStorage
methods identify each blob by a semi-random URI generated when
the blob is created. Blob URIs are not specific to an underlying storage
provider. Rather, blob URIs are generalized so that they can identify the same
blobs regardless of provider configuration changes.
The methods represent blob content as Stream
objects, rather than strings
or byte arrays. This is a conscious design decision intended to force
encourage application authors to use a streaming approach when dealing with
blobs. Streaming enables applications to handle arbitrarily large blobs without
exhausting available memory.
Given a readable Stream
object and a filename extension, it is possible to
create (upload) a new blob storing the content of the stream.
var uri = storage.Put(stream, ".txt");
// or
var uri = await storage.PutAsync(stream, ".txt");
The caller should save the returned URI, as it is the only way to identify the blob.
Put
and PutAsync
do not dispose stream
.
Given a blob URI, it is possible to retrieve (download) the blob's content as a
readable Stream
object.
using (var stream = storage.Get(uri))
{
// read stream in here
}
// or
using (var stream = await storage.GetAsync(uri))
{
// read stream in here
}
The caller should ensure that the stream returned by Get
and GetAsync
is
disposed when done.
Given a blob URI, it is possible to delete the blob.
var existed = storage.Delete(uri);
// or
var existed = await storage.DeleteAsync(uri);
The method succeeds regardless of whether the blob exists. If the blob exists
and is deleted, the method returns true
; otherwise, false
.
- To test Azure storage support, a working
docker
command is required.