gpt4 book ai didi

azure - 通过 Web API 将文件上传到 azure 文件存储

转载 作者:行者123 更新时间:2023-12-03 03:15:56 25 4
gpt4 key购买 nike

我尝试将文件上传到 Azure 的云存储,但只收到错误请求 400 响应。在本地主机上运行应用程序并尝试连接到我们在 Azure 上的帐户。

当我将其放入 try/catch 中时,我在最后一行收到错误请求 400

cloudFile.Create(fileSize);

下面的这一行返回 null,但当我在 if 语句中检查文件共享时它存在:

var listShares = fileClient.ListShares(); // null

我只找到了一个关于如何实际上传这样的文件的示例:

// 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 中,这样应该可以解决问题。

关于azure - 通过 Web API 将文件上传到 azure 文件存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26548237/

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