简体   繁体   English

使用rest api从Web应用程序上传文件到Azure文件存储

[英]upload files to Azure file storage from web app using rest api

I have a web app that is currently using webforms, not MVC, which is going to be hosted on the Azure platform.我有一个 Web 应用程序,当前使用的是 webforms,而不是 MVC,它将托管在 Azure 平台上。

The main function of this web app is to upload user files to Azure File Storage.此 Web 应用程序的主要功能是将用户文件上传到 Azure 文件存储。

The files may be pdf, mp3, etc., not simple text or data stream or data input.文件可能是pdf、mp3等,不是简单的文本或数据流或数据输入。

I am told to use Azure REST API to upload files, but I am really not familiar with it and can't find a good sample or tutorial or video online.有人告诉我使用 Azure REST API 上传文件,但我真的不熟悉它,并且在网上找不到好的示例或教程或视频。 The current documents from Microsoft reads like ??????来自微软的当前文件读起来像 ?????? to me.给我。

Currently I just upload to a local folder, so the code looks like: FileUpload1.PostedFile.SaveAs(Server.MapPath("fileupload\\\\" + FileUpload1.FileName));目前我只是上传到本地文件夹,所以代码看起来像: FileUpload1.PostedFile.SaveAs(Server.MapPath("fileupload\\\\" + FileUpload1.FileName)); in C#;在 C# 中;

Where do I go from there?我从那里去哪里? I think I am supposed to add a StorageConnectionString which looks like DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=yyy , which I already have.我想我应该添加一个 StorageConnectionString ,它看起来像DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=yyy ,我已经有了。

And then I should write some code like 'post' in C#?然后我应该在 C# 中写一些像“post”这样的代码吗?

Azure provide a nuget library that you can use to upload, and do other "file management" types of activities on Azure File Storage. Azure 提供了一个 nuget 库,可用于在 Azure 文件存储上上传和执行其他“文件管理”类型的活动。

The library is called: WindowsAzure.Storage该库名为: WindowsAzure.Storage

UPDATE: The new library to use is Azure.Storage.Blobs .更新:要使用的新库是Azure.Storage.Blobs

Here are the basics of getting this going:以下是实现这一目标的基础知识:

//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();

More links and info here (note scroll a fair way down for samples): https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-files/更多链接和信息在这里(注意向下滚动示例): https : //azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-files/

This piece of code is based on the answer I get from Gary Holland above.这段代码基于我从上面的 Gary Holland 那里得到的答案。 I hope other people benefit from it.我希望其他人从中受益。 I am not good at programming, hopefully the code looks ok.我不擅长编程,希望代码看起来不错。

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();


        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将发布在REST API中的文件上传到Azure存储 - How to upload a file posted in REST API to Azure Storage 通过 web api 将大文件(> 1 GB)上传到 azure blob 存储 - upload large files (> 1 GB) to azure blob storage through web api 使用 Web Api 将文件上传到 Azure blob - Upload files to Azure blob using Web Api 将从Web服务项目生成的文件上载到Azure Blob存储 - Upload a file generated from Web Services project to Azure Blob Storage 如何从Azure Web App本地存储中删除文件? - How to delete a file from Azure Web App local storage? C#Azure将文件上载到“文件存储服务” - 而不是Blob存储 - C# Azure Upload Files to “File Storage Service” - not Blob Storage 如何在 Unity ZD7EFA19FBE7D2972FDZADB60274 中使用 rest api 将文件上传到 firebase 存储 - How to upload files to firebase storage using rest api in Unity C# 使用asp.net Web API 2将媒体上传到Azure Blob存储 - upload media to azure blob storage using asp.net web api 2 Uploading a PDF File to Azure Blob Storage using REST API & C# (Without using any AZURE STORAGE SDKs) - Uploading a PDF File to Azure Blob Storage using REST API & C# (Without using any AZURE STORAGE SDKs) 使用c#从azure文件存储中递归获取文件 - Get files recursively from azure file storage using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM