简体   繁体   English

Azure Blob 在同一容器内移动文件夹 C#

[英]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.

相关问题 Azure 容器内所有文件夹的 blob 总数 - Total blob count of all folders inside an Azure container 使用 C# 获取存储在 azure 容器中的音频 blob 的持续时间 - Get the duration of audio blob stored in azure container using C# 在使用 C# 代码创建 Blob 容器时创建和部署 Azure 函数 - Create and Deploy an Azure Function at Blob Container Creation in C# Code Azure 存储将 blob 移动到其他容器 - Azure Storage move blob to other container 使用 c# 将文件从 azure 文件共享移动到 blob 存储 - Move files from azure file shares into blob storage by using c# 如何使用C#从Azure容器/ Blob中读取/下载所有文件? - How to read/Download all files from a Azure container/Blob using C#? 如何使用 c# 获取 Azure Blob 存储容器中现有目录的列表? - How to get a list of existing directories in Azure Blob Storage container using c#? 如何从 Azure Blob 存储 C# 中的文件 URL 获取容器名称 - How to get container name from file URL in Azure Blob Storage C# 使用 C# 将 JSON 字符串直接添加到 Azure Blob Storage Container - Add JSON string directly to Azure Blob Storage Container using C# 将运行 docker 容器的 C# 单元测试结果发布到 azure blob 存储 - Publish C# unit test results from running docker container to azure blob storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM