api-restore.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * (C) 2018-2021 MinIO, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package minio
  18. import (
  19. "bytes"
  20. "context"
  21. "encoding/xml"
  22. "net/http"
  23. "net/url"
  24. "github.com/minio/minio-go/v7/pkg/s3utils"
  25. "github.com/minio/minio-go/v7/pkg/tags"
  26. )
  27. // RestoreType represents the restore request type
  28. type RestoreType string
  29. const (
  30. // RestoreSelect represents the restore SELECT operation
  31. RestoreSelect = RestoreType("SELECT")
  32. )
  33. // TierType represents a retrieval tier
  34. type TierType string
  35. const (
  36. // TierStandard is the standard retrieval tier
  37. TierStandard = TierType("Standard")
  38. // TierBulk is the bulk retrieval tier
  39. TierBulk = TierType("Bulk")
  40. // TierExpedited is the expedited retrieval tier
  41. TierExpedited = TierType("Expedited")
  42. )
  43. // GlacierJobParameters represents the retrieval tier parameter
  44. type GlacierJobParameters struct {
  45. Tier TierType
  46. }
  47. // Encryption contains the type of server-side encryption used during object retrieval
  48. type Encryption struct {
  49. EncryptionType string
  50. KMSContext string
  51. KMSKeyID string `xml:"KMSKeyId"`
  52. }
  53. // MetadataEntry represents a metadata information of the restored object.
  54. type MetadataEntry struct {
  55. Name string
  56. Value string
  57. }
  58. // S3 holds properties of the copy of the archived object
  59. type S3 struct {
  60. AccessControlList *AccessControlList `xml:"AccessControlList,omiempty"`
  61. BucketName string
  62. Prefix string
  63. CannedACL *string `xml:"CannedACL,omitempty"`
  64. Encryption *Encryption `xml:"Encryption,omitempty"`
  65. StorageClass *string `xml:"StorageClass,omitempty"`
  66. Tagging *tags.Tags `xml:"Tagging,omitempty"`
  67. UserMetadata *MetadataEntry `xml:"UserMetadata,omitempty"`
  68. }
  69. // SelectParameters holds the select request parameters
  70. type SelectParameters struct {
  71. XMLName xml.Name `xml:"SelectParameters"`
  72. ExpressionType QueryExpressionType
  73. Expression string
  74. InputSerialization SelectObjectInputSerialization
  75. OutputSerialization SelectObjectOutputSerialization
  76. }
  77. // OutputLocation holds properties of the copy of the archived object
  78. type OutputLocation struct {
  79. XMLName xml.Name `xml:"OutputLocation"`
  80. S3 S3 `xml:"S3"`
  81. }
  82. // RestoreRequest holds properties of the restore object request
  83. type RestoreRequest struct {
  84. XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ RestoreRequest"`
  85. Type *RestoreType `xml:"Type,omitempty"`
  86. Tier *TierType `xml:"Tier,omitempty"`
  87. Days *int `xml:"Days,omitempty"`
  88. GlacierJobParameters *GlacierJobParameters `xml:"GlacierJobParameters,omitempty"`
  89. Description *string `xml:"Description,omitempty"`
  90. SelectParameters *SelectParameters `xml:"SelectParameters,omitempty"`
  91. OutputLocation *OutputLocation `xml:"OutputLocation,omitempty"`
  92. }
  93. // SetDays sets the days parameter of the restore request
  94. func (r *RestoreRequest) SetDays(v int) {
  95. r.Days = &v
  96. }
  97. // SetDays sets the GlacierJobParameters of the restore request
  98. func (r *RestoreRequest) SetGlacierJobParameters(v GlacierJobParameters) {
  99. r.GlacierJobParameters = &v
  100. }
  101. // SetType sets the type of the restore request
  102. func (r *RestoreRequest) SetType(v RestoreType) {
  103. r.Type = &v
  104. }
  105. // SetTier sets the retrieval tier of the restore request
  106. func (r *RestoreRequest) SetTier(v TierType) {
  107. r.Tier = &v
  108. }
  109. // SetDescription sets the description of the restore request
  110. func (r *RestoreRequest) SetDescription(v string) {
  111. r.Description = &v
  112. }
  113. // SetSelectParameters sets SelectParameters of the restore select request
  114. func (r *RestoreRequest) SetSelectParameters(v SelectParameters) {
  115. r.SelectParameters = &v
  116. }
  117. // SetOutputLocation sets the properties of the copy of the archived object
  118. func (r *RestoreRequest) SetOutputLocation(v OutputLocation) {
  119. r.OutputLocation = &v
  120. }
  121. // RestoreObject is a implementation of https://docs.aws.amazon.com/AmazonS3/latest/API/API_RestoreObject.html AWS S3 API
  122. func (c Client) RestoreObject(ctx context.Context, bucketName, objectName, versionID string, req RestoreRequest) error {
  123. // Input validation.
  124. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  125. return err
  126. }
  127. if err := s3utils.CheckValidObjectName(objectName); err != nil {
  128. return err
  129. }
  130. restoreRequestBytes, err := xml.Marshal(req)
  131. if err != nil {
  132. return err
  133. }
  134. urlValues := make(url.Values)
  135. urlValues.Set("restore", "")
  136. if versionID != "" {
  137. urlValues.Set("versionId", versionID)
  138. }
  139. // Execute POST on bucket/object.
  140. resp, err := c.executeMethod(ctx, http.MethodPost, requestMetadata{
  141. bucketName: bucketName,
  142. objectName: objectName,
  143. queryValues: urlValues,
  144. contentMD5Base64: sumMD5Base64(restoreRequestBytes),
  145. contentSHA256Hex: sum256Hex(restoreRequestBytes),
  146. contentBody: bytes.NewReader(restoreRequestBytes),
  147. contentLength: int64(len(restoreRequestBytes)),
  148. })
  149. defer closeResponse(resp)
  150. if err != nil {
  151. return err
  152. }
  153. if resp.StatusCode != http.StatusAccepted {
  154. return httpRespToErrorResponse(resp, bucketName, "")
  155. }
  156. return nil
  157. }