How to use StartCopyFromBlob between different accounts?

我的梦境 提交于 2019-12-10 19:36:46

问题


I am using this code to copy blobs from one account to another... but it throws an exception.

var srcAccount = CloudStorageAccount.Parse("connection string 1");
var dstAccount = CloudStorageAccount.Parse("connection string 2");

var srcBlobClient = srcAccount.CreateCloudBlobClient();
var dstBlobClient = dstAccount.CreateCloudBlobClient();

foreach (var srcCloudBlobContainer in srcBlobClient.ListContainers())
{
    var dstCloudBlobContainer = dstBlobClient
        .GetContainerReference(srcCloudBlobContainer.Name);

    dstCloudBlobContainer.CreateIfNotExists();

    foreach (var srcBlob in srcCloudBlobContainer.ListBlobs())
    {
        if (srcBlob.GetType() == typeof(CloudBlockBlob))
        {
            var srcBlockBlock = (CloudBlockBlob)srcBlob;
            var dstBlockBlock = dstCloudBlobContainer
                .GetBlockBlobReference(srcBlockBlock.Name);

            // throws exception StorageException:
            // The remote server returned an error: (404) Not Found.
            dstBlockBlock.StartCopyFromBlob(srcBlockBlock.Uri);
        }
    }
}

Microsoft states that cross account copy is supported, but I cannot get it to work.

What am I doing wrong?


回答1:


Can you check the source blob container's ACL? If it's Private you may either need to change the ACL to Public / Blob or create a SAS URL. You can use the following code if you wish to keep your blob container's ACL as Private and make use of SAS URL:

var srcAccount = CloudStorageAccount.Parse("connection string 1");
var dstAccount = CloudStorageAccount.Parse("connection string 2");

            var srcBlobClient = srcAccount.CreateCloudBlobClient();
            var dstBlobClient = dstAccount.CreateCloudBlobClient();

            foreach (var srcCloudBlobContainer in srcBlobClient.ListContainers())
            {
                var dstCloudBlobContainer = dstBlobClient
                    .GetContainerReference(srcCloudBlobContainer.Name);

                dstCloudBlobContainer.CreateIfNotExists();

                //Assuming the source blob container ACL is "Private", let's create a Shared Access Signature with
                //Start Time = Current Time (UTC) - 15 minutes to account for Clock Skew
                //Expiry Time = Current Time (UTC) + 7 Days - 7 days is the maximum time allowed for copy operation to finish.
                //Permission = Read so that copy service can read the blob from source
                var sas = srcCloudBlobContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy()
                {
                    SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
                    SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7),
                    Permissions = SharedAccessBlobPermissions.Read,
                });
                foreach (var srcBlob in srcCloudBlobContainer.ListBlobs())
                {
                    if (srcBlob.GetType() == typeof(CloudBlockBlob))
                    {
                        var srcBlockBlock = (CloudBlockBlob)srcBlob;
                        var dstBlockBlock = dstCloudBlobContainer
                            .GetBlockBlobReference(srcBlockBlock.Name);
                        //Create a SAS URI for the blob
                        var srcBlockBlobSasUri = string.Format("{0}{1}", srcBlockBlock.Uri, sas);
                        // throws exception StorageException:
                        // The remote server returned an error: (404) Not Found.
                        dstBlockBlock.StartCopyFromBlob(new Uri(srcBlockBlobSasUri));
                    }
                }
            }


来源:https://stackoverflow.com/questions/17081415/how-to-use-startcopyfromblob-between-different-accounts

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