【发布时间】:2020-04-24 04:29:51
【问题描述】:
更新:我可以验证此行为已在 Azure.Storage.Blobs 12.5.1 https://www.nuget.org/packages/Azure.Storage.Blobs https://github.com/Azure/azure-sdk-for-net/issues/9404 中得到修复
如何使用主机名连接到 azurite?
我正在尝试使用 Azurite 在 docker 中模拟 Azure Blob Storage 以进行集成测试。
一切正常,以至于我必须通过主机名访问 Azurite(这是 Docker 网络所需的 AFAIK)
我的连接字符串如下所示(这是默认的知名连接字符串):
"AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=http://azurite:10000/devstoreaccount1;"
我的 docker compose 部分为 azurite 如下所示:
services:
azurite:
image: mcr.microsoft.com/azure-storage/azurite
hostname: azurite
command: "azurite-blob --loose --blobHost 0.0.0.0"
ports:
- "10000:10000"
volumes:
- ./test/azurite:/data
networks:
- stillsnet
images:
container_name: images
image: myapp/images
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:5000"
- "5001:5001"
environment:
- ASPNETCORE_ENVIRONMENT=Test
- ASPNETCORE_URLS=http://+:5000
- imagesStorage__AzureBlobStorage__ConnectionString=AccountName=devstoreaccount1;DefaultEndpointsProtocol=http;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://azurite:10000;
depends_on:
- azurite
links:
- azurite
networks:
- stilssnet
我的代码如下所示:
private const string ConnectionString ="AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=http://azurite:10000/devstoreaccount1;";
[Fact]
public async Task UploadFile()
{
var container = new BlobContainerClient(ConnectionString, "images");
await using var stream = File.OpenRead(@"C:\temp\output\3ee9bc41-40ea-4d05-b180-e74bd5065622\images\00000000.jpg");
await container.UploadBlobAsync("test.jpg", stream);
}
这会抛出异常:
System.Xml.XmlException : Root element is missing.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Load(Stream stream, LoadOptions options)
at Azure.Storage.Blobs.BlobRestClient.Container.CreateAsync_CreateResponse(Response response)
at Azure.Storage.Blobs.BlobRestClient.Container.CreateAsync(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri resourceUri, PublicAccessType access, Nullable`1 timeout, IDictionary`2 metadata, String requestId, Boolean async, String operationName, CancellationToken cancellationToken)
at Azure.Storage.Blobs.BlobContainerClient.CreateInternal(PublicAccessType publicAccessType, IDictionary`2 metadata, Boolean async, CancellationToken cancellationToken, String operationName)
at Azure.Storage.Blobs.BlobContainerClient.CreateIfNotExistsInternal(PublicAccessType publicAccessType, IDictionary`2 metadata, Boolean async, CancellationToken cancellationToken)
at Azure.Storage.Blobs.BlobContainerClient.CreateIfNotExistsAsync(PublicAccessType publicAccessType, IDictionary`2 metadata, CancellationToken cancellationToken)
如果我将连接字符串从azurite 更改为127.0.0.1,一切正常。
【问题讨论】:
-
你解决过这个问题吗?我也有同样的问题...
-
@the_witch_king_of_angmar 只有我在下面描述的黑客,我已经在 Azure 存储 SDK 提交了一个错误
-
@the_witch_king_of_angmar 此行为已在 Azure.Storage.Blobs 12.5.1 中修复
标签: c# azure-blob-storage