type.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package oss
  2. import (
  3. "encoding/xml"
  4. "net/url"
  5. "time"
  6. )
  7. // ListBucketsResult ListBuckets请求返回的结果
  8. type ListBucketsResult struct {
  9. XMLName xml.Name `xml:"ListAllMyBucketsResult"`
  10. Prefix string `xml:"Prefix"` // 本次查询结果的前缀
  11. Marker string `xml:"Marker"` // 标明查询的起点,未全部返回时有此节点
  12. MaxKeys int `xml:"MaxKeys"` // 返回结果的最大数目,未全部返回时有此节点
  13. IsTruncated bool `xml:"IsTruncated"` // 所有的结果是否已经全部返回
  14. NextMarker string `xml:"NextMarker"` // 表示下一次查询的起点
  15. Owner Owner `xml:"Owner"` // 拥有者信息
  16. Buckets []BucketProperties `xml:"Buckets>Bucket"` // Bucket列表
  17. }
  18. // BucketProperties Bucket信息
  19. type BucketProperties struct {
  20. XMLName xml.Name `xml:"Bucket"`
  21. Name string `xml:"Name"` // Bucket名称
  22. Location string `xml:"Location"` // Bucket所在的数据中心
  23. CreationDate time.Time `xml:"CreationDate"` // Bucket创建时间
  24. StorageClass string `xml:"StorageClass"` // Bucket的存储方式
  25. }
  26. // GetBucketACLResult GetBucketACL请求返回的结果
  27. type GetBucketACLResult struct {
  28. XMLName xml.Name `xml:"AccessControlPolicy"`
  29. ACL string `xml:"AccessControlList>Grant"` // Bucket权限
  30. Owner Owner `xml:"Owner"` // Bucket拥有者信息
  31. }
  32. // LifecycleConfiguration Bucket的Lifecycle配置
  33. type LifecycleConfiguration struct {
  34. XMLName xml.Name `xml:"LifecycleConfiguration"`
  35. Rules []LifecycleRule `xml:"Rule"`
  36. }
  37. // LifecycleRule Lifecycle规则
  38. type LifecycleRule struct {
  39. XMLName xml.Name `xml:"Rule"`
  40. ID string `xml:"ID"` // 规则唯一的ID
  41. Prefix string `xml:"Prefix"` // 规则所适用Object的前缀
  42. Status string `xml:"Status"` // 规则是否生效
  43. Expiration LifecycleExpiration `xml:"Expiration"` // 规则的过期属性
  44. }
  45. // LifecycleExpiration 规则的过期属性
  46. type LifecycleExpiration struct {
  47. XMLName xml.Name `xml:"Expiration"`
  48. Days int `xml:"Days,omitempty"` // 最后修改时间过后多少天生效
  49. Date time.Time `xml:"Date,omitempty"` // 指定规则何时生效
  50. }
  51. type lifecycleXML struct {
  52. XMLName xml.Name `xml:"LifecycleConfiguration"`
  53. Rules []lifecycleRule `xml:"Rule"`
  54. }
  55. type lifecycleRule struct {
  56. XMLName xml.Name `xml:"Rule"`
  57. ID string `xml:"ID"`
  58. Prefix string `xml:"Prefix"`
  59. Status string `xml:"Status"`
  60. Expiration lifecycleExpiration `xml:"Expiration"`
  61. }
  62. type lifecycleExpiration struct {
  63. XMLName xml.Name `xml:"Expiration"`
  64. Days int `xml:"Days,omitempty"`
  65. Date string `xml:"Date,omitempty"`
  66. }
  67. const expirationDateFormat = "2006-01-02T15:04:05.000Z"
  68. func convLifecycleRule(rules []LifecycleRule) []lifecycleRule {
  69. rs := []lifecycleRule{}
  70. for _, rule := range rules {
  71. r := lifecycleRule{}
  72. r.ID = rule.ID
  73. r.Prefix = rule.Prefix
  74. r.Status = rule.Status
  75. if rule.Expiration.Date.IsZero() {
  76. r.Expiration.Days = rule.Expiration.Days
  77. } else {
  78. r.Expiration.Date = rule.Expiration.Date.Format(expirationDateFormat)
  79. }
  80. rs = append(rs, r)
  81. }
  82. return rs
  83. }
  84. // BuildLifecycleRuleByDays 指定过期天数构建Lifecycle规则
  85. func BuildLifecycleRuleByDays(id, prefix string, status bool, days int) LifecycleRule {
  86. var statusStr = "Enabled"
  87. if !status {
  88. statusStr = "Disabled"
  89. }
  90. return LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
  91. Expiration: LifecycleExpiration{Days: days}}
  92. }
  93. // BuildLifecycleRuleByDate 指定过期时间构建Lifecycle规则
  94. func BuildLifecycleRuleByDate(id, prefix string, status bool, year, month, day int) LifecycleRule {
  95. var statusStr = "Enabled"
  96. if !status {
  97. statusStr = "Disabled"
  98. }
  99. date := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
  100. return LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
  101. Expiration: LifecycleExpiration{Date: date}}
  102. }
  103. // GetBucketLifecycleResult GetBucketLifecycle请求请求结果
  104. type GetBucketLifecycleResult LifecycleConfiguration
  105. // RefererXML Referer配置
  106. type RefererXML struct {
  107. XMLName xml.Name `xml:"RefererConfiguration"`
  108. AllowEmptyReferer bool `xml:"AllowEmptyReferer"` // 是否允许referer字段为空的请求访问
  109. RefererList []string `xml:"RefererList>Referer"` // referer访问白名单
  110. }
  111. // GetBucketRefererResult GetBucketReferer请教返回结果
  112. type GetBucketRefererResult RefererXML
  113. // LoggingXML Logging配置
  114. type LoggingXML struct {
  115. XMLName xml.Name `xml:"BucketLoggingStatus"`
  116. LoggingEnabled LoggingEnabled `xml:"LoggingEnabled"` // 访问日志信息容器
  117. }
  118. type loggingXMLEmpty struct {
  119. XMLName xml.Name `xml:"BucketLoggingStatus"`
  120. }
  121. // LoggingEnabled 访问日志信息容器
  122. type LoggingEnabled struct {
  123. XMLName xml.Name `xml:"LoggingEnabled"`
  124. TargetBucket string `xml:"TargetBucket"` //存放访问日志的Bucket
  125. TargetPrefix string `xml:"TargetPrefix"` //保存访问日志的文件前缀
  126. }
  127. // GetBucketLoggingResult GetBucketLogging请求返回结果
  128. type GetBucketLoggingResult LoggingXML
  129. // WebsiteXML Website配置
  130. type WebsiteXML struct {
  131. XMLName xml.Name `xml:"WebsiteConfiguration"`
  132. IndexDocument IndexDocument `xml:"IndexDocument"` // 目录URL时添加的索引文件
  133. ErrorDocument ErrorDocument `xml:"ErrorDocument"` // 404错误时使用的文件
  134. }
  135. // IndexDocument 目录URL时添加的索引文件
  136. type IndexDocument struct {
  137. XMLName xml.Name `xml:"IndexDocument"`
  138. Suffix string `xml:"Suffix"` // 目录URL时添加的索引文件名
  139. }
  140. // ErrorDocument 404错误时使用的文件
  141. type ErrorDocument struct {
  142. XMLName xml.Name `xml:"ErrorDocument"`
  143. Key string `xml:"Key"` // 404错误时使用的文件名
  144. }
  145. // GetBucketWebsiteResult GetBucketWebsite请求返回结果
  146. type GetBucketWebsiteResult WebsiteXML
  147. // CORSXML CORS配置
  148. type CORSXML struct {
  149. XMLName xml.Name `xml:"CORSConfiguration"`
  150. CORSRules []CORSRule `xml:"CORSRule"` // CORS规则列表
  151. }
  152. // CORSRule CORS规则
  153. type CORSRule struct {
  154. XMLName xml.Name `xml:"CORSRule"`
  155. AllowedOrigin []string `xml:"AllowedOrigin"` // 允许的来源,默认通配符"*"
  156. AllowedMethod []string `xml:"AllowedMethod"` // 允许的方法
  157. AllowedHeader []string `xml:"AllowedHeader"` // 允许的请求头
  158. ExposeHeader []string `xml:"ExposeHeader"` // 允许的响应头
  159. MaxAgeSeconds int `xml:"MaxAgeSeconds"` // 最大的缓存时间
  160. }
  161. // GetBucketCORSResult GetBucketCORS请求返回的结果
  162. type GetBucketCORSResult CORSXML
  163. // GetBucketInfoResult GetBucketInfo请求返回结果
  164. type GetBucketInfoResult struct {
  165. XMLName xml.Name `xml:"BucketInfo"`
  166. BucketInfo BucketInfo `xml:"Bucket"`
  167. }
  168. // BucketInfo Bucket信息
  169. type BucketInfo struct {
  170. XMLName xml.Name `xml:"Bucket"`
  171. Name string `xml:"Name"` // Bucket名称
  172. Location string `xml:"Location"` // Bucket所在的数据中心
  173. CreationDate time.Time `xml:"CreationDate"` // Bucket创建时间
  174. ExtranetEndpoint string `xml:"ExtranetEndpoint"` // Bucket访问的外网域名
  175. IntranetEndpoint string `xml:"IntranetEndpoint"` // Bucket访问的内网域名
  176. ACL string `xml:"AccessControlList>Grant"` // Bucket权限
  177. Owner Owner `xml:"Owner"` // Bucket拥有者信息
  178. StorageClass string `xml:"StorageClass"` // Bucket存储类型
  179. }
  180. // ListObjectsResult ListObjects请求返回结果
  181. type ListObjectsResult struct {
  182. XMLName xml.Name `xml:"ListBucketResult"`
  183. Prefix string `xml:"Prefix"` // 本次查询结果的开始前缀
  184. Marker string `xml:"Marker"` // 这次查询的起点
  185. MaxKeys int `xml:"MaxKeys"` // 请求返回结果的最大数目
  186. Delimiter string `xml:"Delimiter"` // 对Object名字进行分组的字符
  187. IsTruncated bool `xml:"IsTruncated"` // 是否所有的结果都已经返回
  188. NextMarker string `xml:"NextMarker"` // 下一次查询的起点
  189. Objects []ObjectProperties `xml:"Contents"` // Object类别
  190. CommonPrefixes []string `xml:"CommonPrefixes>Prefix"` // 以delimiter结尾并有共同前缀的Object的集合
  191. }
  192. // ObjectProperties Objecct属性
  193. type ObjectProperties struct {
  194. XMLName xml.Name `xml:"Contents"`
  195. Key string `xml:"Key"` // Object的Key
  196. Type string `xml:"Type"` // Object Type
  197. Size int64 `xml:"Size"` // Object的长度字节数
  198. ETag string `xml:"ETag"` // 标示Object的内容
  199. Owner Owner `xml:"Owner"` // 保存Object拥有者信息的容器
  200. LastModified time.Time `xml:"LastModified"` // Object最后修改时间
  201. StorageClass string `xml:"StorageClass"` // Object的存储类型
  202. }
  203. // Owner Bucket/Object的owner
  204. type Owner struct {
  205. XMLName xml.Name `xml:"Owner"`
  206. ID string `xml:"ID"` // 用户ID
  207. DisplayName string `xml:"DisplayName"` // Owner名字
  208. }
  209. // CopyObjectResult CopyObject请求返回的结果
  210. type CopyObjectResult struct {
  211. XMLName xml.Name `xml:"CopyObjectResult"`
  212. LastModified time.Time `xml:"LastModified"` // 新Object最后更新时间
  213. ETag string `xml:"ETag"` // 新Object的ETag值
  214. }
  215. // GetObjectACLResult GetObjectACL请求返回的结果
  216. type GetObjectACLResult GetBucketACLResult
  217. type deleteXML struct {
  218. XMLName xml.Name `xml:"Delete"`
  219. Objects []DeleteObject `xml:"Object"` // 删除的所有Object
  220. Quiet bool `xml:"Quiet"` // 安静响应模式
  221. }
  222. // DeleteObject 删除的Object
  223. type DeleteObject struct {
  224. XMLName xml.Name `xml:"Object"`
  225. Key string `xml:"Key"` // Object名称
  226. }
  227. // DeleteObjectsResult DeleteObjects请求返回结果
  228. type DeleteObjectsResult struct {
  229. XMLName xml.Name `xml:"DeleteResult"`
  230. DeletedObjects []string `xml:"Deleted>Key"` // 删除的Object列表
  231. }
  232. // InitiateMultipartUploadResult InitiateMultipartUpload请求返回结果
  233. type InitiateMultipartUploadResult struct {
  234. XMLName xml.Name `xml:"InitiateMultipartUploadResult"`
  235. Bucket string `xml:"Bucket"` // Bucket名称
  236. Key string `xml:"Key"` // 上传Object名称
  237. UploadID string `xml:"UploadId"` // 生成的UploadId
  238. }
  239. // UploadPart 上传/拷贝的分片
  240. type UploadPart struct {
  241. XMLName xml.Name `xml:"Part"`
  242. PartNumber int `xml:"PartNumber"` // Part编号
  243. ETag string `xml:"ETag"` // ETag缓存码
  244. }
  245. type uploadParts []UploadPart
  246. func (slice uploadParts) Len() int {
  247. return len(slice)
  248. }
  249. func (slice uploadParts) Less(i, j int) bool {
  250. return slice[i].PartNumber < slice[j].PartNumber
  251. }
  252. func (slice uploadParts) Swap(i, j int) {
  253. slice[i], slice[j] = slice[j], slice[i]
  254. }
  255. // UploadPartCopyResult 拷贝分片请求返回的结果
  256. type UploadPartCopyResult struct {
  257. XMLName xml.Name `xml:"CopyPartResult"`
  258. LastModified time.Time `xml:"LastModified"` // 最后修改时间
  259. ETag string `xml:"ETag"` // ETag
  260. }
  261. type completeMultipartUploadXML struct {
  262. XMLName xml.Name `xml:"CompleteMultipartUpload"`
  263. Part []UploadPart `xml:"Part"`
  264. }
  265. // CompleteMultipartUploadResult 提交分片上传任务返回结果
  266. type CompleteMultipartUploadResult struct {
  267. XMLName xml.Name `xml:"CompleteMultipartUploadResult"`
  268. Location string `xml:"Location"` // Object的URL
  269. Bucket string `xml:"Bucket"` // Bucket名称
  270. ETag string `xml:"ETag"` // Object的ETag
  271. Key string `xml:"Key"` // Object的名字
  272. }
  273. // ListUploadedPartsResult ListUploadedParts请求返回结果
  274. type ListUploadedPartsResult struct {
  275. XMLName xml.Name `xml:"ListPartsResult"`
  276. Bucket string `xml:"Bucket"` // Bucket名称
  277. Key string `xml:"Key"` // Object名称
  278. UploadID string `xml:"UploadId"` // 上传Id
  279. NextPartNumberMarker string `xml:"NextPartNumberMarker"` // 下一个Part的位置
  280. MaxParts int `xml:"MaxParts"` // 最大Part个数
  281. IsTruncated bool `xml:"IsTruncated"` // 是否完全上传完成
  282. UploadedParts []UploadedPart `xml:"Part"` // 已完成的Part
  283. }
  284. // UploadedPart 该任务已经上传的分片
  285. type UploadedPart struct {
  286. XMLName xml.Name `xml:"Part"`
  287. PartNumber int `xml:"PartNumber"` // Part编号
  288. LastModified time.Time `xml:"LastModified"` // 最后一次修改时间
  289. ETag string `xml:"ETag"` // ETag缓存码
  290. Size int `xml:"Size"` // Part大小
  291. }
  292. // ListMultipartUploadResult ListMultipartUpload请求返回结果
  293. type ListMultipartUploadResult struct {
  294. XMLName xml.Name `xml:"ListMultipartUploadsResult"`
  295. Bucket string `xml:"Bucket"` // Bucket名称
  296. Delimiter string `xml:"Delimiter"` // 分组分割符
  297. Prefix string `xml:"Prefix"` // 筛选前缀
  298. KeyMarker string `xml:"KeyMarker"` // 起始Object位置
  299. UploadIDMarker string `xml:"UploadIdMarker"` // 起始UploadId位置
  300. NextKeyMarker string `xml:"NextKeyMarker"` // 如果没有全部返回,标明接下去的KeyMarker位置
  301. NextUploadIDMarker string `xml:"NextUploadIdMarker"` // 如果没有全部返回,标明接下去的UploadId位置
  302. MaxUploads int `xml:"MaxUploads"` // 返回最大Upload数目
  303. IsTruncated bool `xml:"IsTruncated"` // 是否完全返回
  304. Uploads []UncompletedUpload `xml:"Upload"` // 未完成上传的MultipartUpload
  305. CommonPrefixes []string `xml:"CommonPrefixes>Prefix"` // 所有名字包含指定的前缀且第一次出现delimiter字符之间的object作为一组的分组结果
  306. }
  307. // UncompletedUpload 未完成的Upload任务
  308. type UncompletedUpload struct {
  309. XMLName xml.Name `xml:"Upload"`
  310. Key string `xml:"Key"` // Object名称
  311. UploadID string `xml:"UploadId"` // 对应UploadId
  312. Initiated time.Time `xml:"Initiated"` // 初始化时间,格式2012-02-23T04:18:23.000Z
  313. }
  314. // 解析URL编码
  315. func decodeDeleteObjectsResult(result *DeleteObjectsResult) error {
  316. var err error
  317. for i := 0; i < len(result.DeletedObjects); i++ {
  318. result.DeletedObjects[i], err = url.QueryUnescape(result.DeletedObjects[i])
  319. if err != nil {
  320. return err
  321. }
  322. }
  323. return nil
  324. }
  325. // 解析URL编码
  326. func decodeListObjectsResult(result *ListObjectsResult) error {
  327. var err error
  328. result.Prefix, err = url.QueryUnescape(result.Prefix)
  329. if err != nil {
  330. return err
  331. }
  332. result.Marker, err = url.QueryUnescape(result.Marker)
  333. if err != nil {
  334. return err
  335. }
  336. result.Delimiter, err = url.QueryUnescape(result.Delimiter)
  337. if err != nil {
  338. return err
  339. }
  340. result.NextMarker, err = url.QueryUnescape(result.NextMarker)
  341. if err != nil {
  342. return err
  343. }
  344. for i := 0; i < len(result.Objects); i++ {
  345. result.Objects[i].Key, err = url.QueryUnescape(result.Objects[i].Key)
  346. if err != nil {
  347. return err
  348. }
  349. }
  350. for i := 0; i < len(result.CommonPrefixes); i++ {
  351. result.CommonPrefixes[i], err = url.QueryUnescape(result.CommonPrefixes[i])
  352. if err != nil {
  353. return err
  354. }
  355. }
  356. return nil
  357. }
  358. // 解析URL编码
  359. func decodeListMultipartUploadResult(result *ListMultipartUploadResult) error {
  360. var err error
  361. result.Prefix, err = url.QueryUnescape(result.Prefix)
  362. if err != nil {
  363. return err
  364. }
  365. result.Delimiter, err = url.QueryUnescape(result.Delimiter)
  366. if err != nil {
  367. return err
  368. }
  369. result.KeyMarker, err = url.QueryUnescape(result.KeyMarker)
  370. if err != nil {
  371. return err
  372. }
  373. result.NextKeyMarker, err = url.QueryUnescape(result.NextKeyMarker)
  374. if err != nil {
  375. return err
  376. }
  377. for i := 0; i < len(result.Uploads); i++ {
  378. result.Uploads[i].Key, err = url.QueryUnescape(result.Uploads[i].Key)
  379. if err != nil {
  380. return err
  381. }
  382. }
  383. for i := 0; i < len(result.CommonPrefixes); i++ {
  384. result.CommonPrefixes[i], err = url.QueryUnescape(result.CommonPrefixes[i])
  385. if err != nil {
  386. return err
  387. }
  388. }
  389. return nil
  390. }
  391. // createBucketConfiguration 规则的过期属性
  392. type createBucketConfiguration struct {
  393. XMLName xml.Name `xml:"CreateBucketConfiguration"`
  394. StorageClass StorageClassType `xml:"StorageClass,omitempty"`
  395. }