Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to copy blobs from one container to another

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.

    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.

like image 223
Randy Minder Avatar asked Mar 16 '23 04:03

Randy Minder


1 Answers

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.

like image 176
Gaurav Mantri Avatar answered Mar 22 '23 23:03

Gaurav Mantri