Unable to copy blobs from one container to another

纵饮孤独 提交于 2019-12-01 04:47:01

问题


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.


回答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:

    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.



来源:https://stackoverflow.com/questions/30076886/unable-to-copy-blobs-from-one-container-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!