我正在创建一个控制台应用程序,它将所有容器中的所有blob从我们用于生产的帐户复制到我们用于开发的另一个帐户。我有以下方法可以做到这一点。'productionStorage'和'developmentStorage'对象位于另一个存储Azure存储客户端方法的程序集中。
static void CopyBlobsToDevelopment()
{
// Get a list of containers in production
List<CloudBlobContainer> productionBlobContainers = productionStorage.GetContainerList();
// For each container in production...
foreach (var productionContainer in productionBlobContainers)
{
// Get a list of blobs in the production container
var blobList = productionStorage.GetBlobList(productionContainer.Name);
// Need a referencee to the development container
var developmentContainer = developmentStorage.GetContainer(productionContainer.Name);
// For each blob in the production container...
foreach (var blob in blobList)
{
CloudBlockBlob targetBlob = developmentContainer.GetBlockBlobReference(blob.Name);
targetBlob.StartCopyFromBlob(new Uri(blob.Uri.AbsoluteUri));
}
}
}
我targetBlob.StartCopyFromBlob()
在行上遇到错误(404)。但是我不明白为什么会出现404错误。Blob确实存在于源(生产)中,我想将其复制到目标(开发)中。不知道我在做什么错。
由于源Blob容器的ACL为Private
,因此您需要做的是创建具有Read
权限的SAS令牌(在Blob容器上或在该容器中的各个Blob上),然后将此SAS令牌附加到Blob的URL上。请在下面查看修改后的代码:
static void CopyBlobsToDevelopment()
{
// Get a list of containers in production
List<CloudBlobContainer> productionBlobContainers = productionStorage.GetContainerList();
// For each container in production...
foreach (var productionContainer in productionBlobContainers)
{
//Gaurav --> create a SAS on source blob container with "read" permission. We will just append this SAS
var sasToken = productionContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddDays(1),
});
// Get a list of blobs in the production container
var blobList = productionStorage.GetBlobList(productionContainer.Name);
// Need a referencee to the development container
var developmentContainer = developmentStorage.GetContainer(productionContainer.Name);
// For each blob in the production container...
foreach (var blob in blobList)
{
CloudBlockBlob targetBlob = developmentContainer.GetBlockBlobReference(blob.Name);
targetBlob.StartCopyFromBlob(new Uri(blob.Uri.AbsoluteUri + sasToken));
}
}
}
我尚未尝试运行此代码,因此,如果您在使用此代码时遇到任何错误,请原谅。但希望您能明白。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句