I'm trying to copy a blob from one Azure storage account to another storage account (same subscription) but I get an exception:
The specified resource does not exist.
As I've realised, the problem is caused by the container being set to Private
, which is what it should be for our business purposes.
I've taken my cue from this SO answer which suggests:
Create a Shared Access Signature (SAS) on the source blob with at least Read permission and an expiry date of at least 15 days and use that SAS URL (blob URL + SAS token) as copy source.
Furthermore, I've found some code on the Microsoft forums from the same poster where he posts code that worked for him. From that, I now have the following code (ct
is a CancellationToken
):
var snapshot = await blob.CreateSnapshotAsync(ct);
var sourceSAS = snapshot.GetSharedAccessSignature(new SharedAccessBlobPolicy {
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.Now.AddYears(1),
SharedAccessStartTime = DateTimeOffset.Now.AddHours(-1)
});
var sourceUri = $"{snapshot.Uri}{sourceSAS}";
var backupBlob = FileBackupContainer.GetBlockBlobReference(blob.Name);
var targetSAS = backupBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy {
Permissions = SharedAccessBlobPermissions.Write,
SharedAccessExpiryTime = DateTime.Now.AddYears(1),
SharedAccessStartTime = DateTimeOffset.Now.AddHours(-1)
});
var targetUri = $"{backupBlob.Uri}{targetSAS}";
var newTargetBlob = new CloudBlockBlob(new Uri(targetUri));
await newTargetBlob.StartCopyAsync(new Uri(sourceUri), ct);
await snapshot.DeleteAsync(ct);
But I still encounter the same exception.
What more do I need in order to copy a blob using its SAS?
UPDATE:
My code wasn't working because the source blob was in my local storage emulator (i.e. localhost) which is obviously inaccessible to the target storage container which is Azure-hosted.