使用主机名连接到azurite失败 [英] Connecting to azurite using a hostname fails

查看:81
本文介绍了使用主机名连接到azurite失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我可以验证此行为在Azure.Storage.Blobs 12.5.1中是否已修复. https://www.nuget.org/packages/Azure.Storage.Blobs https://github.com/Azure/azure-sdk-for-net/issues/9404

UPDATE: I can verify this behavior is fixed in 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?

How can I connect to azurite using a hostname?

我正在尝试使用Azurite进行集成测试以模拟docker中的Azure Blob存储.

I'm trying to emulate Azure Blob Storage in docker using Azurite for integration tests.

一切正常,以至于我必须通过主机名(Docker网络需要AFAIK)访问Azurite

All works well, to the point I have to access Azurite via a hostname (which is AFAIK required for docker networking)

我的连接字符串如下所示(这是默认的知名连接字符串):

My connection string looks like this (which is the default well-known connection string):

"AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=http://azurite:10000/devstoreaccount1;"

我的azurite码头工人组成部分如下所示:

My docker compose part for azurite looks like this:

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

If I change the connection string from azurite to 127.0.0.1 it all works fine.

推荐答案

更新:我可以验证此行为在Azure.Storage.Blobs 12.5.1中是否已修复. packages/Azure.Storage.Blobs"rel =" nofollow noreferrer> https://www.nuget.org/packages/Azure.Storage.Blobs https://github.com/Azure/azure-sdk-for- net/issues/9404

UPDATE: I can verify this behavior is fixed in Azure.Storage.Blobs 12.5.1 https://www.nuget.org/packages/Azure.Storage.Blobs https://github.com/Azure/azure-sdk-for-net/issues/9404

如果您使用的不是本地主机或IP地址,则BlobContainerClient本身会从URL中删除帐户名,因此客户端会生成以下内容:

It looks like the BlobContainerClient itself strips away the account name from the URL if you use anything else than localhost or an IP address, so the client generates this:

PUT /images?restype=container HTTP/1.1" 400- 代替 PUT /devstoreaccount1/images?restype=container HTTP/1.1 201

PUT /images?restype=container HTTP/1.1" 400 - instead of PUT /devstoreaccount1/images?restype=container HTTP/1.1 201

作为一个丑陋的解决方法,在对azurite进行测试时,我们可以在容器名称中包含帐户名称: var container = new BlobContainerClient(ConnectionString,"devstoreaccount1/images");

As an ugly workaround we can include the account name in the container name when testing against azurite: var container = new BlobContainerClient(ConnectionString, "devstoreaccount1/images");

尽管正在等待container.CreateIfNotExistsAsync();然后不能对azurite正常工作(当它已经存在时会抛出409异常...

Though await container.CreateIfNotExistsAsync(); doesn't work properly against azurite then (throws a 409 Exception when it already exist...

所以我们有:

  • 一个丑陋的黑客
  • 放弃青石矿,转而使用真正的Blob存储帐户
  • 删除BlobContainerClient,它似乎根据主机名执行了太多的魔术操作.

它似乎与Azurite本身无关,或者理想情况下,它应该支持与Azure blob存储兼容的根URL,而无需指定帐户名前缀.

It doesn't seem to be related to Azurite itself, or ideally it should support root URL's that are compatible with the Azure blob store without having to specify the account name prefix.

这篇关于使用主机名连接到azurite失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆