How to use StartCopyFromBlob between different acc

2019-07-04 19:20发布

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条回答
老娘就宠你
2楼-- · 2019-07-04 19:33

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));
                    }
                }
            }
查看更多
登录 后发表回答