我正在尝试将文件上传到Azure的云存储,但只能得到bad request 400
响应。在本地主机上运行该应用程序,并尝试连接到我们在Azure上的帐户。
当我将其放在try / catch中时,我bad request 400
在最后一行
cloudFile.Create(fileSize);
下面的这一行返回null,但是当我在if语句中检查它时,文件共享存在:
var listShares = fileClient.ListShares(); // 空值
我只找到了一个有关如何实际上传文件的示例:
// Connect to Azure Storage
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnectionString"));
//Create a CloudFileClient object for credentialed access to File storage.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
//Get a reference to the file share we created previously.
CloudFileShare share = fileClient.GetShareReference("appbackup");
var listShares = fileClient.ListShares();
//Ensure that the share exists.
if (share.Exists())
{
//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
//Get a reference to the sampledir directory we created previously.
CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("sampledir");
//Ensure that the directory exists.
if (sampleDir.Exists())
{
var test = rootDir.ListFilesAndDirectories();
var Credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("accountName", "keyValue");
string stringUri = storageAccount.FileStorageUri.PrimaryUri + share.Name + "/sampledir/";
Uri theUri = new Uri(stringUri);
CloudFile cloudFile = new CloudFile(theUri, Credentials);
System.IO.FileInfo fi = new System.IO.FileInfo(filepath);
long fileSize = fi.Length;
cloudFile.Create(fileSize);
}
}
我相信您的代码中存在一个问题:
string stringUri = storageAccount.FileStorageUri.PrimaryUri + share.Name + "/sampledir/";
您没有将文件名添加到中stringUri
。请尝试通过将文件名添加stringUri
到来解决问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句