gpt4 book ai didi

c# - 递归上传到Azure文件/创建子文件夹

转载 作者:行者123 更新时间:2023-12-03 02:47:46 26 4
gpt4 key购买 nike

我想将文件夹递归上传到 azure 文件存储。文件结构通常有几个子文件夹。在 azure-files 中创建子文件夹的最佳方法是什么?

    foreach (string fullLocalFilename in System.IO.Directory.GetFiles(locationOfFolderToUpload, "*.*", System.IO.SearchOption.AllDirectories))
{
Console.WriteLine(fullLocalFilename);
FileInfo localfile = new FileInfo(fullLocalFilename);
var root = share.GetRootDirectoryReference();
string strPfad = localfile.DirectoryName.Substring(3);
var folder = root.GetDirectoryReference(strPfad);
Console.WriteLine(strPfad);
folder.CreateIfNotExists();
CloudFile file = folder.GetFileReference(localfile.Name);
if (file.Exists() == false) {
file.Create(localfile.Length);
file.UploadFromFile(fullLocalFilename);
Console.WriteLine(fullLocalFilename);
}
}

WebException:远程服务器返回错误:(404) 未找到。

最佳答案

我建议您使用Microsoft.Azure.Storage.DataMovement ,它支持上传目录到azure以及创建与本地路径相同的结构。请安装最新版本1.0.0 of Microsoft.Azure.Storage.DataMovement here 。请注意,如果您安装了其他azure storage sdk,请先卸载它们。

例如,如果我有一个如下所示的本地文件夹:

enter image description here

使用下面的代码:

using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Azure.Storage.DataMovement;
using Microsoft.Azure.Storage.File;

namespace AzureDataMovementTest
{
class Program
{
static void Main(string[] args)
{
string storageConnectionString = "xxxx";
CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);

CloudFileClient fileClient = account.CreateCloudFileClient();
CloudFileShare fileShare = fileClient.GetShareReference("t22");
fileShare.CreateIfNotExists();

CloudFileDirectory fileDirectory= fileShare.GetRootDirectoryReference();

//here, I want to upload all the files and subfolders in the follow path.
string source_path = @"F:\temp\1";

//if I want to upload the folder 1, then use the following code to create a file directory in azure.
CloudFileDirectory fileDirectory_2 = fileDirectory.GetDirectoryReference("1");
fileDirectory_2.CreateIfNotExists();



UploadDirectoryOptions directoryOptions = new UploadDirectoryOptions
{
Recursive = true
};

var task = TransferManager.UploadDirectoryAsync(source_path,fileDirectory_2,directoryOptions,null);
task.Wait();

Console.WriteLine("the upload is completed");
Console.ReadLine();
}

}
}

代码运行完成后,导航到azure门户->文件存储:

enter image description here

如果您有更多问题,请告诉我。

关于c# - 递归上传到Azure文件/创建子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57837535/

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