【发布时间】:2013-01-31 17:00:59
【问题描述】:
任务
使用原始文件名将文件上传到Azure Blob Storage,并将文件名作为meta-data 分配给CloudBlob
问题
meta-data 中不允许使用这些字符,但可以作为 blob 名称使用:
š Š ñ Ñ ç Ç ÿ Ÿ ž Ž Ð œ Œ « » éèëêð ÉÈËÊ àâä ÀÁÂÃÄÅ àáâãäå ÙÚÛÜ ùúûüµ òóôõöø ÒÓÔÕÖØ ìíîï ÌÍÎÏ
问题
- 有没有办法将这些字符存储在
meta-data中?我们是否缺少某些导致此异常的设置? - 这些字符大部分是某些语言的标准字形,那么如何处理呢?
- 是否有任何可用的文档可以说明此问题?我找到了
blob和meta-data命名约定,但没有找到数据本身!
代码
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();
错误
参考文献
- 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)
解决方法
文件名中的非法字符只是冰山一角,仅为了这个问题而放大!更大的图景是我们使用Lucene.net 索引这些文件,因此需要将大量meta-data 存储在blob 上。 请不要建议将它们全部单独存储在数据库中,只是不要!到目前为止,我们很幸运只遇到了一个带有变音符号的文件!
因此,目前我们正在努力避免将文件名保存在 meta-data 中作为一种解决方法!
【问题讨论】:
-
你应该使用
blob.Properties.ContentDisposition=$"filename=\"{file.FileName}\"";保存文件名
标签: azure character-encoding azure-storage azure-blob-storage