Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AWS image cache control
Browse files Browse the repository at this point in the history
ssgueye2 committed Apr 20, 2023
1 parent d7d4545 commit 73e75a0
Showing 3 changed files with 27 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ public async Task<string> ChangeDeviceModelImageAsync(string deviceModelId, Stre

public Uri ComputeImageUri(string deviceModelId)
{
return new Uri($"https://{this.configHandler.AWSBucketName}.s3.{RegionEndpoint.GetBySystemName(this.configHandler.AWSRegion)}.amazonaws.com/{deviceModelId}");
throw new NotImplementedException();
}

private string ComputeImageUrl(string deviceModelId)
@@ -183,42 +183,13 @@ public async Task InitializeDefaultImageBlob()

}

public async Task SyncImagesCacheControl()
public Task SyncImagesCacheControl()
{
/* We don't need an implementation of
this mehod for AWS because new images will processed by the method SetDefaultImageToModel
*/
throw new NotImplementedException();

this.logger.LogInformation($"Synchronize Cache control images");

var listImagesObjects = new ListObjectsRequest
{
BucketName = this.configHandler.AWSBucketName
};

//Get All images from AWS S3
var response = await this.s3Client.ListObjectsAsync(listImagesObjects);
if (response != null && response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
foreach (var item in response.S3Objects)
{
var copyObjectRequest = new CopyObjectRequest
{
SourceBucket = this.configHandler.AWSBucketName,
SourceKey = item.Key,
DestinationBucket = this.configHandler.AWSBucketName,
DestinationKey = item.Key,
Headers = {CacheControl = $"max-age={this.configHandler.StorageAccountDeviceModelImageMaxAge}, must-revalidate" }
};
var copyObjectResponse = await this.s3Client.CopyObjectAsync(copyObjectRequest);
if (copyObjectResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
throw new InternalServerErrorException($"Cache control Synchronization failed for object {item}");

}
}
}
else
{
throw new InternalServerErrorException($"Can not retreive list of images to synchronize");
}
}
}
}
7 changes: 7 additions & 0 deletions src/AzureIoTHub.Portal.Server/Startup.cs
Original file line number Diff line number Diff line change
@@ -448,6 +448,7 @@ public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)
await ConfigureAzureAsync(app);
break;
case CloudProviders.AWS:
await ConfigureAwsAsync(app);
break;
default:
break;
@@ -463,6 +464,12 @@ private static async Task ConfigureAzureAsync(IApplicationBuilder app)

await EnsureDatabaseCreatedAndUpToDate(app)!;
}
private static async Task ConfigureAwsAsync(IApplicationBuilder app)
{
var deviceModelImageManager = app.ApplicationServices.GetService<IDeviceModelImageManager>();

await deviceModelImageManager?.InitializeDefaultImageBlob()!;
}

private static void UseApiExceptionMiddleware(IApplicationBuilder app)
{
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@

namespace AzureIoTHub.Portal.Tests.Unit.Infrastructure.Managers
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
@@ -402,108 +403,33 @@ public void InitializeDefaultImageBlobShouldThrowsAnExceptionForPutACLStatusCode


[Test]
public void SyncImagesCacheControlShouldThrowsAnExceptionForCopyObjectAsyncStatusCode()
public void SyncImagesCacheControlShouldThrowsANotImplmentedException()
{
// Arrange
var bucketName = "invalid bucket Name for example";

var listObjectsResponse = new ListObjectsResponse
{
HttpStatusCode = HttpStatusCode.OK,
S3Objects = new List<S3Object>
{
new S3Object { Key = "image1.jpg" },
new S3Object { Key = "image2.png" }
}
};

_ = this.mockConfigHandler.Setup(handler => handler.AWSBucketName).Returns(bucketName);
_ = this.mockConfigHandler.Setup(handler => handler.StorageAccountDeviceModelImageMaxAge).Returns(Fixture.Create<int>());
// We just verify that the method throw NotImplmentedExeception()

_ = this.s3ClientMock.Setup(s3 => s3.ListObjectsAsync(It.IsAny<ListObjectsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(listObjectsResponse);
_ = this.s3ClientMock.Setup(s3 => s3.CopyObjectAsync(It.IsAny<CopyObjectRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new CopyObjectResponse
{
HttpStatusCode = HttpStatusCode.BadGateway
});
// Act
var act = () => this.awsDeviceModelImageManager.SyncImagesCacheControl();

// Assert
_ = Assert.ThrowsAsync<InternalServerErrorException>(async () =>
{
// Act
await this.awsDeviceModelImageManager.SyncImagesCacheControl();

}, "Cache control Synchronization failed");
this.s3ClientMock.VerifyAll();
_ = act.Should().ThrowAsync<NotImplementedException>();
}

[Test]
public void SyncImagesCacheControlShouldThrowsAnExceptionForListObjectsAsyncStatusCode()
{
// Arrange
var bucketName = "invalid bucket Name for example";

var listObjectsResponse = new ListObjectsResponse
{
HttpStatusCode = HttpStatusCode.BadGateway,
S3Objects = new List<S3Object>
{
new S3Object { Key = "image1.jpg" },
new S3Object { Key = "image2.png" }
}
};

_ = this.mockConfigHandler.Setup(handler => handler.AWSBucketName).Returns(bucketName);
_ = this.mockConfigHandler.Setup(handler => handler.StorageAccountDeviceModelImageMaxAge).Returns(Fixture.Create<int>());

_ = this.s3ClientMock.Setup(s3 => s3.ListObjectsAsync(It.IsAny<ListObjectsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(listObjectsResponse);
/*===========================*** Tests for ComputeImageUri() **===========================*/


// Assert
_ = Assert.ThrowsAsync<InternalServerErrorException>(async () =>
{
// Act
await this.awsDeviceModelImageManager.SyncImagesCacheControl();

}, "Can not retreive list of images to synchronize");
this.s3ClientMock.VerifyAll();
}

[Test]
public async Task SyncImagesCacheControlShouldSucceed()
public void ComputeImageUriShouldThrowsANotImplmentedException()
{
// Arrange
var bucketName = "invalid bucket Name for example";

var listObjectsResponse = new ListObjectsResponse
{
HttpStatusCode = HttpStatusCode.OK,
S3Objects = new List<S3Object>
{
new S3Object { Key = "image1.jpg" },
new S3Object { Key = "image2.png" }
}
};

_ = this.mockConfigHandler.Setup(handler => handler.AWSBucketName).Returns(bucketName);
_ = this.mockConfigHandler.Setup(handler => handler.StorageAccountDeviceModelImageMaxAge).Returns(Fixture.Create<int>());

_ = this.s3ClientMock.Setup(s3 => s3.ListObjectsAsync(It.IsAny<ListObjectsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(listObjectsResponse);

_ = this.s3ClientMock.Setup(s3 => s3.CopyObjectAsync(It.IsAny<CopyObjectRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new CopyObjectResponse
{
HttpStatusCode = HttpStatusCode.OK
});

// Act
await this.awsDeviceModelImageManager.SyncImagesCacheControl();
var deviceModelId = Fixture.Create<string>();

// Assert
this.s3ClientMock.VerifyAll();
_ = Assert.Throws<NotImplementedException>(() =>
{
// Act
_ = this.awsDeviceModelImageManager.ComputeImageUri(deviceModelId);
});
}
}
}

0 comments on commit 73e75a0

Please sign in to comment.