Unable to copy blobs from one container to another

后端 未结 1 707
傲寒
傲寒 2021-01-13 18:56

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 d

相关标签:
1条回答
  • 2021-01-13 19:27

    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:

        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.

    0 讨论(0)
提交回复
热议问题