Azure文件存储创建文件404错误

时间:2017-09-15 17:44:15

标签: c# .net azure azure-storage azure-storage-files

我有以下代码试图在我的azure文件存储帐户

上创建一个空白文件
CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString);
var fc = sa.CreateCloudFileClient();
var share = fc.GetShareReference("uploadparking");
share.CreateIfNotExists();
var rootDirectory = share.GetRootDirectoryReference();
var subDirectory = rootDirectory.GetDirectoryReference("valuationrequests");
subDirectory.CreateIfNotExists();
var uri = new Uri(subDirectory.Uri.ToString() + "/file.txt");
var file = new CloudFile(uri);
file.Create(0);

在最后一行,我得到以下异常:

Microsoft.WindowsAzure.Storage.StorageException' occurred in Microsoft.WindowsAzure.Storage.dll

Additional information: The remote server returned an error: (404) Not Found.

我不确定它找不到什么。它不应该试图找到文件,因为它正在创建它。我已确认目录存在。

如果有人知道我可以成功创建文件,请告诉我。我已经检查了教程,他们遗憾地只展示了如何下载不上传的文件。

1 个答案:

答案 0 :(得分:1)

我认为文档不正确。该文档仅提到URI应该是绝对的。它没有提到如果您使用绝对URI,那么您还应该传递存储凭据,或者URI应该包含至少具有CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString); var fc = sa.CreateCloudFileClient(); var share = fc.GetShareReference("uploadparking"); share.CreateIfNotExists(); var rootDirectory = share.GetRootDirectoryReference(); var subDirectory = rootDirectory.GetDirectoryReference("valuationrequests"); subDirectory.CreateIfNotExists(); var uri = new Uri(subDirectory.Uri.ToString() + "/file.txt"); var file = new CloudFile(uri, sa.Credentials); file.Create(0); 权限的共享访问签名来创建文件。

您应该尝试使用以下CloudFile覆盖来创建实例:https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.file.cloudfile.-ctor?view=azurestorage-8.1.3#Microsoft_WindowsAzure_Storage_File_CloudFile__ctor_System_Uri_Microsoft_WindowsAzure_Storage_Auth_StorageCredentials_

所以你的代码是:

Shared Access Signature (SAS)

其他替代方法是在共享上创建CloudStorageAccount sa = CloudStorageAccount.Parse(connectionString); var fc = account.CreateCloudFileClient(); var share = fc.GetShareReference("uploadparking"); share.CreateIfNotExists(); var rootDirectory = share.GetRootDirectoryReference(); var subDirectory = rootDirectory.GetDirectoryReference("valuationrequests"); subDirectory.CreateIfNotExists(); SharedAccessFilePolicy policy = new SharedAccessFilePolicy() { Permissions = SharedAccessFilePermissions.Create, SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(15) }; var sasToken = share.GetSharedAccessSignature(policy); var uri = new Uri(subDirectory.Uri.ToString() + "/file1.txt" + sasToken); var file = new CloudFile(uri); file.Create(0); 令牌,并在创建CloudFile实例时使用SAS URL。所以在这种情况下你的代码是:

{{1}}