Invalid character exception when adding Metadata to a CloudBlob

前端 未结 4 1589
南方客
南方客 2021-01-01 15:59

Task

Upload a file to Azure Blob Storage with the original filename and also assign the filename as meta-data to the CloudBlob

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 16:34

    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:

        public static class Base64Extensions
        {
            public static string ToBase64(this string input)
            {
                var bytes = Encoding.UTF8.GetBytes(input);
                return Convert.ToBase64String(bytes);
            }
    
            public static string FromBase64(this string input)
            {
                var bytes = Convert.FromBase64String(input);
                return Encoding.UTF8.GetString(bytes);
            }
        }
    

    and then when setting blob metadata:

    blobReference.Metadata["Filename"] = filename.ToBase64();
    

    and when retrieving it:

    var filename = blobReference.Metadata["Filename"].FromBase64();
    

    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.

提交回复
热议问题