gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:48:47 25 4
gpt4 key购买 nike

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

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

}
}

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

最佳答案

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

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.

请参阅 Container naming convention 上的文档

关于c# - Microsoft.Azure.StorageException : The specified resource name contains invalid characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58265382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com