c# - 使用主机名连接到azurite失败

标签 c# azure-storage-blobs

更新:我可以验证此行为在Azure.Storage.Blobs 12.5.1中是否已修复

如何使用主机名连接到azurite?
我正在尝试使用Azurite进行集成测试以模拟docker中的Azure Blob存储。
一切正常,以至于我必须通过主机名访问Azurite(Docker网络需要AFAIK)
我的连接字符串如下所示(这是默认的知名连接字符串):"AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=http://azurite:10000/devstoreaccount1;"我的azurite docker 组成部分如下所示:

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,则一切正常。

最佳答案

该网络中的主机名是azurite,因此您可以使用此连接字符串UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://azurite

为我工作,我认为是这样做的方法。

您也可以使用旧版存储模拟器(包括表),而不是使用azurite放置docker.host.internal

关于c# - 使用主机名连接到azurite失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59615330/

相关文章:

c# - 获取 DEP 设置

rest - 在 HTTP 请求 '...' 中找到的 MAC 签名与任何计算签名都不相同

docker - 使用部署为Azure Webapp的单个Docker镜像从Python连接到Blob存储

python-3.x - 使用 azure-storage-blob 或 azure-storage 上传和删除 Azure 存储 Blob

c# - 如何在 C# 中使用 using 语句进行错误处理

c# - 如何以编程方式编辑 Azure 辅助角色的 ServiceConfiguration 文件的 NetworkConfiguration 节点?

azure-sql-database - 正确使用Azure存储。 (何时使用SQL,表和Blob)

powershell - 在Windows Server 2012实例上通过cmd完成时,通过PowerShell使用 'net use'表现出不同的行为

c# - 如何使用 Unity 名称动态注册泛型类?

c# - 如何访问阻塞集合的底层默认并发队列