【问题标题】:Unable to copy blobs from one container to another无法将 blob 从一个容器复制到另一个容器
【发布时间】:2015-07-16 14:22:06
【问题描述】:

我正在创建一个控制台应用程序,它将所有容器中的所有 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 不是私有的。 HTH。
  • @GauravMantri - 谢谢。它是私人的。那么我需要在 StartCopyFromBlob 调用中提供凭据吗?

标签: azure azure-storage azure-blob-storage


【解决方案1】:

由于源 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));
            }
        }
    }

我没有尝试运行此代码,因此如果您在此代码中遇到任何错误,请见谅。但希望你能明白。

【讨论】:

  • 谢谢。有机会我会尽快测试。
猜你喜欢
  • 2020-03-29
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 2020-03-26
  • 1970-01-01
  • 2020-05-07
相关资源
最近更新 更多