C# Azure Blob在同一容器C中移动文件夹#
我必须从源文件夹移动/复制到目标文件夹,我遵循下面的C代码,它是否也复制文件夹?你能确认一下吗C# Azure Blob在同一容器C中移动文件夹#,c#,azure,azure-blob-storage,C#,Azure,Azure Blob Storage,我必须从源文件夹移动/复制到目标文件夹,我遵循下面的C代码,它是否也复制文件夹?你能确认一下吗 CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connectionstring"); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); var sourceContainer = blobClient.GetCo
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connectionstring");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
var sourceContainer = blobClient.GetContainerReference("container");
var destContainer = blobClient.GetContainerReference("container");
var sourceFilePath = "hhh/www/";
var destFilePath = "hhh/jjj/";
CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(sourceFilePath);
CloudBlockBlob destBlob = destContainer.GetBlockBlobReference(destFilePath);
await destBlob.StartCopyAsync(sourceBlob);
正如评论所说,Azure blob存储上没有文件夹的概念。请看一下: Blob存储提供三种类型的资源:
- 存储帐户
- 存储帐户中的容器
- 容器中的水滴
正如Gaurav Mantri在评论中所说,azure blob存储实际上是一个平面结构(其中没有文件夹)
'filepath'+'filename'='blobname'
但您可以设计如下代码来满足您的需求:
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System;
using System.Collections.Generic;
using System.IO;
namespace Copyfiles
{
class Program
{
static void Main(string[] args)
{
string sourceconnectionString = "DefaultEndpointsProtocol=xxxxxxcore.windows.net";
string sinkconnectionString = "DefaultEndpointsProtocol=xxxxxxcore.windows.net";
string sourcefilepath = "source/f1";
string sinkfilepath = "sink/f1";
BlobServiceClient sourceblobServiceClient = new BlobServiceClient(sourceconnectionString);
BlobServiceClient sinkblobServiceClient = new BlobServiceClient(sinkconnectionString);
//Copy files.
Copyfiles(sourceblobServiceClient, sinkblobServiceClient, sourcefilepath, sinkfilepath);
}
static List<string> Getfilesname(Pageable<BlobItem> blobs, List<string> subs3)
{
//Below code can get the path.
foreach (var blob in blobs)
{
string[] sub_names = blob.Name.Split('/');
if (sub_names.Length > 1 && !subs3.Contains(sub_names[sub_names.Length - 1]))
{
subs3.Add(sub_names[sub_names.Length - 1]);
}
}
return subs3;
}
static string[] Getcontainernameandblobnameprefix(string folderpath)
{
string test = folderpath;
//
string[] sub_names = test.Split('/');
string containername = sub_names[0];
string blobnameprefix = "";
for (int i = 0; i < sub_names.Length; i++)
{
if (i == 0)
{
}
else
{
blobnameprefix = blobnameprefix + sub_names[i] + "/";
}
}
string[] all = new string[2];
all[0] = containername;
all[1] = blobnameprefix;
return all;
}
static string[] Getcontainernameandblobnameprefix2(string folderpath)
{
string test = folderpath;
//
string[] sub_names = test.Split('/');
string containername = sub_names[0];
string blobnameprefix = "";
for (int i = 0; i < sub_names.Length; i++)
{
if (i == 0)
{
}
else
{
blobnameprefix = blobnameprefix + sub_names[i] + "/";
}
}
string[] all = new string[2];
all[0] = containername;
all[1] = blobnameprefix;
return all;
}
static string getsuffixblobnameofsinkblob(string sourcepath, string sourceblobname)
{
string suffixblobname = "";
string[] sourcepatharray = sourcepath.Split("/");
int needtodelete = sourcepatharray.Length - 1;
string[] spdblobname = sourceblobname.Split('/');
for (int i = 0 + needtodelete; i < spdblobname.Length; i++)
{
if (i == spdblobname.Length - 1)
{
suffixblobname = suffixblobname + spdblobname[i];
}
else
{
suffixblobname = suffixblobname + spdblobname[i] + "/";
}
}
return suffixblobname;
}
static void Copyfiles(BlobServiceClient sourceblobServiceClient, BlobServiceClient sinkblobServiceClient, string sourcefilepath, string sinkfilepath)
{
string sourcecontainername = Getcontainernameandblobnameprefix(sourcefilepath)[0];
string sourceblobnameprefix = Getcontainernameandblobnameprefix(sourcefilepath)[1];
string sinkcontainername = Getcontainernameandblobnameprefix(sinkfilepath)[0];
string sinkblobnameprefix = Getcontainernameandblobnameprefix(sinkfilepath)[1];
List<string> sourcefiles = new List<string>();
BlobContainerClient sourcecontainerClient = sourceblobServiceClient.GetBlobContainerClient(sourcecontainername);
BlobContainerClient sinkcontainerClient = sinkblobServiceClient.GetBlobContainerClient(sinkcontainername);
Pageable<BlobItem> sourceblobs = sourcecontainerClient.GetBlobs(prefix: sourceblobnameprefix);
List<string> sourcefilesname = Getfilesname(sourceblobs, sourcefiles);
//Below code can get the file name.
foreach (var sourcefile in sourcefiles)
{
Console.WriteLine("----");
Console.WriteLine(sourcefile);
Console.WriteLine("----");
}
int i = 0;
foreach (var sourceblob in sourceblobs)
{
var sourceblobclient = sourcecontainerClient.GetBlobClient(sourceblob.Name);
BlobDownloadInfo download = sourceblobclient.Download();
Stream sourcestream = download.Content;
var sinkblobclient = sinkcontainerClient.GetBlobClient(sinkblobnameprefix + getsuffixblobnameofsinkblob(sourcefilepath, sourceblob.Name));
sinkblobclient.Upload(sourcestream);
i++;
}
}
}
}
使用Azure;
使用Azure.Storage.Blobs;
使用Azure.Storage.Blobs.Models;
使用制度;
使用System.Collections.Generic;
使用System.IO;
命名空间复制文件
{
班级计划
{
静态void Main(字符串[]参数)
{
string sourceconnectionString=“DefaultEndpointsProtocol=xxxxxx core.windows.net”;
字符串sinkconnectionString=“DefaultEndpointsProtocol=xxxxxx core.windows.net”;
字符串sourcefilepath=“source/f1”;
字符串sinkfilepath=“sink/f1”;
BlobServiceClient sourceblobServiceClient=新BlobServiceClient(sourceconnectionString);
BlobServiceClient-sinkblobServiceClient=新BlobServiceClient(sinkconnectionString);
//复制文件。
复制文件(sourceblobServiceClient、sinkblobServiceClient、sourcefilepath、sinkfilepath);
}
静态列表Getfilesname(可分页blob、列表subs3)
{
//下面的代码可以得到路径。
foreach(blob中的var blob)
{
string[]sub_names=blob.Name.Split('/');
if(sub_names.Length>1&!subs3.Contains(sub_names[sub_names.Length-1]))
{
sub3.添加(sub_名称[sub_名称.Length-1]);
}
}
返回SUB3;
}
静态字符串[]GetContainerNamedBlobNamePrefix(字符串folderpath)
{
字符串测试=folderpath;
//
string[]sub_name=test.Split('/');
字符串containername=sub_名称[0];
字符串blobnameprefix=“”;
对于(int i=0;i<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.3.0" />
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.6.1" />
</ItemGroup>
</Project>