Giter Club home page Giter Club logo

blobhelper's Introduction

Blobject

Blobject (formerly BlobHelper) is a common, consistent storage interface for Microsoft Azure, Amazon S3, S3 compatible storage (i.e. Minio, Less3, View), CIFS (Windows file shares), NFS (Linux and UNIX file shares), and local filesystem written in C#.

Help, Feedback, Contribute

If you have any issues or feedback, please file an issue here in Github. We'd love to have you help by contributing code for new features, optimization to the existing codebase, ideas for future releases, or fixes!

Overview

This project was built to provide a simple interface over external storage to help support projects that need to work with potentially multiple storage providers. It is by no means a comprehensive interface, rather, it supports core methods for creation, retrieval, deletion, metadata, and enumeration.

Contributors

  • @phpfui for adding the original code for BLOB copy functionality
  • @Revazashvili for fixes related to byte array instantiation, Azure, and refactoring
  • @courtzzz for keeping the region list updated

Dependencies

Though this library is MIT licensed, it is dependent upon other libraries, some of which carry a different license. Each of these libraries are included by reference, that is, none of their code has been modified.

Package URL License
AWSSDK.S3 https://github.com/aws/aws-sdk-net Apache 2.0
Azure.Storage.Blobs https://github.com/Azure/azure-sdk-for-net MIT
EzSmb https://github.com/ume05rw/EzSmb LGPL-3.0
SMBLibrary https://github.com/TalAloni/SMBLibrary LGPL-3.0
NFS-Client https://github.com/SonnyX/NFS-Client Unknown, public
Nekodrive https://github.com/nekoni/nekodrive Unknown, public
S3Lite https://github.com/jchristn/S3Lite MIT

New in v5.0.x

  • Rename from BlobHelper to Blobject
  • Added support for CIFS and NFS
  • Remove use of continuation tokens for disk
  • Add S3Lite variant, not dependent on AWSSDK
  • Refactor

Example Project

Refer to the Test project for exercising the library.

Getting Started - AWS S3

using Blobject;

AwsSettings settings = new AwsSettings(
	accessKey, 
	secretKey, 
	"us-west-1",
	bucket);

BlobClient blobs = new BlobClient(settings); 

Getting Started - AWS S3 Compatible Storage (Minio, Less3, etc)

using Blobject.AmazonS3;

AwsSettings settings = new AwsSettings(
	endpoint,      // http://localhost:8000/
	true,          // enable or disable SSL
	accessKey, 
	secretKey, 
	"us-west-1",
	bucket,
	baseUrl        // i.e. http://localhost:8000/{bucket}/{key}
	);

AmazonS3BlobClient blobs = new AmazonS3BlobClient(settings); 

Getting Started - AWS S3 Lite (non-AWS library to reduce dependency drag)

using Blobject.AmazonS3Lite;

// Initialize settings as above
AmazonS3LiteBlobClient blobs = new AmazonS3BlobClient(settings); 

Getting Started - Azure

using Blobject.AzureBlob;

AzureBlobSettings settings = new AzureBlobSettings(
	accountName, 
	accessKey, 
	"https://[accountName].blob.core.windows.net/", 
	containerName);

AzureBlobClient blobs = new AzureBlobClient(settings); 

Getting Started - CIFS

using Blobject.CIFS;

CifsSettings settings = new CifsSettings(
	"localhost",
	username,
	password,
	sharename);

CifsBlobClient blobs = new CifsBlobClient(settings);

Getting Started - Disk

using Blobject.Disk;

DiskSettings settings = new DiskSettings("blobs"); 

DiskBlobClient blobs = new DiskBlobClient(settings);

Getting Started - NFS

using Blobject.NFS;

NfsSettings settings = new NfsSettings(
	"localhost",
	0, // user ID
	0, // group ID,
	sharename,
	NfsVersionEnum.V3 // V2, V3, or V4
	);

NfsBlobClient = new NfsBlobClient(settings);

Getting Started (Byte Arrays for Smaller Objects)

await blobs.WriteAsync("test", "text/plain", "This is some data");  // throws IOException
byte[] data = await blobs.GetAsync("test");                         // throws IOException
bool exists = await blobs.ExistsAsync("test");
await blobs.DeleteAsync("test");

Getting Started (Streams for Larger Objects)

// Writing a file using a stream
FileInfo fi = new FileInfo(inputFile);
long contentLength = fi.Length;

using (FileStream fs = new FileStream(inputFile, FileMode.Open))
{
    await _Blobs.WriteAsync("key", "content-type", contentLength, fs);  // throws IOException
}

// Downloading to a stream
BlobData blob = await _Blobs.GetStreamAsync(key);
// read blob.ContentLength bytes from blob.Data

Accessing Files within Folders

//
// Use a key of the form [path]/[to]/[file]/[filename].[ext]
//
await blobs.WriteAsync("subdirectory/filename.ext", "text/plain", "Hello!");

Metadata and Enumeration

// Get BLOB metadata
BlobMetadata md = await _Blobs.GetMetadataAsync("key");

// Enumerate BLOBs
EnumerationResult result = await _Blobs.EnumerateAsync();
// list of BlobMetadata contained in result.Blobs
// continuation token in result.NextContinuationToken

Copying BLOBs from Repository to Repository

If you have multiple storage repositories and wish to move BLOBs from one repository to another, use the BlobCopy class (refer to the Test.Copy project for a full working example).

Thanks to @phpfui for contributing code and the idea for this enhancement!

// instantiate two BLOB clients
BlobCopy copy = new BlobCopy(from, to);
CopyStatistics stats = copy.Start();
/*
	{
	  "Success": true,
	  "Time": {
	    "Start": "2021-12-22T18:44:42.9098249Z",
	    "End": "2021-12-22T18:44:42.9379215Z",
	    "TotalMs": 28.1
	  },
	  "ContinuationTokens": 0,
	  "BlobsEnumerated": 12,
	  "BytesEnumerated": 1371041,
	  "BlobsRead": 12,
	  "BytesRead": 1371041,
	  "BlobsWritten": 12,
	  "BytesWritten": 1371041,
	  "Keys": [
	    "filename.txt",
	    ...
	  ]
	}
 */

Version History

Refer to CHANGELOG.md for version history.

blobhelper's People

Contributors

courtzzz avatar danielharman avatar jchristn avatar phpfui avatar revazashvili avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

blobhelper's Issues

No Blobs returned from Blob.Enumerate() while using DiskStorage

Operating system and version: Windows 10 / .NET Core 3.1.17
Library Version: (NuGet) 2.1.3
Issue encountered: Different Enumeration results (Disk vs S3)
Expected behavior: Listing of objects
Steps to reproduce: Call Blob.Enumerate() while using DiskStorage

Sample code encapsulating the problem:

DiskSettings diskSettings = new DiskSettings("storage");
var BlobClient = new Blobs(diskSettings);
var blobs = await BlobClient.Enumerate();

foreach (var blobMeta in blobs.Blobs)
    Console.WriteLine(blobMeta.Key);

Exception details: N/A

I noticed while using DiskStorage that enumerating over the files returns odd results compared to S3.
For example, when enumerating over a S3 bucket, I get back blobs with keys using the full path without having to provide a prefix.

When I switch to DiskStorage, if I enumerate, I only get back directories, not files.
This is the same regardless if I provide a prefix or not.

I believe the issue is related to the flags passed to Directory.EnumerateFiles.
I also believe the call to Directory.EnumerateDirectories is unneeded as Directory.EnumerateFiles would provide the same information.

In my own testing, using SearchOption.AllDirectories instead returns what I'd expect when enumerating.

I'm opening this as an issue incase I'm misunderstanding the purpose of DiskStorage.

Ref:
https://github.com/jchristn/BlobHelper/blob/9953f09ab6455bf5086f1d5dad8c98cf292d29c5/BlobHelper/Blobs.cs#L1085
https://github.com/jchristn/BlobHelper/blob/9953f09ab6455bf5086f1d5dad8c98cf292d29c5/BlobHelper/Blobs.cs#L1089

Unable to set CannedACL

Hi!

First of thank you for BlobHelper it is delicous of use.

I can't find a way to set permission when write to blob. For exemple:
CannedACL = S3CannedACL.PublicRead

Has a way to do that?

Regards

Sub Directory support question

First of all, this is a great wrapper around blob storage engines! I was going to have to write this from scratch, so I am glad I found this.

I am writing a Storage copy command line utility (happy to add it to this repo once finished if you like). Basically it asks for a source server and a destination server, then iterates through the blobs on the source server and adds them to the destination server. This is great for downloading from a cloud server to a local server for testing.

I pointed the source server to a random local directory with sub directories, and to my surprise, I found it iterated through all the files in all directories. I then tried to add these "blobs" into an empty directory without the corresponding sub directories, and it crashed on the first directory. Looks like the target sub directories are not being created on a write.

So the question is, should it iterate through sub directories on read, or should it create sub directories on write? Or neither?

Totally understand if sub directories are not supported. But in that case, it should probably not enumerate sub directories.

And thanks again for this very nicely designed library.

Add function to create folders

Hi,

first of all, thanks for creating this library.

I didnt find a way of creating empty folders. Is there a possibility of adding this in a future release?

Thanks,

Update to Azure.Storage.Blobs

Would have been nice, to see such a helper for the New Azure.Storage.Blobs Package.
Seems like this Repo uses the old "Microsoft.WindowsAzure.Storage;" package - which is depricated and completly different to handle then the new one.

DiskEnumerate() will never return continuation token

Hi,
Trying to get continuation token back from DiskEnumerate()
Looking at the call of DiskBuildContinuationToken(int start, int count)

The call is made with these parameters:
ret.NextContinuationToken = DiskBuildContinuationToken(startIndex + count, count);

First line in DiskBuildContinuationToken()
if (start >= count) return null;

So looks like only negative values on startIndex on any count can ever return a result that is not null?

Separate Implementations

we can create interface that contains all methods like Blobs.cs and create concrete implementations for each blob provider.

  1. Blobs.cs will use this interface, base on storage type (now there is too many code in this class), we can separate them
  2. we can use this interface to inject in DI container. (as far as i know there is no support for DI Container for now)
public interface IBlobClient
    {
        Task<byte[]> GetAsync(string key, CancellationToken token = default);
        Task<BlobData> GetStreamAsync(string key, CancellationToken token = default);
        Task<BlobMetadata> GetMetadataAsync(string key, CancellationToken token = default);
        Task WriteAsync(string key, string contentType, string data, CancellationToken token = default);
        Task WriteAsync(string key, string contentType, byte[] data, CancellationToken token = default);
        Task WriteAsync(string key, string contentType, long contentLength, Stream stream, CancellationToken token = default);
        Task WriteManyAsync(List<WriteRequest> objects, CancellationToken token = default);
        Task DeleteAsync(string key, CancellationToken token = default);
        public Task<bool> ExistsAsync(string key, CancellationToken token = default);
        string GenerateUrlAsync(string key, CancellationToken token = default);
        Task<EnumerationResult> EnumerateAsync(string prefix = null, string continuationToken = null, CancellationToken token = default);
        Task<EmptyResult> EmptyAsync(CancellationToken token = default);
    }
public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddAzureBlobStorage(this IServiceCollection services,
            AzureSettings azureSettings, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
        {
            var azureBlobClientServiceDescriptor = new ServiceDescriptor(typeof(IBlobClient),
                _ => new AzureBlobClient(azureSettings), serviceLifetime);
            services.Add(azureBlobClientServiceDescriptor);
            return services;
        }
    }

checkout this branch: https://github.com/Revazashvili/BlobHelper/tree/refactor P.S code is just changed, nothing is tested

Could we seek to the begin of the stream in DiskWrite(string, long, Stream, CancellationToken)?

Ciao ragazzi,

I have a case where I can't change the code and my stream is at the end position.

Would you mind seeking to the begin of the stream, e.g.

if (stream.CanSeek && stream.Length == stream.Position)
{ 
    stream.Seek(0, System.IO.SeekOrigin.Begin); 
}

in the method

private async Task DiskWrite(string key, long contentLength, Stream stream, CancellationToken token)

?

Thanks a lot in advance

Best regards,

Florian

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.