使用REST API从Web应用程序上传文件到Azure文件存储

15

我有一个目前使用WebForms而非MVC的网络应用程序,它将被托管在Azure平台上。

这个Web应用程序的主要功能是将用户文件上传到Azure文件存储中。

这些文件可能是pdf、mp3等,不是简单的文本、数据流或数据输入。

我被告知要使用Azure REST API来上传文件,但我真的不熟悉它,也找不到好的样例、教程或在线视频。Microsoft目前的文档对我来说读起来像??????。

目前我只是上传到本地文件夹,所以代码看起来像这样:

FileUpload1.PostedFile.SaveAs(Server.MapPath("fileupload\\" + FileUpload1.FileName));

然后我该去哪里呢? 我想我应该添加一个StorageConnectionString,它看起来像 DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=yyy,我已经有了。

那么我应该写一些类似于C#中的'post'的代码吗?

2个回答

27

Azure提供了一个NuGet库,可用于在Azure文件存储中上传和执行其他“文件管理”类型的活动。

该库称为:WindowsAzure.Storage

更新:新要使用的库是Azure.Storage.Blobs

以下是启动此操作的基础知识:

//Connect to Azure
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

// Create a reference to the file client.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();      

// Create a reference to the Azure path
CloudFileDirectory cloudFileDirectory = GetCloudFileShare().GetRootDirectoryReference().GetDirectoryReference(path);

//Create a reference to the filename that you will be uploading
CloudFile cloudFile = cloudSubDirectory.GetFileReference(fileName);

//Open a stream from a local file.
Stream fileStream= File.OpenRead(localfile);

//Upload the file to Azure.
await cloudFile.UploadFromStreamAsync(fileStream);
fileStream.Dispose();

更多链接和信息在这里(请注意向下滚动一段距离以查看示例):https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-files/


哦,我的天啊,我都快哭了,这太有帮助了,谢谢!救了我的一天。解决了大部分的问题! - AprilX
为什么我们要使用 "await"?这是必要的吗?当我使用它时,错误显示 "await operator can only be used within an Async method",当我将 async 添加到我的 "protected void Button1_Click"(这是上传文件的按钮单击事件)中,将其更改为 "protected async void" 时,另一个错误将显示为 "An asynchronous operation cannot be started at this time"......我感到很羞愧,因为我不理解这么多东西 (T . T) - AprilX
1
UploadFromStreamAsync 是一个异步方法。因此,您需要将其放在一个带有 async 的方法中,例如 static async void UploadFile(),或使用 Wait() 函数使其同步。cloudFile.UploadFromStreamAsync(fileStream).Wait();。祝你好运。 - Gary Holland
谢谢!经过一些努力,我终于解决了这个问题,并在下面附上了我的最终代码。感谢您的帮助!我希望这个问题能帮助那些有类似问题的人。 - AprilX
@GaryHolland,我们是否有其他方法可以直接将文件上传到云目录中,而不是在目录中创建文件引用并进行异步复制? - RJN

10

这段代码基于我从Gary Holland那里得到的答案。希望其他人也能受益。我不擅长编程,希望这段代码看起来还不错。

if (FileUpload1.HasFile)
    {
        //Connect to Azure
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

        // Create a reference to the file client.
        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

        // Get a reference to the file share we created previously.
        CloudFileShare share = fileClient.GetShareReference("yourfilesharename");

        if (share.Exists())
        {


            // Generate a SAS for a file in the share
            CloudFileDirectory rootDir = share.GetRootDirectoryReference();
            CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("folderthatyouuploadto");
            CloudFile file = sampleDir.GetFileReference(FileUpload1.FileName);

            Stream fileStream = FileUpload1.PostedFile.InputStream;

            file.UploadFromStream(fileStream);
            fileStream.Dispose();


        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接