将blob从一个目录复制到同一容器Azure Storage中的另一个目录

时间:2018-05-07 09:45:18

标签: c# azure-storage-blobs

问题

我正在尝试将blob从一个目录复制到另一个目录并从源中删除blob。我已经尝试了以下代码,但没有复制并过去到另一个文件夹已完成,但删除操作有效。

代码

var cred = new StorageCredentials("storageName", "Key");
var account = new CloudStorageAccount(cred, true);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("containerName");
CloudBlockBlob sourceBlob = container.GetBlockBlobReference("folder/test.jpg");

//this doesn't work
sourceBlob.StartCopyAsync(new Uri("destination url"));
//this works
sourceBlob.DeleteAsync();

2 个答案:

答案 0 :(得分:3)

  • 你需要{I}提及的<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script> <div class="modal fade" id="cartConfirm" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-body"> <div class="container"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> <div class="row"> <div class="col-lg-12 confrm-cart-box"> <div class="close-btn" data-dismiss="modal"><i class="fa fa-times"></i></div> <h3>order confirmation</h3> <br> <br> <p>Order number <span class="green">123456</span> is now confirmed<br> You can check your order and its progress at any time.<br> By logging into your account</p> <a href="#" data-toggle="modal" data-target="#multi-login" data-dismiss="modal">Log In</a> <p>We have emailed you the order details for your convenience.</p> <p>If you have any queries please contact us.</p> <p><span class="green">This order is covered by our 100% No Quibble Money Back Guarantee</span></p> </div> </div> </div> </div> </div> </div> </div> <button data-toggle="modal" data-dismiss="modal" data-target="#cartConfirm">BUY</button>

  • await只是一种“启动”复制过程的方法,您仍需要在删除blob之前监视复制状态。有关详细信息,请在此处找到我的答案:How to get updated copy state of azure blob when using blob StartCopyAsync

答案 1 :(得分:2)

嗯,你必须等待async方法。 StartCopyAsync失败,因为您同时运行删除文件调用。尝试使用awaitResult属性。

var cred = new StorageCredentials("storageName", "Key");
var account = new CloudStorageAccount(cred, true);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("containerName");
CloudBlockBlob sourceBlob = container.GetBlockBlobReference("folder/test.jpg");

//this doesn't work
await sourceBlob.StartCopyAsync(new Uri("destination url"));
//this works
await sourceBlob.DeleteAsync();

代码未经过测试,但应该给您一个大致的想法。