我正在尝试将一个Blob(特别是vhd)从存储帐户复制到另一个存储帐户。我正在编写的应用可以同时拥有两个存储帐户的所有者访问权限,并且可以在这些存储帐户上运行其他操作。vhd上没有租约(未附加VM)。我有以下代码,但收到403禁止响应。
StorageCredentials scSource = new StorageCredentials(SourceStorageName, strSourceStorageKey);
StorageCredentials scTarget = new StorageCredentials(TargetStorageName, strTargetStorageKey);
CloudStorageAccount csaSource = new CloudStorageAccount(scSource, true);
CloudStorageAccount csaTarget = new CloudStorageAccount(scTarget, true);
CloudBlobClient cbcSource = csaSource.CreateCloudBlobClient();
CloudBlobClient cbcTarget = csaTarget.CreateCloudBlobClient();
CloudBlobContainer bcSource = cbcSource.GetContainerReference(SourceContainer);
CloudBlobContainer bcTarget = cbcTarget.GetContainerReference(TargetContainer);
CloudBlob cbSource = bcSource.GetBlobReference(strSourceDiskName);
CloudBlob cbTarget = bcTarget.GetBlobReference(strTargetDiskName);
Task<string> tskCopy = cbTarget.StartCopyAsync(cbSource.Uri);
while (tskCopy.Status != TaskStatus.RanToCompletion)
{
if (tskCopy.Exception != null)
throw tskCopy.Exception;
Thread.Sleep(2500);
}
不知道为什么会这样,因为我能够通过其他工具(Cloud berry等)将Blob从一个存储帐户复制到另一个存储帐户。
感谢Gaurav Mantri向我指出正确的方向。查找共享访问签名后,我从Microsoft https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-shared-access-signature-part-2/上获得了这篇文章。
此代码是特定的:
SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write;
// Generate the shared access signature on the blob, setting the constraints directly on the signature.
string sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);
// Return the URI string for the container, including the SAS token.
return blob.Uri + sasBlobToken;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句