Ran*_*der 4 azure azure-storage azure-storage-blobs
我正在创建一个控制台应用程序,它将所有容器中的所有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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误(404)targetBlob.StartCopyFromBlob().但我不明白为什么我会收到404错误.blob确实存在于源(生产)中,我想将其复制到目标(开发).不知道我做错了什么.
由于源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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我没有尝试运行此代码,所以如果您遇到此代码的任何错误,请原谅.但希望你能得到这个想法.