A netstandard class Library that allows programmers to use 1 interface for all different cloud service's storage bucket service. It has simplified functionalities, which includes:
- Bucket CRUD
- Blob CRUD
- Bucket level ReadAccess control
Useful for CLI for deployment, or multiple-blob storage projec that uses same interface.
Install via .NET CLI
$ dotnet add package Kirinnee.StorageBucketClient
or
Install via NuGet Package Manager
PM> Install-Package Kirinnee.StorageBucketClient
The only difference between using each service is the initial creation of the client. Using each services' own authentication method, you can create the same client, which are used the same later.
//Create a credential
string accessId = "someID";
string accessSecret = "supersecret!";
string region = "us-east-1";
ICredential awsCred = new AWSCredential(accessId, accessSecret, region);
//Use the BucketClientFactory to create a bucket Client
IBucketClient bucketClient = BucketClientFactory.CreateClient(CloudServiceProvider.AWS, awsCred);
//Create a credential object
string accessId = "someID";
string accessSecret = "supersecret!";
string region = "nyc3";
ICredential digitalOceanCred = new DigitalOceanCredential(accessId, accessSecret, region);
//Use the BucketClientFactory to create a bucket Client
IBucketClient bucketClient = BucketClientFactory.CreateClient(CloudServiceProvider.DigitalOcean, digitalOceanCred);
//Create a credential object
string accountName = "storage-account-name";
string accessToken = "supersecret!";
ICredential azureCred = new AzureCredential(accountName, accessToken);
//Use the BucketClientFactory to create a bucket Client
IBucketClient bucketClient = BucketClientFactory.CreateClient(CloudServiceProvider.Azure,azureCred);
//Create a credential object with your secrets
string projectID = "project-id-123456";
string jsonSecret = "{\"secret\":\"super-secret\"}";//download the json secret and pass the json as a string
ICredential googleCred = new GCPCredential(projectID, jsonSecret);
//Use the BucketClientFactory to create a bucket Client
IBucketClient bucketClient = BucketClientFactory.CreateClient(CloudServiceProvider.GCP,googleCred);
Most methods provided in this library returns an Operation Result, which contains 4 values that is used to inform you about the reqest.
Property | Type | Description |
---|---|---|
Success | boolean | Whether the operation is successful or not |
Message | string | Response message |
Code | HttpStatusCode | The Real HttpStatusCode recieved |
Usable | Uri | Usable link to the blob |
Creates a bucket
IBucketClient client = _bucketClient;
OperationResult result = await client.CreateBucket("bucket-name");
Deletes a bucket
IBucketClient client = _bucketClient;
OperationResult result = await client.Delete("bucket-name");
Makes the bucket and all its blob readable by everyone
IBucketClient client = _bucketClient;
OperationResult result = await client.SetReadPolicy("bucket-name", ReadAccess.Public);
Makes the bucket and all its blob readable only by owner (with the credential)
IBucketClient client = _bucketClient;
OperationResult result = await client.SetReadPolicy("bucket-name", ReadAccess.Private);
IBucketClient client = _bucketClient;
OperationResult result = await client.SetGETCors("bucket-name", new string[]{"*"}});
Checks if the bucket exist
IBucketClient client = _bucketClient;
bool exist = await client.ExistBucket("bucket-name");
Gets a bucket object, which can be used to perform CRUD operation on the objects it contains. The object will be IBucket
IBucket bucket = await client.GetBucket("bucket-name");
Returns the URI for user to use. Will not fail if the file does not exist.
IBucket bucket = somebucket;
//Get the uri for image.png within the bucket
Uri uri = await bucket.GetUri("image.png");
Creates a blob in the bucket. MimeType will be automatically detected.
Will fail if blob already exist.
IBucket bucket = somebucket;
byte[] bytePayload = someBytes;
FileStream streamPayload = someStream;
//Create a blob using byte array
OperationResult result1 = await bucket.CreateBlob(bytePayload, "image.png");
//Create a blob using Stream
OperationResult result2 = await bucket.CreateBlob(streamPayload, "image.png");
Updates an exist blob in the bucket. MimeType will be automatically updated.
Will fail if blob does not exist
IBucket bucket = somebucket;
byte[] bytePayload = someBytes;
FileStream streamPayload = someStream;
//Update the blob using byte array with bucket specific id
OperationResult result1 = await bucket.CreateBlob(bytePayload, "image.png");
//Update the blob using Stream with bucket specific id
OperationResult result2 = await bucket.CreateBlob(streamPayload, "image.png");
//Update the blob using byte array with full blob URI
OperationResult result3 = await bucket.CreateBlob(bytePayload, "https://aws.com/somebucket/image.png");
//Update the blob using byte array with full blob URI
OperationResult result4 = await bucket.CreateBlob(streamPayload, "https://aws.com/somebucket/image.png");
This will replace the current space with the nwe blob, whether it exist or not.
MimeType will be automatically set.
IBucket bucket = somebucket;
byte[] bytePayload = someBytes;
FileStream streamPayload = someStream;
//Update the blob using byte array with bucket specific id
OperationResult result1 = await bucket.PutBlob(bytePayload, "image.png");
//Update the blob using Stream with bucket specific id
OperationResult result2 = await bucket.PutBlob(streamPayload, "image.png");
//Update the blob using byte array with full blob URI
OperationResult result3 = await bucket.PutBlob(bytePayload, "https://aws.com/bucket/img.png");
//Update the blob using byte array with full blob URI
OperationResult result4 = await bucket.PutBlob(streamPayload, "https://aws.com/bucket/img.png");
IBucket bucket = somebucket;
//Check if the blob exist using bucket specific id
bool exist1 = await bucket.ExistBlob("image.png");
//Check if the blob exist using blob URI
bool exist2 = await bucket.ExistBlob("https://aws.com/bucket/img.png");
Do note this throws error when it fails, it does not check or return a OperationResult
IBucket bucket = somebucket;
//Downloads blob using specific id
byte[] blob1 = await bucket.GetBlob("image.png");
//Downloads blob using full URI
byte[] blob2 = await bucket.GetBlob("https://aws.com/bucket/img.png");
Do note that this throws exception when it fails. It does not check nor return a OperationResult object.
IBucketClient client = _bucketClient;
//Download using full URI
byte[] blob = await client.GetBlob("https://aws.com/bucket/img.png");
Updates an exist blob in the bucket. MimeType will be automatically updated too.
Will fail if blob does not exist
IBucketClient client = _bucketClient;
byte[] bytePayload = someBytes;
FileStream streamPayload = someStream;
//Update the blob using byte array with the full blob URI
OperationResult result3 = await client.CreateBlob(bytePayload, "https://aws.com/bucket/img.png");
//Update the blob using byte array with the full blob URI
OperationResult result4 = await client.CreateBlob(streamPayload, "https://aws.com/bucket/img.png");
This will replace the current space with the nwe blob, whether it exist or not.
MimeType will be automatically set.
IBucketClient client = _bucketClient;
byte[] bytePayload = someBytes;
FileStream streamPayload = someStream;
//Update the blob using byte array with full blob URI
OperationResult result3 = await client.PutBlob(bytePayload, "https://aws.com/bucket/img.png");
//Update the blob using byte array with full blob URI
OperationResult result4 = await client.PutBlob(streamPayload, "https://aws.com/bucket/img.png");
IBucketClient client = _bucketClient;
//Check if the blob exist using full blob URI
bool exist2 = await client.ExistBlob("https://aws.com/bucket/img.png");
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
This project is licensed under MIT - see the LICENSE.md file for details