type.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package oss
  2. import (
  3. "encoding/xml"
  4. "net/url"
  5. "time"
  6. )
  7. // ListBucketsResult defines the result object from ListBuckets request
  8. type ListBucketsResult struct {
  9. XMLName xml.Name `xml:"ListAllMyBucketsResult"`
  10. Prefix string `xml:"Prefix"` // The prefix in this query
  11. Marker string `xml:"Marker"` // The marker filter
  12. MaxKeys int `xml:"MaxKeys"` // The max entry count to return. This information is returned when IsTruncated is true.
  13. IsTruncated bool `xml:"IsTruncated"` // Flag true means there's remaining buckets to return.
  14. NextMarker string `xml:"NextMarker"` // The marker filter for the next list call
  15. Owner Owner `xml:"Owner"` // The owner information
  16. Buckets []BucketProperties `xml:"Buckets>Bucket"` // The bucket list
  17. }
  18. // BucketProperties defines bucket properties
  19. type BucketProperties struct {
  20. XMLName xml.Name `xml:"Bucket"`
  21. Name string `xml:"Name"` // Bucket name
  22. Location string `xml:"Location"` // Bucket datacenter
  23. CreationDate time.Time `xml:"CreationDate"` // Bucket create time
  24. StorageClass string `xml:"StorageClass"` // Bucket storage class
  25. }
  26. // GetBucketACLResult defines GetBucketACL request's result
  27. type GetBucketACLResult struct {
  28. XMLName xml.Name `xml:"AccessControlPolicy"`
  29. ACL string `xml:"AccessControlList>Grant"` // Bucket ACL
  30. Owner Owner `xml:"Owner"` // Bucket owner
  31. }
  32. // LifecycleConfiguration is the Bucket Lifecycle configuration
  33. type LifecycleConfiguration struct {
  34. XMLName xml.Name `xml:"LifecycleConfiguration"`
  35. Rules []LifecycleRule `xml:"Rule"`
  36. }
  37. // LifecycleRule defines Lifecycle rules
  38. type LifecycleRule struct {
  39. XMLName xml.Name `xml:"Rule"`
  40. ID string `xml:"ID"` // The rule ID
  41. Prefix string `xml:"Prefix"` // The object key prefix
  42. Status string `xml:"Status"` // The rule status (enabled or not)
  43. Expiration LifecycleExpiration `xml:"Expiration"` // The expiration property
  44. }
  45. // LifecycleExpiration defines the rule's expiration property
  46. type LifecycleExpiration struct {
  47. XMLName xml.Name `xml:"Expiration"`
  48. Days int `xml:"Days,omitempty"` // Relative expiration time: The expiration time in days after the last modified time
  49. Date time.Time `xml:"Date,omitempty"` // Absolute expiration time: The expiration time in date.
  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 builds a lifecycle rule with specified expiration days
  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 builds a lifecycle rule with specified expiration time.
  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 defines GetBucketLifecycle's result object
  104. type GetBucketLifecycleResult LifecycleConfiguration
  105. // RefererXML defines Referer configuration
  106. type RefererXML struct {
  107. XMLName xml.Name `xml:"RefererConfiguration"`
  108. AllowEmptyReferer bool `xml:"AllowEmptyReferer"` // Allow empty referrer
  109. RefererList []string `xml:"RefererList>Referer"` // Referer whitelist
  110. }
  111. // GetBucketRefererResult defines result object for GetBucketReferer request
  112. type GetBucketRefererResult RefererXML
  113. // LoggingXML defines logging configuration
  114. type LoggingXML struct {
  115. XMLName xml.Name `xml:"BucketLoggingStatus"`
  116. LoggingEnabled LoggingEnabled `xml:"LoggingEnabled"` // The logging configuration information
  117. }
  118. type loggingXMLEmpty struct {
  119. XMLName xml.Name `xml:"BucketLoggingStatus"`
  120. }
  121. // LoggingEnabled defines the logging configuration information
  122. type LoggingEnabled struct {
  123. XMLName xml.Name `xml:"LoggingEnabled"`
  124. TargetBucket string `xml:"TargetBucket"` // The bucket name for storing the log files
  125. TargetPrefix string `xml:"TargetPrefix"` // The log file prefix
  126. }
  127. // GetBucketLoggingResult defines the result from GetBucketLogging request
  128. type GetBucketLoggingResult LoggingXML
  129. // WebsiteXML defines Website configuration
  130. type WebsiteXML struct {
  131. XMLName xml.Name `xml:"WebsiteConfiguration"`
  132. IndexDocument IndexDocument `xml:"IndexDocument"` // The index page
  133. ErrorDocument ErrorDocument `xml:"ErrorDocument"` // The error page
  134. }
  135. // IndexDocument defines the index page info
  136. type IndexDocument struct {
  137. XMLName xml.Name `xml:"IndexDocument"`
  138. Suffix string `xml:"Suffix"` // The file name for the index page
  139. }
  140. // ErrorDocument defines the 404 error page info
  141. type ErrorDocument struct {
  142. XMLName xml.Name `xml:"ErrorDocument"`
  143. Key string `xml:"Key"` // 404 error file name
  144. }
  145. // GetBucketWebsiteResult defines the result from GetBucketWebsite request.
  146. type GetBucketWebsiteResult WebsiteXML
  147. // CORSXML defines CORS configuration
  148. type CORSXML struct {
  149. XMLName xml.Name `xml:"CORSConfiguration"`
  150. CORSRules []CORSRule `xml:"CORSRule"` // CORS rules
  151. }
  152. // CORSRule defines CORS rules
  153. type CORSRule struct {
  154. XMLName xml.Name `xml:"CORSRule"`
  155. AllowedOrigin []string `xml:"AllowedOrigin"` // Allowed origins. By default it's wildcard '*'
  156. AllowedMethod []string `xml:"AllowedMethod"` // Allowed methods
  157. AllowedHeader []string `xml:"AllowedHeader"` // Allowed headers
  158. ExposeHeader []string `xml:"ExposeHeader"` // Allowed response headers
  159. MaxAgeSeconds int `xml:"MaxAgeSeconds"` // Max cache ages in seconds
  160. }
  161. // GetBucketCORSResult defines the result from GetBucketCORS request.
  162. type GetBucketCORSResult CORSXML
  163. // GetBucketInfoResult defines the result from GetBucketInfo request.
  164. type GetBucketInfoResult struct {
  165. XMLName xml.Name `xml:"BucketInfo"`
  166. BucketInfo BucketInfo `xml:"Bucket"`
  167. }
  168. // BucketInfo defines Bucket information
  169. type BucketInfo struct {
  170. XMLName xml.Name `xml:"Bucket"`
  171. Name string `xml:"Name"` // Bucket name
  172. Location string `xml:"Location"` // Bucket datacenter
  173. CreationDate time.Time `xml:"CreationDate"` // Bucket creation time
  174. ExtranetEndpoint string `xml:"ExtranetEndpoint"` // Bucket external endpoint
  175. IntranetEndpoint string `xml:"IntranetEndpoint"` // Bucket internal endpoint
  176. ACL string `xml:"AccessControlList>Grant"` // Bucket ACL
  177. Owner Owner `xml:"Owner"` // Bucket owner
  178. StorageClass string `xml:"StorageClass"` // Bucket storage class
  179. }
  180. // ListObjectsResult defines the result from ListObjects request
  181. type ListObjectsResult struct {
  182. XMLName xml.Name `xml:"ListBucketResult"`
  183. Prefix string `xml:"Prefix"` // The object prefix
  184. Marker string `xml:"Marker"` // The marker filter.
  185. MaxKeys int `xml:"MaxKeys"` // Max keys to return
  186. Delimiter string `xml:"Delimiter"` // The delimiter for grouping objects' name
  187. IsTruncated bool `xml:"IsTruncated"` // Flag indicates if all results are returned (when it's false)
  188. NextMarker string `xml:"NextMarker"` // The start point of the next query
  189. Objects []ObjectProperties `xml:"Contents"` // Object list
  190. CommonPrefixes []string `xml:"CommonPrefixes>Prefix"` // You can think of commonprefixes as "folders" whose names end with the delimiter
  191. }
  192. // ObjectProperties defines Objecct properties
  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 size
  198. ETag string `xml:"ETag"` // Object ETag
  199. Owner Owner `xml:"Owner"` // Object owner information
  200. LastModified time.Time `xml:"LastModified"` // Object last modified time
  201. StorageClass string `xml:"StorageClass"` // Object storage class (Standard, IA, Archive)
  202. }
  203. // Owner defines Bucket/Object's owner
  204. type Owner struct {
  205. XMLName xml.Name `xml:"Owner"`
  206. ID string `xml:"ID"` // Owner ID
  207. DisplayName string `xml:"DisplayName"` // Owner's display name
  208. }
  209. // CopyObjectResult defines result object of CopyObject
  210. type CopyObjectResult struct {
  211. XMLName xml.Name `xml:"CopyObjectResult"`
  212. LastModified time.Time `xml:"LastModified"` // New object's last modified time.
  213. ETag string `xml:"ETag"` // New object's ETag
  214. }
  215. // GetObjectACLResult defines result of GetObjectACL request
  216. type GetObjectACLResult GetBucketACLResult
  217. type deleteXML struct {
  218. XMLName xml.Name `xml:"Delete"`
  219. Objects []DeleteObject `xml:"Object"` // Objects to delete
  220. Quiet bool `xml:"Quiet"` // Flag of quiet mode.
  221. }
  222. // DeleteObject defines the struct for deleting object
  223. type DeleteObject struct {
  224. XMLName xml.Name `xml:"Object"`
  225. Key string `xml:"Key"` // Object name
  226. }
  227. // DeleteObjectsResult defines result of DeleteObjects request
  228. type DeleteObjectsResult struct {
  229. XMLName xml.Name `xml:"DeleteResult"`
  230. DeletedObjects []string `xml:"Deleted>Key"` // Deleted object list
  231. }
  232. // InitiateMultipartUploadResult defines result of InitiateMultipartUpload request
  233. type InitiateMultipartUploadResult struct {
  234. XMLName xml.Name `xml:"InitiateMultipartUploadResult"`
  235. Bucket string `xml:"Bucket"` // Bucket name
  236. Key string `xml:"Key"` // Object name to upload
  237. UploadID string `xml:"UploadId"` // Generated UploadId
  238. }
  239. // UploadPart defines the upload/copy part
  240. type UploadPart struct {
  241. XMLName xml.Name `xml:"Part"`
  242. PartNumber int `xml:"PartNumber"` // Part number
  243. ETag string `xml:"ETag"` // ETag value of the part's data
  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 defines result object of multipart copy request.
  256. type UploadPartCopyResult struct {
  257. XMLName xml.Name `xml:"CopyPartResult"`
  258. LastModified time.Time `xml:"LastModified"` // Last modified time
  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 defines result object of CompleteMultipartUploadRequest
  266. type CompleteMultipartUploadResult struct {
  267. XMLName xml.Name `xml:"CompleteMultipartUploadResult"`
  268. Location string `xml:"Location"` // Object URL
  269. Bucket string `xml:"Bucket"` // Bucket name
  270. ETag string `xml:"ETag"` // Object ETag
  271. Key string `xml:"Key"` // Object name
  272. }
  273. // ListUploadedPartsResult defines result object of ListUploadedParts
  274. type ListUploadedPartsResult struct {
  275. XMLName xml.Name `xml:"ListPartsResult"`
  276. Bucket string `xml:"Bucket"` // Bucket name
  277. Key string `xml:"Key"` // Object name
  278. UploadID string `xml:"UploadId"` // Upload ID
  279. NextPartNumberMarker string `xml:"NextPartNumberMarker"` // Next part number
  280. MaxParts int `xml:"MaxParts"` // Max parts count
  281. IsTruncated bool `xml:"IsTruncated"` // Flag indicates all entries returned.false: all entries returned.
  282. UploadedParts []UploadedPart `xml:"Part"` // Uploaded parts
  283. }
  284. // UploadedPart defines uploaded part
  285. type UploadedPart struct {
  286. XMLName xml.Name `xml:"Part"`
  287. PartNumber int `xml:"PartNumber"` // Part number
  288. LastModified time.Time `xml:"LastModified"` // Last modified time
  289. ETag string `xml:"ETag"` // ETag cache
  290. Size int `xml:"Size"` // Part size
  291. }
  292. // ListMultipartUploadResult defines result object of ListMultipartUpload
  293. type ListMultipartUploadResult struct {
  294. XMLName xml.Name `xml:"ListMultipartUploadsResult"`
  295. Bucket string `xml:"Bucket"` // Bucket name
  296. Delimiter string `xml:"Delimiter"` // Delimiter for grouping object.
  297. Prefix string `xml:"Prefix"` // Object prefix
  298. KeyMarker string `xml:"KeyMarker"` // Object key marker
  299. UploadIDMarker string `xml:"UploadIdMarker"` // UploadId marker
  300. NextKeyMarker string `xml:"NextKeyMarker"` // Next key marker, if not all entries returned.
  301. NextUploadIDMarker string `xml:"NextUploadIdMarker"` // Next uploadId marker, if not all entries returned.
  302. MaxUploads int `xml:"MaxUploads"` // Max uploads to return
  303. IsTruncated bool `xml:"IsTruncated"` // Flag indicates all entries are returned.
  304. Uploads []UncompletedUpload `xml:"Upload"` // Ongoing uploads (not completed, not aborted)
  305. CommonPrefixes []string `xml:"CommonPrefixes>Prefix"` // Common prefixes list.
  306. }
  307. // UncompletedUpload structure wraps an uncompleted upload task
  308. type UncompletedUpload struct {
  309. XMLName xml.Name `xml:"Upload"`
  310. Key string `xml:"Key"` // Object name
  311. UploadID string `xml:"UploadId"` // The UploadId
  312. Initiated time.Time `xml:"Initiated"` // Initialization time in the format such as 2012-02-23T04:18:23.000Z
  313. }
  314. // decodeDeleteObjectsResult decodes deleting objects result in URL encoding
  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. // decodeListObjectsResult decodes list objects result in URL encoding
  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. // decodeListMultipartUploadResult decodes list multipart upload result in URL encoding
  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 defines the configuration for creating a bucket.
  392. type createBucketConfiguration struct {
  393. XMLName xml.Name `xml:"CreateBucketConfiguration"`
  394. StorageClass StorageClassType `xml:"StorageClass,omitempty"`
  395. }