Microsoft.Azure.StorageException: The specified resource name contains invalid characters

荒凉一梦 提交于 2021-02-18 10:29:30

问题


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");
            }
        }

        }
    }                     

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.


回答1:


Seems your Container name or connection string wrong.

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.

Please refer the documentation on Container naming convention




回答2:


In case this helps anyone, I encountered this same error when publishing to an Azure function from Visual Studio. After a bit of poking around, it turns out that during the setup process a new function file was automatically added that included the following code:

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

Once I changed the

Path/{name} part to lowercase

path/{name}, the error went away.

I ultimately deleted this auto generated file, but it goes to show that you can't always trust the automatic route, and further confirms Sajeetharan's answer.



来源:https://stackoverflow.com/questions/58265382/microsoft-azure-storageexception-the-specified-resource-name-contains-invalid-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!