Microsoft.Azure.StorageException:指定的资源名称包含无效字符 [英] Microsoft.Azure.StorageException: The specified resource name contains invalid characters

查看:51
本文介绍了Microsoft.Azure.StorageException:指定的资源名称包含无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Blob存储,以将文件从本地路径加载到云.使用我在门户网站上创建的存储帐户,出现错误: Microsoft.Azure.Storage.StorageException:指定的资源名称包含无效字符.这是我想要实现的代码.它缺少什么?请指教

I am creating blob storage to load a file from local path to cloud. Using storage account I have created on portal, I am getting an error: Microsoft.Azure.Storage.StorageException:The specified resource name contains invalid characters. Here is my code below what i am trying to achieve. What is it missing? Please advice

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天.反馈可能会受到高度赞赏和指导,非常感谢,因为我期待着更多有关此问题的详细信息.

Hi Team is there any mate who can help me, as i am getting this exception? What am missing from the solution? The Storage account has been created on portal and i am using connection string from portal, the container has been also created. Is there anything i should be in a position to add or modify? What could be reason for this error? i just want to understand it, maybe i am calling not valid name on my 'connectionString'? Or Kindly please provide me with some idea around this as i am stuck to this problem for close 1 day with no help from internet. Feedback maybe highly appreciated and guidance, thanks as i am looking forward for more details to this problem.

推荐答案

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

Seems your Container name or connection string wrong.

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

A container name must be a valid DNS name, conforming to the following naming rules: Container names must start with a letter or number, and can contain only letters, numbers, and the dash (-) character. Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in container names. All letters in a container name must be lowercase. Container names must be from 3 through 63 characters long.

请参考 容器命名约定

这篇关于Microsoft.Azure.StorageException:指定的资源名称包含无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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