简体   繁体   English

上传文件到 Azure 文件存储

[英]Upload file to Azure File Storage

I am trying to upload a file to my Azure File Storage account.我正在尝试将文件上传到我的 Azure 文件存储帐户。

This is my code:这是我的代码:

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("myConnString");

        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

        CloudFileShare share = fileClient.GetShareReference("myFileStorage");

        if (await share.ExistsAsync())
        {
            CloudFileDirectory rootDir = share.GetRootDirectoryReference();
            CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("/folder1/folder2/");
            CloudFile file = sampleDir.GetFileReference("fileName.jpg");

            using Stream fileStream = new MemoryStream(data);

            await file.UploadFromStreamAsync(fileStream);
        }

I am getting this error:我收到此错误:

The specified parent path does not exist.指定的父路径不存在。

After this line:在这一行之后:

CloudFile file = sampleDir.GetFileReference("fileName");

the file has this uri:file有这个uri:

https://myFileStorage.file.core.windows.net/myFileStorage/folder1/folder2/fileName.jpg

ie as expected.即如预期的那样。

Currently my file storage is empty, there are no files/folders.目前我的文件存储是空的,没有文件/文件夹。 How do I create my custom folders if they do not already exist?如果自定义文件夹不存在,如何创建它们?

If you're using WindowsAzure.Storage, version 9.3.3 and create only one directory(no sub-directories), you can directly use CreateIfNotExistsAsync() method to create a directory.如果您使用的是WindowsAzure.Storage,版本 9.3.3并且只创建一个目录(没有子目录),您可以直接使用CreateIfNotExistsAsync()方法创建一个目录。

But here is one thing you should remember, for file share, the SDK does not support create a directory which contains subdirectories, like "folder1/folder2" in your case.但是您应该记住一件事,对于文件共享,SDK 不支持创建包含子目录的目录,例如在您的情况下为“folder1/folder2”。 The solution for that is create these directories one by one.解决方案是一个一个地创建这些目录。

Here is the sample code for your case, create a directory with sub-directories:这是您的案例的示例代码,创建一个包含子目录的目录:

        static async void UploadFiles()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection_string");

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare share = fileClient.GetShareReference("file_share");

            if (await share.ExistsAsync())
            {
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();

                //define your directories like below:
                var myfolder = "folder1/folder2";

                var delimiter = new char[] { '/' };
                var nestedFolderArray = myfolder.Split(delimiter);
                for (var i = 0; i < nestedFolderArray.Length; i++)
                {
                    rootDir = rootDir.GetDirectoryReference(nestedFolderArray[i]);
                    await rootDir.CreateIfNotExistsAsync();
                    Console.WriteLine(rootDir.Name + " created...");
                }


                CloudFile file = rootDir.GetFileReference("fileName.jpg");

                byte[] data = File.ReadAllBytes(@"file_local_path");
                Stream fileStream = new MemoryStream(data);
                await file.UploadFromStreamAsync(fileStream);
            }

        }

The test result=> directories are created and file uploaded into azure:测试结果=> 目录创建,文件上传到azure:

在此处输入图像描述

You can make use of CloudFileDirectory.BeginCreateIfNotExists method您可以使用CloudFileDirectory.BeginCreateIfNotExists方法

More information 更多信息

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM