无法将 blob 从一个容器复制到另一个容器
Unable to copy blobs from one container to another
我正在创建一个控制台应用程序,它将所有容器中的所有 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));
}
}
}
我还没有尝试 运行 使用此代码,所以如果您 运行 使用此代码有任何错误,请原谅。但希望你明白了。
我正在创建一个控制台应用程序,它将所有容器中的所有 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));
}
}
}
我还没有尝试 运行 使用此代码,所以如果您 运行 使用此代码有任何错误,请原谅。但希望你明白了。