【发布时间】:2020-02-04 12:04:16
【问题描述】:
我正在创建 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 天。反馈可能会受到高度赞赏和指导,谢谢,因为我期待有关此问题的更多详细信息。
【问题讨论】:
-
问题在于以大写字母 (T) 开头的容器名称。请将其更改为小写,您应该不会收到此错误。
-
感谢团队,我已经设法使它工作,我只缺少命名转换规则。一切似乎都很好,感谢您的回复。
标签: c# azure azure-blob-storage