Skip to content

Commit

Permalink
Добавил методы ListObjects, ListBuckets
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 c35c4c5 commit 6d89705
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net;
using System.Threading.Tasks;
using AspNetCore.Yandex.ObjectStorage.Models;
using AspNetCore.Yandex.ObjectStorage.Bucket.Requests;
using Bogus;
using Xunit;

Expand Down Expand Up @@ -56,6 +54,48 @@ public async Task DeleteBucket_Success()
Assert.True(deleteResult.IsSuccess);
}

[Fact(DisplayName = "[003] List bucket objects")]
public async Task ListBucketObjects_Success()
{
const string bucketName = "testbucketlib";

var result = await _yandexStorageService.BucketService.GetBucketListObjects(new BucketListObjectsParameters()
{
BucketName = bucketName
});

Assert.True(result.IsSuccessStatusCode);
Assert.Equal(HttpStatusCode.OK, result.StatusCode);

var listObjectsResult = await result.ReadResultAsync();

Assert.True(listObjectsResult.IsSuccess);

var listObjects = listObjectsResult.Value;

Assert.Equal(2,listObjects.Contents.Count);
}


[Fact(DisplayName = "[004] Bucket list")]
public async Task BucketList_Success()
{
var result = await _yandexStorageService.BucketService.GetBucketList();

Assert.True(result.IsSuccessStatusCode);
Assert.Equal(HttpStatusCode.OK, result.StatusCode);

var bucketListResult = await result.ReadResultAsync();

Assert.True(bucketListResult.IsSuccess);

var bucketList = bucketListResult.Value;

Assert.Single(bucketList.Buckets);
Assert.Equal("testbucketlib", bucketList.Buckets[0].Name);
}


private async Task DeleteBucketAsync(string bucketName)
{
await _yandexStorageService.BucketService.CreateBucket(bucketName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ public static YandexStorageOptions GetFromEnvironment()
{
return new YandexStorageOptions
{
BucketName = "testbucketlib",
AccessKey = "YCAJEmcixPRabFaAWHp-k_RA7",
SecretKey = "YCOflzMKw_FF5GjpxX2jMjSz7fgfj7cT_aItejSz"
BucketName = Environment.GetEnvironmentVariable("BucketName"),
AccessKey = Environment.GetEnvironmentVariable("AccessKey"),
SecretKey = Environment.GetEnvironmentVariable("SecretKey")
};
}

public static YandexStorageOptions GetFromEnvironmentWithNotDefaultLocation()
{
return new YandexStorageOptions
{
BucketName = "testbucketlib",
AccessKey = "YCAJEmcixPRabFaAWHp-k_RA7",
SecretKey = "YCOflzMKw_FF5GjpxX2jMjSz7fgfj7cT_aItejSz",
BucketName = Environment.GetEnvironmentVariable("BucketName"),
AccessKey = Environment.GetEnvironmentVariable("AccessKey"),
SecretKey = Environment.GetEnvironmentVariable("SecretKey"),
Location = "ru-central1",
Endpoint = "s3.yandexcloud.net"
};
Expand Down
31 changes: 22 additions & 9 deletions AspNetCore.Yandex.ObjectStorage/Bucket/BucketService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using AspNetCore.Yandex.ObjectStorage.Bucket.Builders;
using AspNetCore.Yandex.ObjectStorage.Bucket.Models;
using AspNetCore.Yandex.ObjectStorage.Bucket.Requests;
using AspNetCore.Yandex.ObjectStorage.Bucket.Responses;
using AspNetCore.Yandex.ObjectStorage.Configuration;
using AspNetCore.Yandex.ObjectStorage.Enums;
using AspNetCore.Yandex.ObjectStorage.Models;
Expand All @@ -26,11 +30,7 @@ public BucketService(YandexStorageOptions options)
public async Task<S3PutResponse> CreateBucket(string bucketName)
{
var builder = new BucketPutRequestBuilder(_options);
var createModel = new BucketCreateOptions
{
BucketName = bucketName
};
var requestMessage = builder.Build(createModel).GetResult();
var requestMessage = builder.Build(bucketName).GetResult();

using var client = new HttpClient();
var response = await client.SendAsync(requestMessage);
Expand All @@ -43,14 +43,27 @@ public Task<S3GetResponse> GetBucketMeta(string bucketName)
throw new NotImplementedException();
}

public Task<S3GetResponse> GetBucketListObjects(string bucketName)
public async Task<S3BucketObjectListResponse> GetBucketListObjects(BucketListObjectsParameters parameters)
{
throw new NotImplementedException();
var builder = new BucketListObjectsRequestBuilder(_options);
var requestMessage = builder.Build(parameters).GetResult();

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

return new S3BucketObjectListResponse(response);
}

public Task<S3GetResponse> GetBucketList()
public async Task<S3BucketListResponse> GetBucketList()
{
throw new NotImplementedException();
var requestMessage = new BucketListRequestBuilder(_options)
.Build()
.GetResult();

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

return new S3BucketListResponse(response);
}

public async Task<S3DeleteResponse> DeleteBucket(string bucketName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using AspNetCore.Yandex.ObjectStorage.Configuration;
using AspNetCore.Yandex.ObjectStorage.Helpers;

namespace AspNetCore.Yandex.ObjectStorage.Bucket
namespace AspNetCore.Yandex.ObjectStorage.Bucket.Builders
{
internal class BucketDeleteRequestBuilder
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 BucketListObjectsRequestBuilder
{
private readonly YandexStorageOptions _options;
private HttpRequestMessage _request;

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

internal BucketListObjectsRequestBuilder Build(BucketListObjectsParameters parameters)
{
var url = $"{_options.Protocol}://{_options.Endpoint}/{parameters.BucketName}?{FormatParameters(parameters)}";

var requestMessage = new HttpRequestMessage(HttpMethod.Get, 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;
}

private static string FormatParameters(BucketListObjectsParameters parameters)
{
var continueToken = string.IsNullOrEmpty(parameters.ContinueToken)
? string.Empty
: $"&continuation-token={parameters.ContinueToken}";
var delimiter = string.IsNullOrEmpty(parameters.Delimiter)
? string.Empty
: $"&delimiter={parameters.Delimiter}";
var maxKeys = $"&max-keys={parameters.MaxKeys}";
var prefix = string.IsNullOrEmpty(parameters.Prefix)
? string.Empty
: $"&prefix={parameters.Prefix}";
var startAfter = string.IsNullOrEmpty(parameters.StartAfter)
? string.Empty
: $"&start-after={parameters.StartAfter}";

return string.Concat("list-type=2", continueToken, delimiter, maxKeys, prefix, startAfter);
}
}
}
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 BucketListRequestBuilder
{
private readonly YandexStorageOptions _options;
private HttpRequestMessage _request;

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

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

var requestMessage = new HttpRequestMessage(HttpMethod.Get, 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using AspNetCore.Yandex.ObjectStorage.Configuration;
using AspNetCore.Yandex.ObjectStorage.Helpers;

namespace AspNetCore.Yandex.ObjectStorage.Bucket
namespace AspNetCore.Yandex.ObjectStorage.Bucket.Builders
{
internal class BucketPutRequestBuilder
{
Expand All @@ -15,9 +15,9 @@ internal BucketPutRequestBuilder(YandexStorageOptions options)
_options = options;
}

internal BucketPutRequestBuilder Build(BucketCreateOptions createModel)
internal BucketPutRequestBuilder Build(string bucketName)
{
var requestMessage = new HttpRequestMessage(HttpMethod.Put, new Uri($"{_options.Protocol}://{_options.Endpoint}/{createModel.BucketName}"));
var requestMessage = new HttpRequestMessage(HttpMethod.Put, new Uri($"{_options.Protocol}://{_options.Endpoint}/{bucketName}"));
var dateAmz = DateTime.UtcNow;

requestMessage.AddBothHeaders(_options, dateAmz);
Expand Down
6 changes: 4 additions & 2 deletions AspNetCore.Yandex.ObjectStorage/Bucket/IBucketService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using AspNetCore.Yandex.ObjectStorage.Bucket.Requests;
using AspNetCore.Yandex.ObjectStorage.Bucket.Responses;
using AspNetCore.Yandex.ObjectStorage.Models;

namespace AspNetCore.Yandex.ObjectStorage.Bucket
Expand All @@ -9,9 +11,9 @@ public interface IBucketService

Task<S3GetResponse> GetBucketMeta(string bucketName);

Task<S3GetResponse> GetBucketListObjects(string bucketName);
Task<S3BucketObjectListResponse> GetBucketListObjects(BucketListObjectsParameters parameters);

Task<S3GetResponse> GetBucketList();
Task<S3BucketListResponse> GetBucketList();

Task<S3DeleteResponse> DeleteBucket(string bucketName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
namespace AspNetCore.Yandex.ObjectStorage.Bucket.Models
using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace AspNetCore.Yandex.ObjectStorage.Bucket.Models
{
/// <summary>
/// https://cloud.yandex.ru/docs/storage/s3/api-ref/bucket/list#response-scheme
/// </summary>
[XmlRoot(ElementName="ListAllMyBucketsResult", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public class BucketListResult
{
[XmlArray("Buckets", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
[XmlArrayItem("Bucket", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public List<Bucket> Buckets { get; set; }
}

[XmlRoot(ElementName="Bucket", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public class Bucket
{
[XmlElement(ElementName="Name", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public string Name { get; set; }
[XmlElement(ElementName="CreationDate", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public string CreationDate { get; set; }
}

}
27 changes: 21 additions & 6 deletions AspNetCore.Yandex.ObjectStorage/Bucket/Models/ObjectListResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace AspNetCore.Yandex.ObjectStorage.Bucket.Models
/// <summary>
/// https://cloud.yandex.ru/docs/storage/s3/api-ref/bucket/listobjects#structureV2
/// </summary>
[XmlRoot(ElementName="ListBucketResult", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public class ObjectListResult
{
/// <summary>
Expand All @@ -18,7 +19,7 @@ public class ObjectListResult
/// <summary>
/// Описание объекта
/// </summary>
[XmlElement(ElementName="Contents")]
[XmlElement(ElementName="Contents", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public List<Contents> Contents { get; set; }

/// <summary>
Expand Down Expand Up @@ -78,7 +79,14 @@ public class ObjectListResult
public string StartAfter { get; set; }
}

[XmlRoot(ElementName="Contents")]

[XmlRoot(ElementName="CommonPrefixes")]
public class CommonPrefixes
{
public string Prefix { get; set; }
}

[XmlRoot(ElementName="Contents", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public class Contents
{
/// <summary>
Expand All @@ -88,7 +96,8 @@ public class Contents
/// <summary>
/// Ключ объекта.
/// </summary>
public string Key { get; set; }
[XmlElement(ElementName="Key")]
public string Filename { get; set; }
/// <summary>
/// Дата и время последнего изменения объекта.
/// </summary>
Expand All @@ -101,10 +110,16 @@ public class Contents
/// Класс хранения объекта: STANDARD или COLD.
/// </summary>
public string StorageClass { get; set; }

public Owner Owner { get; set; }
}

[XmlRoot(ElementName="CommonPrefixes")]
public class CommonPrefixes {
public string Prefix { get; set; }
[XmlRoot(ElementName="Owner", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public class Owner
{
[XmlElement(ElementName="ID", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public string Id { get; set; }
[XmlElement(ElementName="DisplayName", Namespace="http://s3.amazonaws.com/doc/2006-03-01/")]
public string DisplayName { get; set; }
}
}
Loading

0 comments on commit 6d89705

Please sign in to comment.