Gco*_*bza 17 c# azure azure-blob-storage
我正在创建 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");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
嗨,团队有没有人可以帮助我,因为我遇到了这个例外?解决方案中缺少什么?存储帐户已在门户上创建,我正在使用来自门户的连接字符串,容器也已创建。有什么我应该可以添加或修改的吗?这个错误可能是什么原因?我只是想了解它,也许我在我的“connectionString”上调用了无效的名称?或者请为我提供一些关于此的想法,因为我在没有互联网帮助的情况下坚持了近 1 天的时间。反馈可能会受到高度赞赏和指导,谢谢,因为我期待有关此问题的更多详细信息。
Saj*_*ran 21
似乎您的容器名称或连接字符串错误。
容器名称必须是有效的 DNS 名称,符合以下命名规则: 容器名称必须以字母或数字开头,并且只能包含字母、数字和破折号 (-) 字符。每个破折号 (-) 字符的前后必须紧跟一个字母或数字;容器名称中不允许出现连续的破折号。容器名称中的所有字母都必须小写。容器名称的长度必须为 3 到 63 个字符。
请参考文档 Container naming convention
如果这对任何人都有帮助,我在从 Visual Studio 发布到 Azure 函数时遇到了同样的错误。经过一番摸索,发现在安装过程中自动添加了一个新的函数文件,其中包含以下代码:
[FunctionName("Function")]
public static void Run([BlobTrigger("Path/{name}") ...
Run Code Online (Sandbox Code Playgroud)
一旦我改变了
Path/{name}
部分小写
path/{name}
,错误消失了。
我最终删除了这个自动生成的文件,但这表明您不能总是信任自动路由,并进一步证实了 Sajeetharan 的回答。
归档时间: |
|
查看次数: |
13186 次 |
最近记录: |