ObjectMetadata.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using Aliyun.OSS.Util;
  10. namespace Aliyun.OSS
  11. {
  12. /// <summary>
  13. /// OSS中Object的元数据。
  14. /// <para>
  15. /// 包含了用户自定义的元数据,也包含了OSS发送的标准HTTP头(如Content-Length, ETag等)。
  16. /// </para>
  17. /// </summary>
  18. public class ObjectMetadata
  19. {
  20. private readonly IDictionary<string, string> _userMetadata =
  21. new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  22. private readonly IDictionary<string, object> _metadata =
  23. new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
  24. /// <summary>
  25. /// 256位的Aes加密算法
  26. /// </summary>
  27. public const string Aes256ServerSideEncryption = "AES256";
  28. /// <summary>
  29. /// 获取用户自定义的元数据。
  30. /// </summary>
  31. /// <remarks>
  32. /// OSS内部保存用户自定义的元数据时,会以x-oss-meta-为请求头的前缀。
  33. /// 但用户通过该接口处理用户自定义元数据里,不需要加上前缀“x-oss-meta-”。
  34. /// 同时,元数据字典的键名是不区分大小写的,并且在从服务器端返回时会全部以小写形式返回,
  35. /// 即使在设置时给定了大写字母。比如键名为:MyUserMeta,通过GetObjectMetadata接口
  36. /// 返回时键名会变为:myusermeta。
  37. /// </remarks>
  38. public IDictionary<string, string> UserMetadata
  39. {
  40. get { return _userMetadata; }
  41. }
  42. /// <summary>
  43. /// 获取Last-Modified请求头的值,表示Object最后一次修改的时间。
  44. /// </summary>
  45. public DateTime LastModified
  46. {
  47. get
  48. {
  49. return _metadata.ContainsKey(HttpHeaders.LastModified)
  50. ? (DateTime)_metadata[HttpHeaders.LastModified] : DateTime.MinValue;
  51. }
  52. internal set
  53. {
  54. _metadata[HttpHeaders.LastModified] = value;
  55. }
  56. }
  57. /// <summary>
  58. /// 获取Expires请求头,表示Object的过期时间。
  59. /// </summary>
  60. public DateTime ExpirationTime
  61. {
  62. get
  63. {
  64. return _metadata.ContainsKey(HttpHeaders.Expires)
  65. ? DateUtils.ParseRfc822Date((string)_metadata[HttpHeaders.Expires]) : DateTime.MinValue;
  66. }
  67. set
  68. {
  69. _metadata[HttpHeaders.Expires] = DateUtils.FormatRfc822Date(value);
  70. }
  71. }
  72. /// <summary>
  73. /// 获取Content-Length请求头,表示Object内容的大小。
  74. /// </summary>
  75. public long ContentLength
  76. {
  77. get
  78. {
  79. return _metadata.ContainsKey(HttpHeaders.ContentLength)
  80. ? (long)_metadata[HttpHeaders.ContentLength] : 0;
  81. }
  82. set
  83. {
  84. if (value > OssUtils.MaxFileSize)
  85. throw new ArgumentException("content length not allow to exceed 5GB.");
  86. _metadata[HttpHeaders.ContentLength] = value;
  87. }
  88. }
  89. /// <summary>
  90. /// 获取或设置Content-Type请求头,表示Object内容的类型,为标准的MIME类型。
  91. /// </summary>
  92. public string ContentType
  93. {
  94. get
  95. {
  96. return _metadata.ContainsKey(HttpHeaders.ContentType)
  97. ? _metadata[HttpHeaders.ContentType] as string : null;
  98. }
  99. set
  100. {
  101. if (!string.IsNullOrEmpty(value))
  102. {
  103. _metadata[HttpHeaders.ContentType] = value;
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// 获取或设置Content-Encoding请求头,表示Object内容的编码方式。
  109. /// </summary>
  110. public string ContentEncoding
  111. {
  112. get
  113. {
  114. return _metadata.ContainsKey(HttpHeaders.ContentEncoding)
  115. ? _metadata[HttpHeaders.ContentEncoding] as string : null;
  116. }
  117. set
  118. {
  119. if (value != null)
  120. {
  121. _metadata[HttpHeaders.ContentEncoding] = value;
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// 获取或设置Cache-Control请求头,表示用户指定的HTTP请求/回复链的缓存行为。
  127. /// </summary>
  128. public string CacheControl
  129. {
  130. get
  131. {
  132. return _metadata.ContainsKey(HttpHeaders.CacheControl)
  133. ? _metadata[HttpHeaders.CacheControl] as string : null;
  134. }
  135. set
  136. {
  137. if (value != null)
  138. {
  139. _metadata[HttpHeaders.CacheControl] = value;
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// 获取Content-Disposition请求头,表示MIME用户代理如何显示附加的文件。
  145. /// </summary>
  146. public string ContentDisposition
  147. {
  148. get
  149. {
  150. return _metadata.ContainsKey(HttpHeaders.ContentDisposition)
  151. ? _metadata[HttpHeaders.ContentDisposition] as string : null;
  152. }
  153. set
  154. {
  155. if (value != null)
  156. {
  157. _metadata[HttpHeaders.ContentDisposition] = value;
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// 获取或者设置ETAG值,如果要设置Content-Md5值,请使用Content-Md5属性。
  163. /// </summary>
  164. public string ETag
  165. {
  166. get
  167. {
  168. return _metadata.ContainsKey(HttpHeaders.ETag)
  169. ? _metadata[HttpHeaders.ETag] as string : null;
  170. }
  171. set
  172. {
  173. if (value != null)
  174. {
  175. _metadata[HttpHeaders.ETag] = value;
  176. }
  177. }
  178. }
  179. /// <summary>
  180. /// 获取或设置一个值表示与Object相关的hex编码的128位MD5摘要。
  181. /// </summary>
  182. public string ContentMd5
  183. {
  184. get
  185. {
  186. return _metadata.ContainsKey(HttpHeaders.ContentMd5)
  187. ? _metadata[HttpHeaders.ContentMd5] as string : null;
  188. }
  189. set
  190. {
  191. if (value != null)
  192. {
  193. _metadata[HttpHeaders.ContentMd5] = value;
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// 获取和设置服务器端加密算法
  199. /// </summary>
  200. public string ServerSideEncryption
  201. {
  202. get
  203. {
  204. return _metadata.ContainsKey(HttpHeaders.ServerSideEncryption)
  205. ? _metadata[HttpHeaders.ServerSideEncryption] as string : null;
  206. }
  207. set
  208. {
  209. if (Aes256ServerSideEncryption != value)
  210. throw new ArgumentException("Unsupported server side encryption");
  211. _metadata[HttpHeaders.ServerSideEncryption] = value;
  212. }
  213. }
  214. /// <summary>
  215. /// 获取文件类型[Normal/Appendable]
  216. /// </summary>
  217. public string ObjectType
  218. {
  219. get
  220. {
  221. return _metadata.ContainsKey(HttpHeaders.ObjectType)
  222. ? _metadata[HttpHeaders.ObjectType] as string : null;
  223. }
  224. }
  225. /// <summary>
  226. /// 初始化一个新的<see cref="ObjectMetadata" />实例。
  227. /// </summary>
  228. public ObjectMetadata()
  229. {
  230. ContentLength = -1L;
  231. }
  232. /// <summary>
  233. /// 增加HTTP header信息
  234. /// </summary>
  235. /// <param name="key">header元素的名称</param>
  236. /// <param name="value">header元素的值</param>
  237. public void AddHeader(string key, object value)
  238. {
  239. _metadata.Add(key, value);
  240. }
  241. /// <summary>
  242. /// Populates the request header dictionary with the metdata and user metadata.
  243. /// </summary>
  244. /// <param name="requestHeaders"></param>
  245. internal void Populate(IDictionary<string, string> requestHeaders)
  246. {
  247. foreach(var entry in _metadata)
  248. requestHeaders.Add(entry.Key, entry.Value.ToString());
  249. if (!requestHeaders.ContainsKey(HttpHeaders.ContentType))
  250. requestHeaders.Add(HttpHeaders.ContentType, HttpUtils.DefaultContentType);
  251. foreach(var entry in _userMetadata)
  252. requestHeaders.Add(OssHeaders.OssUserMetaPrefix + entry.Key, entry.Value);
  253. }
  254. }
  255. }