我正在创建一个控制台应用程序,它将所有容器中的所有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确实存在于源(生产)中,我想将其复制到目标(开发)。不知道我做错了什么。
答案 0 :(得分:7)
因为源blob容器ACL是Private
,所以您需要做的是创建SAS令牌(在blob容器上或在该容器中的单个blob上)并Read
权限并附加这个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));
}
}
}
我没有尝试过运行此代码,所以如果您遇到此代码的任何错误,请原谅。但希望你明白了。