Skip to content

Commit

Permalink
refactor: removed '/' from relative path ensure base addres end with '/'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Miranda authored and aloneguid committed Feb 23, 2023
1 parent adc5749 commit 26490d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/Stowage.Test/Impl/AzureBlobTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public AzureBlobTest()
_storage = (IAzureBlobFileStorage)Files.Of.AzureBlobStorage(
settings.AzureStorageAccount, settings.AzureStorageKey, settings.AzureContainerName);

//_storage = (IAzureBlobFileStorage)Files.Of.AzureBlobStorageWithLocalEmulator(settings.AzureContainerName);

}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/Stowage/Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static IFileStorage AzureBlobStorageWithLocalEmulator(this IFilesFactory
const string accountName = "devstoreaccount1";
const string sharedKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
var endpoint = new Uri("http://127.0.0.1:10000/devstoreaccount1");
return new AzureBlobFileStorage(endpoint, $"devstoreaccount1/{containerName}", new SharedKeyAuthHandler(accountName, sharedKey));
return new AzureBlobFileStorage(endpoint, containerName, new SharedKeyAuthHandler(accountName, sharedKey));
}

/*public static IFileStorage AzureTableStorage(
Expand Down
25 changes: 15 additions & 10 deletions src/Stowage/Impl/Microsoft/AzureBlobFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ sealed class AzureBlobFileStorage : PolyfilledHttpFileStorage, IAzureBlobFileSto
private readonly string _containerName;

public AzureBlobFileStorage(string accountName, string containerName, DelegatingHandler authHandler)
: base(new Uri($"https://{accountName}.blob.core.windows.net"), authHandler)
: base(new Uri($"https://{accountName}.blob.core.windows.net/"), authHandler)
{
if(string.IsNullOrEmpty(accountName))
throw new ArgumentException($"'{nameof(accountName)}' cannot be null or empty", nameof(accountName));
Expand All @@ -34,7 +34,7 @@ public AzureBlobFileStorage(string accountName, string containerName, Delegating
}

public AzureBlobFileStorage(Uri endpoint, string containerName, DelegatingHandler authHandler)
: base(endpoint, authHandler)
: base(EnsureUriEndsWithSlash(endpoint), authHandler)
{
if(string.IsNullOrEmpty(containerName))
throw new ArgumentException($"'{nameof(containerName)}' cannot be null or empty", nameof(containerName));
Expand All @@ -44,6 +44,11 @@ public AzureBlobFileStorage(Uri endpoint, string containerName, DelegatingHandle
_containerName = containerName;
}

private static Uri EnsureUriEndsWithSlash(Uri endpoint)
=> endpoint.OriginalString.EndsWith(IOPath.PathSeparator)
? endpoint
: new Uri($"{endpoint.OriginalString}{IOPath.PathSeparator}");

public override Task<IReadOnlyCollection<IOEntry>> Ls(IOPath path, bool recurse = false, CancellationToken cancellationToken = default)
{
if(path != null && !path.IsFolder)
Expand Down Expand Up @@ -101,7 +106,7 @@ private async Task<string> ListAsync(string containerName,
string delimiter = null,
string include = "metadata")
{
string url = $"/{containerName}?restype=container&comp=list&include={include}";
string url = $"{containerName}?restype=container&comp=list&include={include}";

if(prefix != null)
url += "&prefix=" + prefix;
Expand All @@ -128,7 +133,7 @@ private async Task<IReadOnlyCollection<IOEntry>> ListAsync(
IOPath.IsRoot(prefix) ? null : (prefix.Trim('/') + "/"),
delimiter: recurse ? null : "/");

XElement x = XElement.Parse(rawXml);
var x = XElement.Parse(rawXml);
XElement blobs = x.Element("Blobs");
if(blobs != null)
{
Expand Down Expand Up @@ -169,15 +174,15 @@ private HttpRequestMessage CreatePutBlockRequest(int blockId, string containerNa
blockIdStr = Convert.ToBase64String(Encoding.ASCII.GetBytes(blockId.ToString()));

// call https://docs.microsoft.com/en-us/rest/api/storageservices/put-block
var request = new HttpRequestMessage(HttpMethod.Put, $"/{containerName}/{blobName}?comp=block&blockid={blockIdStr}");
var request = new HttpRequestMessage(HttpMethod.Put, $"{containerName}/{blobName}?comp=block&blockid={blockIdStr}");
request.Content = new ByteArrayContent(buffer, 0, count);
return request;
}

private HttpRequestMessage CreateAppendBlockRequest(string containerName, string blobName, byte[] buffer, int count)
{
// call https://docs.microsoft.com/en-us/rest/api/storageservices/append-block
var request = new HttpRequestMessage(HttpMethod.Put, $"/{containerName}/{blobName}?comp=appendblock");
var request = new HttpRequestMessage(HttpMethod.Put, $"{containerName}/{blobName}?comp=appendblock");
request.Content = new ByteArrayContent(buffer, 0, count);
return request;
}
Expand Down Expand Up @@ -242,7 +247,7 @@ private HttpRequestMessage CreatePutBlockListRequest(string containerName, strin
sb.Append("</BlockList>");

// call https://docs.microsoft.com/en-us/rest/api/storageservices/put-block-list
var request = new HttpRequestMessage(HttpMethod.Put, $"/{containerName}/{blobName}?comp=blocklist");
var request = new HttpRequestMessage(HttpMethod.Put, $"{containerName}/{blobName}?comp=blocklist");
request.Content = new StringContent(sb.ToString());
return request;
}
Expand All @@ -263,7 +268,7 @@ private HttpRequestMessage CreatePutBlobRequest(string containerName, string blo
{
// https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob

var request = new HttpRequestMessage(HttpMethod.Put, $"/{containerName}/{blobName}");
var request = new HttpRequestMessage(HttpMethod.Put, $"{containerName}/{blobName}");
request.Content = new ByteArrayContent(new byte[0]); // sets Content-Type to 0
request.Headers.Add("x-ms-blob-type", blobType);
return request;
Expand Down Expand Up @@ -299,7 +304,7 @@ public override async Task<Stream> OpenRead(IOPath path, CancellationToken cance
throw new ArgumentNullException(nameof(path));

// call https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob
var request = new HttpRequestMessage(HttpMethod.Get, $"/{GetContainerName(path)}/{GetPathInContainer(path)}");
var request = new HttpRequestMessage(HttpMethod.Get, $"{GetContainerName(path)}/{GetPathInContainer(path)}");
HttpResponseMessage response = await SendAsync(request);

if(response.StatusCode == HttpStatusCode.NotFound)
Expand Down Expand Up @@ -327,7 +332,7 @@ public override async Task Rm(IOPath path, bool recurse, CancellationToken cance
else
{
https://docs.microsoft.com/en-us/rest/api/storageservices/delete-blob
HttpResponseMessage response = await SendAsync(new HttpRequestMessage(HttpMethod.Delete, $"/{_containerName}{path}"));
HttpResponseMessage response = await SendAsync(new HttpRequestMessage(HttpMethod.Delete, $"{_containerName}{path}"));
if(response.StatusCode != HttpStatusCode.NotFound)
{
response.EnsureSuccessStatusCode();
Expand Down

0 comments on commit 26490d4

Please sign in to comment.