Task
Upload a file to Azure Blob Storage
with the original filename and also assign the filename as meta-data
to the CloudBlob
Problem
These characters are not permitted in the meta-data
but are acceptable as the blob
name:
š Š ñ Ñ ç Ç ÿ Ÿ ž Ž Ð œ Œ « » éèëêð ÉÈËÊ àâä ÀÁÂÃÄÅ àáâãäå ÙÚÛÜ ùúûüµ òóôõöø ÒÓÔÕÖØ ìíîï ÌÍÎÏ
Question
- Is there a way to store these characters in the
meta-data
? Are we missing some setting that causes this exception? - Most of these characters are standard glyphs in some languages, so how to handle that?
- Is there any documentation available that advises about this issue? I found
blob
andmeta-data
naming conventions, but none about the data itself!
Code
var dirtyFileName = file.FileName;
var normalizedFileName = file.FileName.CleanOffDiacriticAndNonASCII();
// Blob name accepts almost characters that are acceptable as filenames in Windows
var blob = container.GetBlobReference(dirtyFileName);
//Upload content to the blob, which will create the blob if it does not already exist.
blob.Metadata["FileName"] = normalizedFileName;
blob.Attributes.Properties.ContentType = file.ContentType;
// ERROR: Occurs here!
blob.UploadFromStream(file.InputStream);
blob.SetMetadata();
blob.SetProperties();
Error
References
- Naming and Referencing Containers, Blobs, and Metadata
- How to support other languages in Azure blob storage?
- How do I remove diacritics (accents) from a string in .NET?
- Azure CloudBlob SetMetadata fails with "The metadata specified is invalid. It has characters that are not permitted."
- Replacing characters in C# (ascii)
Workarounds
Illegal characters in filename is only the tip of the ice-berg, magnified only for the purpose of this question! The bigger picture is that we index these files using Lucene.net
and as such need a lot of meta-data
to be stored on the blob
. Please don't suggest storing it all separately in a database, just don't! Up until now we have been lucky to only have come across one file with diacritic characters!
So, at the moment we are making the effort to avoid saving the filename in the meta-data
as a workaround!
If the above list is exhaustive, it should be possible to encode the metadata to HTML and then decode it when you need it:
Just have had confirmation from the
azure-sdk-for-net
team on GitHub that onlyASCII
characters are valid as data withinblob meta-data
.Source on GitHub
So as a work-around, either:
To expand on the answer by bPratik, we've found that Base64 encoding metadata works nicely. We use this extension method to do the encode and decode:
and then when setting blob metadata:
and when retrieving it:
For search, you would have to decode the filename before presenting it to the indexer, or use the blob's actual filename assuming you're still using the original filename there.
Unless I get an answer that actually solves the issue, this workaround is a solution for the above issue!
Workaround
To get this to work, I am using a combination of the below methods to:
But this isn't ideal as we are losing data!
Diacritics to ASCII
Non-ASCII Burninator
I really hope to get an answer as the workaround is obviously not ideal, and it also doesn't make sense why this is not possible!