1

if i upload a file on azure blob in the same container where the file is existing already, it is over-writing the file, how to avoid overwriting the same? below i am mentioning the scenario...

step1 - upload file "abc.jpg" on azure in container called say "filecontainer"

step2 - once it gets uploaded, try uploading some different file with the same name to the same container

Output - it will overwrite existing file with the latest uploaded

My Requirement - i want to avoid this overwrite, as different people may upload files having same name to my container.

Please help

P.S.

-i do not want to create different containers for different users

-i am using REST API with Java

dev2d
  • 4,245
  • 3
  • 31
  • 54

3 Answers3

4

Windows Azure Blob Storage supports conditional headers using which you can prevent overwriting of blobs. You can read more about conditional headers here: http://msdn.microsoft.com/en-us/library/windowsazure/dd179371.aspx.

Since you want that a blob should not be overwritten, you would need to specify If-None-Match conditional header and set it's value to *. This would cause the upload operation to fail with Precondition Failed (412) error.

Other idea would be to check for blob's existence just before uploading (by fetching it's properties) however I would not recommend this approach as it may lead to some concurrency issues.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Thanks for the answer Gaurav, need one more help, can you please guide me on following? http://stackoverflow.com/q/16958910/2381006 i am getting the same exception when i am adding this parameter to header :( – dev2d Jun 06 '13 at 12:17
2

You have no control over the name your users upload their files with. You, however, have control over the name you store those files with. The standard way is to generate a Guid and name each file accordingly. The chances of conflict is almost zero.

A simple pseudocode looks like this:

//generate a Guid and rename the file the user uploaded with the generated Guid
//store the name of the file in a dbase or what-have-you with the Guid
//upload the file to the blob storage using the name you generated above

Hope that helps.

olatunjee
  • 351
  • 2
  • 3
  • 12
1

Let me put it that way:

  • step one - user X uploads file "abc1.jpg" and you save it io a local folder XYZ
  • step two - user Y uploads another file with same name "abc1.jpg", and now you save it again in a local folder XYZ

What do you do now?

With this I am illustrating that your question does not relate to Azure in any way!

Just do not rely on original file names when saving files. Where-ever you are saving them. Generate random names (GUIDs for example) and "attach" the original name as meta-data.

astaykov
  • 30,768
  • 3
  • 70
  • 86