[英]How to rename file/directory (not Blob file) in Azure Storage (not Blob storage)?
[英]How to integration test Azure Blob Storage?
我正在开展一个企业业务项目,我需要将应用程序从本地迁移到 Azure 云。
某些应用程序需要 Azure Blob 存储。 所有 Azure 云基础设施都可以使用管理身份访问,并且业务要求是测试和验证 Azure Blob 方法,而无需访问 Z3A580F142203677F1F0BC30898F63F5 生产或生产门户。 也就是说,业务要求我们通过在本地和 GitHub 工作流上测试代码,甚至在将代码推送到云之前使所有存储工作正常。
当然,我可以启动我的个人 Azure 帐户并使用它,但仍然会使用我的帐户作为游乐场进行测试,但不是真正可用的测试。
通用测试 Azure Blob 存储的整个想法,无需对 Blob 存储具有任何访问权限。
这可能吗?我怎样才能做到这一点?
以下是我对 Azure Blob 的工作 POC 方法:
private readonly BlobContainerClient _blobContainerClient;
public AzureBlobStorage(string connectionString, string container)
{
_blobContainerClient = new BlobContainerClient(connectionString, container);
_blobContainerClient.CreateIfNotExists();
}
public async Task<string> ReadTextFile(string filename)
{
var blob = _blobContainerClient.GetBlobClient(filename);
if (!await _blobContainerClient.ExistsAsync()) return string.Empty;
var reading = await blob.DownloadStreamingAsync();
StreamReader reader = new StreamReader(reading.Value.Content);
return await reader.ReadToEndAsync();
}
public async Task CreateTextFile(string filename, byte[] data)
{
var blob = _blobContainerClient.GetBlobClient(filename);
await using var ms = new MemoryStream(data, false);
await blob.UploadAsync(ms, CancellationToken.None);
}
public async Task DeleteTextFile(string filename)
{
var blobClient = _blobContainerClient.GetBlobClient(filename);
await blobClient.DeleteAsync();
}
经过一番研究,我找到了解决方案并写了一篇关于它的文章。 这是解决方案的简短版本。
要针对测试环境进行集成测试,我建议您遵循以下答案:
这是可能的,但需要几个步骤:
// here we pull azurite image
docker pull mcr.microsoft.com/azure-storage/azurite
// here we run azurite image and store data under c:\azurite folder
docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 -v c:/azurite:/data mcr.microsoft.com/azure-storage/azurite
就我而言,我使用 azure 存储方法制作了一个简单的 class。 您可以执行以下操作(仅示例):
[Fact]
public async Task AzureBlobStorageTest()
{
// Arrange
await _azureBlobStorage?.CreateTextFile("file.txt", Encoding.UTF8.GetBytes(Content))!;
// Act
var readTextFile = await _azureBlobStorage.ReadTextFile("file.txt");
// Assert
Assert.Equal(Content, readTextFile);
// Finalizing
await _azureBlobStorage.DeleteTextFile("file.txt");
}
您可以更详细地阅读这些步骤并找到源代码。
享受
免责声明:正如我上面提到的,我写了 3 篇关于解决这个问题的文章。 这个链接是指我个人的web站点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.