Verifying CloudBlob.UploadFromStream compleated with no errors? - azure

I want to save files users upload to my site into my Azure Blob and I am using the CloudBlob.UploadFromStream method to do so but I want to make sure the file completed saving to the blob with no problems before doing some more work. I am currently just uploading the blob then checking to see if a reference to the new blob exists using GetBlockBlobReference inside an if statement. Is there a better way of verifying the upload completed fine?

If there's any problem while uploading the blob, CloudBlob.UploadFromStream method would throw an error so that would be the first place to check if the upload went fine.
I don't think creating a reference for a blob using GetBlockBlobReference would do you any good as it just creates an instance of CloudBlockBlob. It doesn't check if the blob exists in the storage or not. If you want to check if the blob exists in the storage, you could either fetch blob attributes using CloudBlockBlob.FetchAttributes method or creating an instance of CloudBlob using CloudBlobContainer.GetBlobReferenceFromServer or CloudBlobClient.GetBlobReferenceFromServer. All of the three methods above will fetch information about the blob from storage and would throw appropriate errors if something is not right (e.g. Not Found error if the blob does not exist).

Related

Azure - get_blob_client - blob creation

Whats the exact functionality of get_blob_client()?
get_blob_client(container, blob, snapshot=None)
I understood as, it automatically creates one if its the blob is not yet available.
My issue :
I used get_blob_client(container, blob, snapshot=None) for creating NEW blobs before. It now neither shows error nor the blob is created.
Note: When tried download_blob() it say
NO blob found.
Whats the issue here?
On Workaround on the get_blob_client() function. It wont creates the blob it only creates the reference .
I tried with the python taken the sample example .I don’t have the example.txt blob in container (test) .When I call get_blob_client() function it creates the reference not the blob.
In azure storage the blob not created
So when you try to download the blob it throws the error. So Create the blob the get the reference for the blob then download it

how to programmatically delete uncommitted blocks in azure storage using blob service?

We get an error 'The specified blob or block content is invalid' if we try to upload a block that is already present in server. how to clear those uncommittedblocks before user retries to upload the same blob?
code:'InvalidBlobOrBlock'
message:'The specified blob or block content is invalid.\nRequestId:1b015a55-201e-00be-7b1c-7e8fb8000000\nTime:2021-07-21T10:35:48.0075829Z'
name:'StorageError'
requestId:'1b015a55-201e-00be-7b1c-7e8fb8000000'
stack:'StorageError: The specified blob or block content is invalid
statusCode:400
how to clear those uncommittedblocks before user retries to upload the
same blob?
AFAIK, there's no direct way to delete uncommitted blocks. Simplest way for you would be to create a blob with the same name and then delete that blob. When creating a blob with the same name, please ensure that it is uploaded without splitting the contents into blocks.
I wrote a blog post about this error some time back that you may find useful: https://gauravmantri.com/2013/05/18/windows-azure-blob-storage-dealing-with-the-specified-blob-or-block-content-is-invalid-error/.

Azure blob storage overwriting duplicate files

I am using Azure Blob storage to upload/download files. The problem is, if I upload any new file to azure blob that have the same name as already uploaded file then its automatically overwriting the content of previously uploaded file.
For example
These are the files uploaded on azure blob storage -
file1.docx
file2.png
file1.png
So if i am uploading a new file named as "file1.docx" which have the different content. Then blob storage is replacing the previous uploaded file1.docx . So in this case i am losing the previously uploaded file.
Is there any way that blob storage can automatically detect that there is duplicate so it can append _1 or (1) in the end or any other way to solve this problem ?
Is there any way that blob storage can automatically detect that there
is duplicate so it can append _1 or (1) in the end or any other way to
solve this problem ?
Out of the box this feature is not available and you will have to handle this in your application. If your upload operation fails with a Conflict (HTTP Status Code 409) error that would mean that a blob by the name of the uploaded file exists. You would then need to retry the operation by appending _1 or (1). You will need to keep on doing it by increasing the counter till the time your upload does not fail with conflict status code.
You can also append GUID to your file name which will make the file unique.

Check if Blob of unknown Blob type exists

I've inherited a project built using the Azure Storage Client 1.7 and am upgrading it as Microsoft have announced that this will no longer be supported from December this year.
References to the files in Blob storage are stored in a database with the following fields:
FilePath - a string in the form of uploadfiles/xxx/yyy/Image-20140117170146.jpg
FileURI - A string in the form of https://zzz.blob.core.windows.net/uploadfiles/xxx/yyy/Image-20140117170146.jpg
GetBlobReferenceFromServer will throw an exception if the file doesn't exist, so it seems you should use GetBlockBlobReference if you know the container and the Blob type.
So my question(s):
Can I assume any Blobs currently uploaded (using StorageClient 1.7) will be BlockBlobs?
As I need to know the container name to call GetBlockBlobReference can I reliably say that in the examples above my container would always be uploadfiles
Can I assume any Blobs currently uploaded (using StorageClient 1.7)
will be BlockBlobs?
Though you can't be 100% sure that the blobs uploaded via Storage Client library 1.7 are Blob Blobs because 1.7 also supported Page Blobs however you can make some intelligent guesses. For example, if the files are image files and other commonly used files (pdf, document etc.), you can assume that they are block blobs. Typically you would see vhd files uploaded as page blobs. Again if these are uploaded by the users of your application, more than likely they are block blobs.
Having said this, I think you should use GetBlobReferenceFromServer method. What you could do is list all blobs from the database and for each of them call GetBlobReferenceFromServer method. If the blob exists, then you will get the blob type. If the blob doesn't exist, this method will give you an error. This would be the quickest way to identify the blob type of existing entries in the database. If you want, you can store the blob type back in the database along with existing record if you find both block and page blobs when you check the blob type so that if in future you need to decide between creating a CloudBlockBlob or CloudPageBlob reference, you can look at this field.
As I need to know the container name to call GetBlockBlobReference can
I reliably say that in the examples above my container would always be
uploadfiles
Yes. In the examples you listed above, you can say that the blob container is upload files.

Upload to Blob Storage while overwriting existing Blob

When using the StorageClient and calling UploadByteArray() with a blob name that already exists, will this cause any data corruption ? (In other words, do I have to call Delete() before uploading, which will cost another transaction ?)
You should just be able to call UploadByteArray() without having to do the delete first and it will overwrite the blob that is already there.

Resources