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

Gcobza picture Gcobza · Oct 7, 2019 · Viewed 9k times · Source

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.

Answer

Sajeetharan picture Sajeetharan · Oct 7, 2019

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