Reputation: 7449
I 'm saving the uri of the file in the database in this form:
https://app.blob.core.windows.net/container/Accounts/Images/acc.jpg
But to delete it I need to pass only the blob name, when I try this
CloudBlockBlob blockBlob = Container.GetBlockBlobReference(uri);
The blob's full uri
becomes:
https://app.blob.core.windows.net/container/https://app.blob.core.windows.net/container/Accounts/Images/acc.jpg
So I get 404
error (not found),
I can do some trimming to the uri
but that doesn't seem efficient.
so is there a way to delete a blob/ get reference by its full URI?
Upvotes: 10
Views: 24418
Reputation: 2072
You can now use the BlobUriBuilder
class to safely get the blob name from the URI without the need for string parsing or other methods.
BlobUriBuilder blobUriBuilder = new BlobUriBuilder(new Uri(blobUri));
var sourceBlob = container.GetBlobClient(blobUriBuilder.BlobName);
Upvotes: 7
Reputation: 143
Your problem is that you're putting the URI string inside the blob name with this function GetBlockBlobReference
as defined: public virtual CloudBlockBlob GetBlockBlobReference(string blobName);
. You will use moondaisy solution's.
Upvotes: -2
Reputation: 223
I did face similar issue , since i already had valid container reference this worked for me :
CloudBlockBlob blockblob = container.GetBlockBlobReference(new CloudBlockBlob(blobUri).Name);
Upvotes: 18
Reputation: 4481
It is possible to do this creating the CloudBlockBlob
with this constructor:
public CloudBlockBlob(Uri blobAbsoluteUri)
In your case, assuming uri is of type Uri and not just a string:
CloudBlockBlob blob = new CloudBlockBlob(uri);
You might need to use your credentials if the blob isn't public or the uri doesn't contain SAS credentials (like to one you included). In that case you will need this constructor:
public CloudBlockBlob(Uri blobAbsoluteUri, StorageCredentials credentials)
As stated by Zhaoxing Lu - Microsoft on the comments,
Public access is read only access, you need to specify the storage account key or Shared Access Signature for deleting operation.
Upvotes: 9