Giter Site home page Giter Site logo

homely.storage.blobs's Introduction

Homely - Storage Blobs library.

This library contains some helpers when working with Blob Storage.
A common pattern when working with blobs is to serialize/deserialize complex objects to be stored in a blob. This library helps simplify this process - both ways. The content of the blob needs to be a string, so any value is converted either into a string representation (when a 'simple' type) -or- a string-JSON representation (when a 'complex' type). It can also handle byte arrays and streams, untouched.

What's a Simple / Complex Type?

  • Simple: a .NET Primitive or string or decimal
  • Complex: everything else. Usually a custom class.

Azure Storage only?

The library is (currently) only targetting Azure Storage Blobs. Why not other blobs like AWS? Because:

  • We (@Homely) don't work with AWS.
  • There are docker images for Azure Storage (for localhost development) - so we don't need other implementations @Homely.
  • We will accept Pull Requeust for other platforms, though ❤️

Samples

Simple example.

// Create an instance of the Azure blob container.
var blob = new AzureBlob("some-connectionstring", "users", logger);

// Create a fake user.
var user = new User { Name = "Jane" };

// Add this user instance to the blob container. This will
// create a random Guid (as the blob identifier).
var blobId = await blob.AddAsync(user, cancellationToken);

// Retrieve the user.
var existingUser = await blob.GetAsync<User>(blobId, cancellationToken);

// Delete the user.
await blob.DeleteAsync(blobId);

Methods

  • AddAsync : this will accept any object, Byte array, Stream or Uri. If it is a simple object (like a string or int) it will just a insert that value as is into the blob. Otherwise, it will serialize the object to JSON if it's not a Byte/Stream/Uri.

e.g.

var newBlobId = await blob.AddAsync(1);            // int '1' pushed as a string "1" => `application/text`
var newBlobId = await blob.AddAsync("hi there!");  // string 'Hi there' pushed. => `application/text`
var newBlobId = await blob.AddAsync(new Foo());    // serialized to JSON. => `application/json`
var newBlobId = await blob.AddAsync(myListOfFoos); // serialized to JSON. => `application/json`
var newBlobId = await blob.AddAsync(myStream);     // Stream converted to a binary/byte array content and then pushed up => `application/octec-stream`
var newBlobId = await blob.AddAsync(myBytes);      // Binary/byte array content then pushed up => `application/octec-stream`

You can also specify the Id of the blob you wish to store in the container:

e.g.

await blob.AddAsync(new Foo(), "user-1");

Also, you can add a collection of items in one simple call:

  • AddBatchAsync : adds all the items in the collection, a batch at a time. Default batch size is 25 items. Returns all the blobId's for the created blobs.

e.g.

var foos = GetFoosFromSomewhere(); // Returns 100 foo's.
var blobIds = blob.AddBatchAsync(foos, 10); // Adds 100 foo's in batches of 10.
  • GetAsync : this will retrieve a stream of the unknown object type. Then do as you please with it.
  • GetAsync<Foo> : this will retrieve the item from the blob then attempt to deserialize the message into that complex object.

e.g.

// No 'Type' provided. Assumption: message content is not json and will therefore not be deserialized.
var myMessage = await blob.GeteAsync("some-blob-id"); // Stream to unknown blob type.

// Type is provided.
var myFoo = await blob.GetAsync<Foo>("some-foo-Id");  // A foo instance populated with it's respective blob information.
  • DeleteAsync : removes the blob from the container.

e.g.

blob.DeleteAsync("some-blob-id");

Contributing

Discussions and pull requests are encouraged :) Please ask all general questions in this repo or pick a specialized repo for specific, targetted issues. We also have a contributing document which goes into detail about how to do this.

Code of Conduct

Yep, we also have a code of conduct which applies to all repositories in the (GitHub) Homely organisation.

Feedback

Yep, refer to the contributing page about how best to give feedback - either good or needs-improvement :)


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.