Microsoft.Azure.StorageException:指定的资源名称包含无效字符

时间:2019-10-07 07:47:58

标签: c# azure azure-blob-storage

我正在创建Blob存储,以将文件从本地路径加载到云。使用我在门户网站上创建的存储帐户,出现错误:Microsoft.Azure.Storage.StorageException:The specified resource name contains invalid characters。这是我想要实现的代码。它缺少什么?请指教

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Azure.Storage;
using System.IO;

namespace BlobStorageApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Azure Blob Storage - Net");
            Console.WriteLine();

            ProcessAsync().GetAwaiter().GetResult();
        }

        private static async Task ProcessAsync()
        {
            CloudStorageAccount storageAccount = null;
            CloudBlobContainer cloudBlobContainer = null;
            string sourceFile = null;
            string destinationFile = null;

            string storageConnectionString = "DefaultEndpointsProtocol=https;" +
                "AccountName=gcobanistorage;" +
                "AccountKey=****;" +
                "EndpointSuffix=core.windows.net";

            if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
            {
                try
                {
                    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                    cloudBlobContainer = cloudBlobClient.GetContainerReference("Test" + Guid.NewGuid().ToString());
                    await cloudBlobContainer.CreateAsync();
                    Console.WriteLine("Created container '{0}'", cloudBlobContainer.Name);
                    Console.WriteLine();

                    BlobContainerPermissions permissions = new BlobContainerPermissions
                    {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    };
                    await cloudBlobContainer.SetPermissionsAsync(permissions);

                    string localPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    string localFileName = "Test.txt" + Guid.NewGuid().ToString() + "test_.txt";
                    sourceFile = Path.Combine(localPath, localFileName);

                    File.WriteAllText(sourceFile,"Good day, how are you!!?");
                    Console.WriteLine("Temp file = {0}", sourceFile);
                    Console.WriteLine("Uploading to Blob storage as blob {0}", localFileName);

                    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(localFileName);
                    await cloudBlockBlob.UploadFromFileAsync(sourceFile);

                    Console.WriteLine("Listing blobs in container.");
                    BlobContinuationToken blobContinuationToken = null;

                    do
                    {
                        var resultSegment = await cloudBlobContainer.ListBlobsSegmentedAsync(null, blobContinuationToken);
                        blobContinuationToken = resultSegment.ContinuationToken;
                        foreach (IListBlobItem item in resultSegment.Results) {
                            Console.WriteLine(item.Uri);
                        }
                    } while (blobContinuationToken != null);
                      Console.WriteLine();

                        destinationFile = sourceFile.Replace("test_eNtsa.txt", "Rest.txt");
                        Console.WriteLine("Downloading blob to {0}", destinationFile);
                        Console.WriteLine();

                        await cloudBlockBlob.DownloadToFileAsync(destinationFile, FileMode.Create);

                }
                catch(StorageException ex)
                {
                    Console.WriteLine("Error returned from the service:{0}", ex.Message);
                }
                finally
                {
                    Console.WriteLine("Press any key to delete the sample files and example container");
                    Console.ReadLine();
                    Console.WriteLine("Deleting the container and any blobs in contains");

                    if(cloudBlobContainer != null)
                    {
                        await cloudBlobContainer.DeleteIfExistsAsync();
                    }
                    Console.WriteLine("Deleting the local source file and local downloaded files");
                    Console.WriteLine();
                    File.Delete(sourceFile);
                    File.Delete(destinationFile);
                }
            }
            else
            {
                Console.WriteLine("A connection string has not been defined in the system environment variables." 
                 + "Add a environment variable named 'storageconnectionstring' with your storage" 
                 + "connection string as a value");
            }
        }

        }
    }                     

嗨,有队友可以帮助我,因为我遇到这种情况?解决方案缺少什么?已在门户网站上创建了存储帐户,并且我正在使用门户网站上的连接字符串,还创建了容器。我应该添加或修改任何内容吗?该错误的原因可能是什么?我只是想了解它,也许我在'connectionString'上打了无效的名字?或者,请为我提供一些有关此问题的想法,因为在没有互联网帮助的情况下,我在接近1天的时间内一直被这个问题困扰。反馈可能会受到高度赞赏和指导,非常感谢,因为我期待着有关此问题的更多详细信息。

2 个答案:

答案 0 :(得分:6)

如果这对任何人有帮助,我在从 Visual Studio 发布到 Azure 函数时遇到了同样的错误。经过一番摸索,发现在安装过程中自动添加了一个新的函数文件,其中包含以下代码:

[FunctionName("Function")]
public static void Run([BlobTrigger("Path/{name}") ...

一旦我改变了

Path/{name} 部分为小写

path/{name},错误消失了。

我最终删除了这个自动生成的文件,但这表明您不能总是信任自动路由,并进一步证实了 Sajeetharan 的回答。

答案 1 :(得分:0)

您的容器名称或连接字符串似乎错误。

  

容器名称必须是有效的DNS名称,符合以下要求   命名规则:容器名称必须以字母或数字开头,并且   只能包含字母,数字和破折号(-)字符。每一个   破折号(-)必须紧接在前面,并在其后   字母或数字;容器中不允许连续的破折号   名称。容器名称中的所有字母都必须小写。容器   名称必须在3到63个字符之间。

请参阅Container naming convention

上的文档