[英]C# + Azure Storage blobs: StorageException: There is currently a pending copy operation
[英]Copy blobs between storage containers in Azure using C#
我编写了以下 C# 代码,用于在 Azure 中的存储容器之间复制 blob。 它不会抛出任何错误,但也不会给出输出。
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);
}
}
您需要使用useFlatBlobListing: true
和blob.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.