【问题标题】:ASP.NET Core, Azure storage controllerASP.NET Core,Azure 存储控制器
【发布时间】:2020-03-29 19:31:06
【问题描述】:

我有一个非常新手的问题。

我正在关注此文档“Azure Blob 存储客户端库 v12 for .NET”-https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet

当我在我的控制台和 Azure 存储上进行测试时,它可以正常工作。

但我想知道是否可以使用建议的“主要”方法制作控制器? 因为我希望当用户输入从前端更改时启动这些获取和发布到服务器的操作。

这是 Program.cs 内部的 Main 方法基于文档的样子

        static async Task Main()
        {
            Console.WriteLine("Azure Blob storage v12 - .NET quickstart sample\n");

            string connectionString = Environment.GetEnvironmentVariable("My_CONNECTION_STRING");


            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            string containerName = "quickstartblobs" + Guid.NewGuid().ToString();

            BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName);


            string localPath = "./data/";
            string fileName = "quickstart" + Guid.NewGuid().ToString() + ".txt";
            string localFilePath = Path.Combine(localPath, fileName);

            // Write text to the file
            await File.WriteAllTextAsync(localFilePath, "Hello, World!");

            // Get a reference to a blob
            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n", blobClient.Uri);

            // Open the file and upload its data
            using FileStream uploadFileStream = File.OpenRead(localFilePath);
            await blobClient.UploadAsync(uploadFileStream, true);
            uploadFileStream.Close();

            Console.WriteLine("Listing blobs...");

            // List all blobs in the container
            await foreach (BlobItem blobItem in containerClient.GetBlobsAsync())
            {
                Console.WriteLine("\t" + blobItem.Name);
            }
            Console.Write("Press any key to begin clean up");
            Console.ReadLine();

            string downloadFilePath = localFilePath.Replace(".txt", "DOWNLOAD.txt");

            Console.WriteLine("\nDownloading blob to\n\t{0}\n", downloadFilePath);

            // Download the blob's contents and save it to a file
            BlobDownloadInfo download = await blobClient.DownloadAsync();

            using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath))
            {
                await download.Content.CopyToAsync(downloadFileStream);
                downloadFileStream.Close();
            }
        }

所以比如在我的 HomeController 中我能不能把帖子相关的函数用作

            [HttpPost]
            public void Post([FromBody]string value)
            {
                //Create a unique name for the container
                string containerName = "filedata" + Guid.NewGuid().ToString();

                // Create the container and return a container client object
                BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName);

                // Create a local file in the ./data/ directory for uploading and downloading
                string localPath = "./data/";
                string fileName = "textfile" + Guid.NewGuid().ToString() + ".txt";
                string localFilePath = Path.Combine(localPath, fileName);

            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            // Get a reference to a blob
            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n", blobClient.Uri);

            // Open the file and upload its data
            using FileStream uploadFileStream = File.OpenRead(localFilePath);
            await blobClient.UploadAsync(uploadFileStream, true);
            uploadFileStream.Close();


            }

或者这是不行的?

感谢您帮助这个超级新手!

【问题讨论】:

    标签: asp.net-core azure-blob-storage


    【解决方案1】:

    例如,在我的 HomeController 中,我可以使用与帖子相关的功能吗?或者这是不行的?

    是的,你可以实现它。

    您可以使用邮递员在本地发送post请求进行测试。请记住删除 SSL 以进行网络服务器设置。

    另外,将public void Post 更改为public async Task Post 并在代码中删除using:

     FileStream uploadFileStream = File.OpenRead(localFilePath);
     await blobClient.UploadAsync(uploadFileStream, true);
     uploadFileStream.Close()
    

    【讨论】:

    • 非常感谢!关于这个我能再问你一个问题吗?我使用 ms doc 指南创建了一个 HomeController,但我收到错误消息,它说我不能在我创建的单独方法下使用“文件”(其中包括 uploadFileStream),因为它在给定的上下文中无效。 - 这是带有代码的stackoverflow,stackoverflow.com/questions/60927331/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2018-11-26
    相关资源
    最近更新 更多