[英]Azure Blob Move folders inside same container C#
i have to move/copy from the source folder to destination folder, i'm following the below C# code, Does it even copy the folders too?我必须从源文件夹移动/复制到目标文件夹,我遵循下面的 C# 代码,它是否也复制文件夹? can you please confirm
你能确认一下吗
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);
Like the comments said, there is no folders' concept on Azure blob storage.正如评论所说,Azure blob 存储上没有文件夹的概念。 Have a look at the official document :
看看官方文档:
Blob storage offers three types of resources:
Blob 存储提供三种类型的资源:
- The storage account
存储帐户
- A container in the storage account
存储帐户中的容器
- A blob in a container
容器中的 Blob
You could Copy and move blobs from one container or storage account to another, have a look at this tutorial: Copy and move blobs from one container or storage account to another from the command line and in code您可以将Blob从一个容器或存储帐户复制并移动到另一个,请查看本教程: 从命令行和代码中将 Blob 从一个容器或存储帐户复制并移动到另一个
As Gaurav Mantri said in the comment, azure blob storage is actually a flat structure(there is no folder in it.).正如 Gaurav Mantri 在评论中所说,azure blob 存储实际上是一个扁平结构(里面没有文件夹。)。
'filepath'+'filename'='blobname'
But you can design code such as below to achieve your requirement:但是您可以设计如下代码来满足您的要求:
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++;
}
}
}
}
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.