无法将blob从一个容器复制到另一个容器

时间:2022-09-24 19:09:46

I am creating a console app that copies all blobs in all containers from an account we use for production to another we use for development. I have the following method to do this. The 'productionStorage' and 'developmentStorage' objects are in another assembly where Azure storage client methods are housed.

我正在创建一个控制台应用程序,它将所有容器中的所有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));
            }
        }
    }

I'm getting an error (404) on the targetBlob.StartCopyFromBlob() line. But I don't understand why I'm getting a 404 error. The blob does exist in the source (production) and I want to copy it to the destination (development). Not sure what I'm doing wrong.

我在targetBlob.StartCopyFromBlob()行上收到错误(404)。但我不明白为什么我会收到404错误。 blob确实存在于源(生产)中,我想将其复制到目标(开发)。不知道我做错了什么。

1 个解决方案

#1


Because the source blob container ACL is Private, what you would need to do is create a SAS Token (either on the blob container or on individual blobs in that container) with Read permission and append this SAS token to your blob's URL. Please see modified code below:

因为源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));
            }
        }
    }

I haven't tried running this code so please excuse me if you run into any errors with this code. But hopefully you get the idea.

我没有尝试运行此代码,所以如果您遇到此代码的任何错误,请原谅。但希望你能得到这个想法。

#1


Because the source blob container ACL is Private, what you would need to do is create a SAS Token (either on the blob container or on individual blobs in that container) with Read permission and append this SAS token to your blob's URL. Please see modified code below:

因为源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));
            }
        }
    }

I haven't tried running this code so please excuse me if you run into any errors with this code. But hopefully you get the idea.

我没有尝试运行此代码,所以如果您遇到此代码的任何错误,请原谅。但希望你能得到这个想法。