简体   繁体   English

使用 C# 在 Azure 中的存储容器之间复制 blob

[英]Copy blobs between storage containers in Azure using C#

I wrote the following C# code for copying blobs between storage containers in Azure.我编写了以下 C# 代码,用于在 Azure 中的存储容器之间复制 blob。 It doesnt throw any error, but it doesnt give the output either.它不会抛出任何错误,但也不会给出输出。

static void TransferBlob(string accountName, string accountKey, string containerName, string targetContainerName)

    {
        CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
        CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer sourceContainer = cloudBlobClient.GetContainerReference(containerName);
        CloudBlobContainer targetContainer = cloudBlobClient.GetContainerReference(targetContainerName);
        CloudBlockBlob sourceBlob;
        CloudBlockBlob targetBlob;
        foreach (var blobItem in sourceContainer.ListBlobs())
        {
            sourceBlob = sourceContainer.GetBlockBlobReference(blobItem.Uri.ToString());
            targetBlob = targetContainer.GetBlockBlobReference(blobItem.Uri.ToString());
            targetBlob.StartCopy(sourceBlob);
        }
    }

You need to use useFlatBlobListing: true and blob.Name .您需要使用useFlatBlobListing: trueblob.Name

static void TransferBlob(string accountName, string accountKey, string sourceContainerName, string targetContainerName)
{
    CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer sourceContainer = cloudBlobClient.GetContainerReference(sourceContainerName);
    CloudBlobContainer targetContainer = cloudBlobClient.GetContainerReference(targetContainerName);
    if (sourceContainer.Exists() && targetContainer.Exists())
    {
        foreach (IListBlobItem item in sourceContainer.ListBlobs(useFlatBlobListing: true))
        {
            var blob = item as CloudBlockBlob;
            if (blob != null)
            {
                CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(blob.Name);
                CloudBlockBlob targetBlob = targetContainer.GetBlockBlobReference(blob.Name);
                targetBlob.StartCopy(sourceBlob);
            }
        }
    }
}

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

相关问题 C#+ Azure存储Blob:StorageException:当前有一个挂起的复制操作 - C# + Azure Storage blobs: StorageException: There is currently a pending copy operation 使用 Azure.Storage.Blobs 枚举大型 Azure BLOB 容器 - Enumerating large Azure BLOB containers using Azure.Storage.Blobs 如何复制Azure容器和Blob - How to Copy Azure Containers & Blobs 如何在容器之间复制blob? - How to copy blobs between containers? 如何使用 C# 中的 Azure.Storage.Blobs 以 ByteArray 格式从 Azure 存储 blob 中获取文件 - How to get file from Azure storage blob in a ByteArray format using Azure.Storage.Blobs in C# 使用新的 Azure.Storage.Blobs 时如何计算存储帐户中 Blob 存储容器的总大小 - How to calculate the total size of Blob storage containers in an storage account when using the new Azure.Storage.Blobs 在Azure存储Blob上使用.Net Core和C#将图像序列转换为MP4文件 - Convert image sequence to MP4 file using .Net Core and C# on Azure Storage Blobs 使用 C# 在多级文件夹结构中查找 Azure 存储容器中的 Blob - Find Blobs in Azure Storage Container in multi level folder structure using C# 使用 c# 代码在 azure 存储帐户 gen 2 之间复制大文件 - Copy large file between azure storage account gen 2 using c# code 在 c# 的私有访问下无法下载 azure 存储 blob - Trouble downloading azure storage blobs under private access in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM