Skip to content

Commit

Permalink
Bucket method implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Shumkin authored and Alexander Shumkin committed Jul 29, 2022
1 parent 5574b79 commit d9459e8
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Threading.Tasks;
using AspNetCore.Yandex.ObjectStorage.Models;
using AspNetCore.Yandex.ObjectStorage.Object.Models;
using Bogus;
using Xunit;

Expand Down Expand Up @@ -163,7 +164,7 @@ public async Task PutObject_NotDefaultLocation_Success()

#region Private

private async Task<S3GetResponse> TryGetObjectAsync(string filename)
private async Task<S3ObjectGetResponse> TryGetObjectAsync(string filename)
{
return await _yandexStorageService.TryGetAsync(filename);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<RepositoryUrl>https://github.com/DubZero/AspNetCore.Yandex.ObjectStorage</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>S3, Yandex, Object Storage</PackageTags>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageReleaseNotes>Impliment core bucket service functions</PackageReleaseNotes>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<SignAssembly>false</SignAssembly>
<PackageIconUrl>https://yastatic.net/q/cloud-www/build/ru/assets/img/storage.15691e8c.png</PackageIconUrl>
<AssemblyVersion>0.2.0.1</AssemblyVersion>
<FileVersion>0.2.0.1</FileVersion>
<AssemblyVersion>0.2.1</AssemblyVersion>
<FileVersion>0.2.1</FileVersion>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<PackageVersion>0.2.0.1-alpha001</PackageVersion>
<PackageVersion>0.2.1.0</PackageVersion>
<TargetFramework>netstandard2.1</TargetFramework>
<Title>Yandex.ObjectStorage S3 API Client Library</Title>
</PropertyGroup>
Expand Down
20 changes: 14 additions & 6 deletions AspNetCore.Yandex.ObjectStorage/Bucket/BucketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using AspNetCore.Yandex.ObjectStorage.Configuration;
using AspNetCore.Yandex.ObjectStorage.Enums;
using AspNetCore.Yandex.ObjectStorage.Models;
using AspNetCore.Yandex.ObjectStorage.Object.Models;
using Microsoft.Extensions.Options;

namespace AspNetCore.Yandex.ObjectStorage.Bucket
Expand All @@ -27,20 +28,27 @@ public BucketService(YandexStorageOptions options)
_options = options;
}

public async Task<S3PutResponse> CreateBucket(string bucketName)
public async Task<S3ObjectPutResponse> CreateBucket(string bucketName)
{
var builder = new BucketPutRequestBuilder(_options);
var requestMessage = builder.Build(bucketName).GetResult();

using var client = new HttpClient();
var response = await client.SendAsync(requestMessage);

return new S3PutResponse(response, GetBucketUri(bucketName));
return new S3ObjectPutResponse(response, GetBucketUri(bucketName));
}

public Task<S3GetResponse> GetBucketMeta(string bucketName)
public async Task<S3Response> GetBucketMeta(string bucketName)
{
throw new NotImplementedException();
var requestMessage = new BucketMetaRequestBuilder(_options)
.Build(bucketName)
.GetResult();

using var client = new HttpClient();
var response = await client.SendAsync(requestMessage);

return new S3Response(response);
}

public async Task<S3BucketObjectListResponse> GetBucketListObjects(BucketListObjectsParameters parameters)
Expand All @@ -66,15 +74,15 @@ public async Task<S3BucketListResponse> GetBucketList()
return new S3BucketListResponse(response);
}

public async Task<S3DeleteResponse> DeleteBucket(string bucketName)
public async Task<S3ObjectDeleteResponse> DeleteBucket(string bucketName)
{
var builder = new BucketDeleteRequestBuilder(_options);
var requestMessage = builder.Build(bucketName).GetResult();

using var client = new HttpClient();
var response = await client.SendAsync(requestMessage);

return new S3DeleteResponse(response);
return new S3ObjectDeleteResponse(response);
}

private string GetBucketUri(string bucket)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Net.Http;
using AspNetCore.Yandex.ObjectStorage.Bucket.Requests;
using AspNetCore.Yandex.ObjectStorage.Configuration;
using AspNetCore.Yandex.ObjectStorage.Helpers;

namespace AspNetCore.Yandex.ObjectStorage.Bucket.Builders
{
internal class BucketMetaRequestBuilder
{
private readonly YandexStorageOptions _options;
private HttpRequestMessage _request;

internal BucketMetaRequestBuilder(YandexStorageOptions options)
{
_options = options;
}

internal BucketMetaRequestBuilder Build(string bucketName)
{
var url = $"{_options.Protocol}://{_options.Endpoint}/{bucketName}";

var requestMessage = new HttpRequestMessage(HttpMethod.Head, new Uri(url));
var dateAmz = DateTime.UtcNow;

requestMessage.AddBothHeaders(_options, dateAmz);

string[] headers = { "host", "x-amz-content-sha256", "x-amz-date" };
requestMessage.AddAuthHeader(_options, dateAmz, headers);
_request = requestMessage;

return this;
}

internal HttpRequestMessage GetResult()
{
return _request;
}
}
}
20 changes: 17 additions & 3 deletions AspNetCore.Yandex.ObjectStorage/Bucket/IBucketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@
using AspNetCore.Yandex.ObjectStorage.Bucket.Requests;
using AspNetCore.Yandex.ObjectStorage.Bucket.Responses;
using AspNetCore.Yandex.ObjectStorage.Models;
using AspNetCore.Yandex.ObjectStorage.Object.Models;

namespace AspNetCore.Yandex.ObjectStorage.Bucket
{
public interface IBucketService
{
Task<S3PutResponse> CreateBucket(string bucketName);
Task<S3ObjectPutResponse> CreateBucket(string bucketName);

Task<S3GetResponse> GetBucketMeta(string bucketName);
/// <summary>
/// Returns the bucket's metadata or an error.
/// Use this method to check:
/// 1. Whether the bucket exists.
/// 2. Whether the user has sufficient permissions to access the bucket
/// </summary>
Task<S3Response> GetBucketMeta(string bucketName);


/// <summary>
/// Get list of objects in bucket
/// </summary>
Task<S3BucketObjectListResponse> GetBucketListObjects(BucketListObjectsParameters parameters);

/// <summary>
/// Get list of buckets
/// </summary>
Task<S3BucketListResponse> GetBucketList();

Task<S3DeleteResponse> DeleteBucket(string bucketName);
Task<S3ObjectDeleteResponse> DeleteBucket(string bucketName);
}
}
5 changes: 3 additions & 2 deletions AspNetCore.Yandex.ObjectStorage/IYandexStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AspNetCore.Yandex.ObjectStorage.Bucket;
using AspNetCore.Yandex.ObjectStorage.Models;
using AspNetCore.Yandex.ObjectStorage.Object;
using AspNetCore.Yandex.ObjectStorage.Object.Models;

namespace AspNetCore.Yandex.ObjectStorage
{
Expand All @@ -19,11 +20,11 @@ public interface IYandexStorageService
/// Test connection to storage
/// </summary>
/// <returns>Retruns true if all credentials correct</returns>
Task<S3GetResponse> TryConnectAsync();
Task<S3ObjectGetResponse> TryConnectAsync();
/// <summary>
/// Test connection to storage
/// </summary>
/// <returns>Retruns true if all credentials correct</returns>
Task<S3GetResponse> TryGetAsync(string filename);
Task<S3ObjectGetResponse> TryGetAsync(string filename);
}
}
23 changes: 23 additions & 0 deletions AspNetCore.Yandex.ObjectStorage/Models/S3Response.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Net.Http;
using System.Threading.Tasks;
using FluentResults;

namespace AspNetCore.Yandex.ObjectStorage.Models
{
public class S3Response : BaseS3Response
{
public S3Response(HttpResponseMessage response) : base(response)
{
}

public async Task<Result<string>> ReadResultAsStringAsync()
{
if (IsSuccessStatusCode)
{
return await Response.Content.ReadAsStringAsync();
}

return Result.Fail(await Response.Content.ReadAsStringAsync());
}
}
}
9 changes: 5 additions & 4 deletions AspNetCore.Yandex.ObjectStorage/Object/IObjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.IO;
using System.Threading.Tasks;
using AspNetCore.Yandex.ObjectStorage.Models;
using AspNetCore.Yandex.ObjectStorage.Object.Models;
using FluentResults;

namespace AspNetCore.Yandex.ObjectStorage.Object
{
public interface IObjectService
{
Task<S3GetResponse> GetAsync(string filename);
Task<S3ObjectGetResponse> GetAsync(string filename);

Task<Result<byte[]>> GetAsByteArrayAsync(string filename);

Expand All @@ -25,10 +26,10 @@ public interface IObjectService
/// </summary>
/// <param name="stream">Put ob</param>
/// <param name="filename"></param>
Task<S3PutResponse> PutAsync(Stream stream, string filename);
Task<S3ObjectPutResponse> PutAsync(Stream stream, string filename);

Task<S3PutResponse> PutAsync(byte[] byteArr, string filename);
Task<S3ObjectPutResponse> PutAsync(byte[] byteArr, string filename);

Task<S3DeleteResponse> DeleteAsync(string filename);
Task<S3ObjectDeleteResponse> DeleteAsync(string filename);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Net.Http;
using System.Threading.Tasks;
using AspNetCore.Yandex.ObjectStorage.Models;
using FluentResults;

namespace AspNetCore.Yandex.ObjectStorage.Models
namespace AspNetCore.Yandex.ObjectStorage.Object.Models
{
public class S3DeleteResponse : BaseS3Response
public class S3ObjectDeleteResponse : BaseS3Response
{

public S3DeleteResponse(HttpResponseMessage response) : base(response)
public S3ObjectDeleteResponse(HttpResponseMessage response) : base(response)
{

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using AspNetCore.Yandex.ObjectStorage.Models;
using FluentResults;

namespace AspNetCore.Yandex.ObjectStorage.Models
namespace AspNetCore.Yandex.ObjectStorage.Object.Models
{
public class S3GetResponse : BaseS3Response
public class S3ObjectGetResponse : BaseS3Response
{
public S3GetResponse(HttpResponseMessage response) : base(response)
public S3ObjectGetResponse(HttpResponseMessage response) : base(response)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Net.Http;
using System.Threading.Tasks;
using AspNetCore.Yandex.ObjectStorage.Models;
using FluentResults;

namespace AspNetCore.Yandex.ObjectStorage.Models
namespace AspNetCore.Yandex.ObjectStorage.Object.Models
{
public class S3PutResponse : BaseS3Response
public class S3ObjectPutResponse : BaseS3Response
{
private readonly string _url;

public S3PutResponse(HttpResponseMessage response, string url) : base(response)
public S3ObjectPutResponse(HttpResponseMessage response, string url) : base(response)
{
_url = url;
}
Expand Down
21 changes: 11 additions & 10 deletions AspNetCore.Yandex.ObjectStorage/Object/ObjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using AspNetCore.Yandex.ObjectStorage.Configuration;
using AspNetCore.Yandex.ObjectStorage.Helpers;
using AspNetCore.Yandex.ObjectStorage.Models;
using AspNetCore.Yandex.ObjectStorage.Object.Models;
using FluentResults;
using Microsoft.Extensions.Options;

Expand Down Expand Up @@ -45,13 +46,13 @@ public ObjectService(YandexStorageOptions options)
_hostName = options.HostName;
}

public async Task<S3GetResponse> GetAsync(string filename)
public async Task<S3ObjectGetResponse> GetAsync(string filename)
{
var formattedPath = FormatPath(filename);

var requestMessage = PrepareGetRequest(formattedPath);

var response = new S3GetResponse(await _client.SendAsync(requestMessage));
var response = new S3ObjectGetResponse(await _client.SendAsync(requestMessage));

return response;
}
Expand All @@ -67,7 +68,7 @@ public async Task<Result<byte[]>> GetAsByteArrayAsync(string filename)

var requestMessage = PrepareGetRequest(formattedPath);

var response = new S3GetResponse(await _client.SendAsync(requestMessage));
var response = new S3ObjectGetResponse(await _client.SendAsync(requestMessage));

return await response.ReadAsByteArrayAsync().ConfigureAwait(false);
}
Expand All @@ -82,42 +83,42 @@ public async Task<Result<Stream>> GetAsStreamAsync(string filename)
var formattedPath = FormatPath(filename);
var requestMessage = PrepareGetRequest(formattedPath);

var response = new S3GetResponse(await _client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead));
var response = new S3ObjectGetResponse(await _client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead));

return await response.ReadAsStreamAsync().ConfigureAwait(false);
}

public async Task<S3PutResponse> PutAsync(Stream stream, string filename)
public async Task<S3ObjectPutResponse> PutAsync(Stream stream, string filename)
{
var formattedPath = FormatPath(filename);

var requestMessage = PreparePutRequest(stream, formattedPath);

var response = await _client.SendAsync(requestMessage);

return new S3PutResponse(response, GetObjectUri(formattedPath));
return new S3ObjectPutResponse(response, GetObjectUri(formattedPath));
}

public async Task<S3PutResponse> PutAsync(byte[] byteArr, string filename)
public async Task<S3ObjectPutResponse> PutAsync(byte[] byteArr, string filename)
{
var formattedPath = FormatPath(filename);

var requestMessage = PreparePutRequest(byteArr, formattedPath);

var response = await _client.SendAsync(requestMessage);

return new S3PutResponse(response, GetObjectUri(formattedPath));
return new S3ObjectPutResponse(response, GetObjectUri(formattedPath));
}

public async Task<S3DeleteResponse> DeleteAsync(string filename)
public async Task<S3ObjectDeleteResponse> DeleteAsync(string filename)
{
var formattedPath = FormatPath(filename);

var requestMessage = PrepareDeleteRequest(formattedPath);

var response = await _client.SendAsync(requestMessage);

return new S3DeleteResponse(response);
return new S3ObjectDeleteResponse(response);
}

private HttpRequestMessage PrepareGetRequest()
Expand Down
Loading

0 comments on commit d9459e8

Please sign in to comment.