5
votes

I am uploading files from a windows systems to Azure storage blobs

The local file names may contain characters that are invalid in blob names

I need a way of encoding these names as to fulfill the requirements set out by MSDN on blob storage names, as shown below

A blob name must conforming to the following naming rules:

  • A blob name can contain any combination of characters.
  • A blob name must be at least one character long and cannot be more than 1,024 characters long.
  • Blob names are case-sensitive.
  • Reserved URL characters must be properly escaped.
  • The number of path segments comprising the blob name cannot exceed 254. A path segment is the string between consecutive delimiter characters (e.g., the forward slash '/') that corresponds to the name of a virtual directory.

The pertinent information above is "Reserved URL characters must be properly escaped. However, what method is the "standard" to use?

1
Almost all my project, I'm using GUID for the blob name (with its original extname), and mapping its original name in Table, SQL Database or in blob's metadata. Did you considered this solution? Personally thought it's better than use the client side file name directly since it gives us more flexibility.Shaun Xu
For another project we do that and it works well, however this one is linked to a dedicated cut down ftp server worker role, and i kind of need to keep the names intact, escaping seems to be appropriate in this caseTheGeneral
Thanks for your clarification. So this is another case please ignore my 'off-topic' comment below.Shaun Xu
Are you using storage client library or have you written your own code to upload by consuming REST API?Gaurav Mantri
If you're using Storage Client library, then I don't think you can do much. Internally the library is doing URL encoding and you will have to live with that. If you encode the blob name and then send it to the library, it will encode it again and thus your blob name would be incorrect.Gaurav Mantri

1 Answers

1
votes

You can use Uri.EscapeUriString(yourStringToEscape)

See docs here

You still have to do the other ones manually i.e the length and path segments .etc.