|
| 1 | +//go:build gcp |
| 2 | + |
| 3 | +// Package backend contains core/backend interface implementations for supported backend providers. |
| 4 | +/* |
| 5 | + * Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 6 | + */ |
| 7 | +package backend |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/xml" |
| 11 | + "errors" |
| 12 | + "fmt" |
| 13 | + "io" |
| 14 | + "net/http" |
| 15 | + "net/url" |
| 16 | + "strconv" |
| 17 | + |
| 18 | + "github.com/NVIDIA/aistore/api/apc" |
| 19 | + "github.com/NVIDIA/aistore/cmn" |
| 20 | + "github.com/NVIDIA/aistore/cmn/cos" |
| 21 | + "github.com/NVIDIA/aistore/cmn/nlog" |
| 22 | + "github.com/NVIDIA/aistore/core" |
| 23 | +) |
| 24 | + |
| 25 | +// NOTE: Google's XML API is a compatibility layer for S3 clients and implements a subset of S3's control plane — buckets, ACLs, multipart, versioning. |
| 26 | +// This API exists separately from the Google's Go client library (cloud.google.com/go/storage) that implements regular GET, PUT, HEAD, etc. |
| 27 | + |
| 28 | +type ( |
| 29 | + // XML response structures for GCP multipart upload |
| 30 | + initiateMptUploadResult struct { |
| 31 | + XMLName xml.Name `xml:"InitiateMultipartUploadResult"` |
| 32 | + Bucket string `xml:"Bucket"` |
| 33 | + Key string `xml:"Key"` |
| 34 | + UploadID string `xml:"UploadId"` |
| 35 | + } |
| 36 | + |
| 37 | + completeMptUploadResult struct { |
| 38 | + XMLName xml.Name `xml:"CompleteMultipartUploadResult"` |
| 39 | + Location string `xml:"Location"` |
| 40 | + Bucket string `xml:"Bucket"` |
| 41 | + Key string `xml:"Key"` |
| 42 | + ETag string `xml:"ETag"` |
| 43 | + } |
| 44 | + |
| 45 | + completedPart struct { |
| 46 | + PartNumber int32 `xml:"PartNumber"` |
| 47 | + ETag string `xml:"ETag"` |
| 48 | + } |
| 49 | + |
| 50 | + completeMultipartUpload struct { |
| 51 | + XMLName xml.Name `xml:"CompleteMultipartUpload"` |
| 52 | + Parts []completedPart `xml:"Part"` |
| 53 | + } |
| 54 | +) |
| 55 | + |
| 56 | +func (gsbp *gsbp) StartMpt(lom *core.LOM, _ *http.Request) (id string, ecode int, err error) { |
| 57 | + cloudBck := lom.Bck().RemoteBck() |
| 58 | + |
| 59 | + reqArgs := cmn.AllocHra() |
| 60 | + { |
| 61 | + reqArgs.Method = http.MethodPost |
| 62 | + reqArgs.Base = gcpXMLEndpoint |
| 63 | + reqArgs.Path = cos.JoinPath(cloudBck.Name, lom.ObjName) |
| 64 | + reqArgs.Header = http.Header{ |
| 65 | + cos.HdrContentLength: []string{"0"}, |
| 66 | + } |
| 67 | + reqArgs.Query = url.Values{ |
| 68 | + apc.QparamMptUploads: []string{""}, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + req, err := reqArgs.Req() |
| 73 | + if err != nil { |
| 74 | + cmn.FreeHra(reqArgs) |
| 75 | + return "", http.StatusInternalServerError, err |
| 76 | + } |
| 77 | + |
| 78 | + resp, err := gsbp.httpClient.Do(req) |
| 79 | + cmn.FreeHra(reqArgs) |
| 80 | + cmn.HreqFree(req) |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + return "", http.StatusInternalServerError, err |
| 84 | + } |
| 85 | + defer resp.Body.Close() |
| 86 | + |
| 87 | + if resp.StatusCode != http.StatusOK { |
| 88 | + body, _ := io.ReadAll(resp.Body) |
| 89 | + err = fmt.Errorf("gcp: failed to initiate multipart upload: %s (status: %d)", string(body), resp.StatusCode) |
| 90 | + return "", resp.StatusCode, err |
| 91 | + } |
| 92 | + |
| 93 | + var result initiateMptUploadResult |
| 94 | + if err = xml.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 95 | + return "", http.StatusInternalServerError, fmt.Errorf("failed to decode response: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + if cmn.Rom.V(5, cos.ModBackend) { |
| 99 | + nlog.Infof("[start_mpt] %s, upload_id: %s", cloudBck.Cname(lom.ObjName), result.UploadID) |
| 100 | + } |
| 101 | + |
| 102 | + return result.UploadID, 0, nil |
| 103 | +} |
| 104 | + |
| 105 | +func (gsbp *gsbp) PutMptPart(lom *core.LOM, r io.ReadCloser, _ *http.Request, uploadID string, size int64, partNum int32) (string, int, error) { |
| 106 | + cloudBck := lom.Bck().RemoteBck() |
| 107 | + |
| 108 | + reqArgs := cmn.AllocHra() |
| 109 | + { |
| 110 | + reqArgs.Method = http.MethodPut |
| 111 | + reqArgs.Base = gcpXMLEndpoint |
| 112 | + reqArgs.Path = cos.JoinPath(cloudBck.Name, lom.ObjName) |
| 113 | + reqArgs.BodyR = r |
| 114 | + reqArgs.Query = url.Values{ |
| 115 | + apc.QparamMptPartNo: []string{strconv.FormatInt(int64(partNum), 10)}, |
| 116 | + apc.QparamMptUploadID: []string{uploadID}, |
| 117 | + } |
| 118 | + reqArgs.Header = http.Header{ |
| 119 | + cos.HdrContentLength: []string{strconv.FormatInt(size, 10)}, |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + req, err := reqArgs.Req() |
| 124 | + if err != nil { |
| 125 | + cmn.FreeHra(reqArgs) |
| 126 | + cos.Close(r) |
| 127 | + return "", http.StatusInternalServerError, err |
| 128 | + } |
| 129 | + req.ContentLength = size |
| 130 | + |
| 131 | + resp, err := gsbp.httpClient.Do(req) |
| 132 | + cmn.FreeHra(reqArgs) |
| 133 | + cmn.HreqFree(req) |
| 134 | + cos.Close(r) |
| 135 | + |
| 136 | + if err != nil { |
| 137 | + return "", http.StatusInternalServerError, err |
| 138 | + } |
| 139 | + defer resp.Body.Close() |
| 140 | + |
| 141 | + if resp.StatusCode != http.StatusOK { |
| 142 | + body, _ := io.ReadAll(resp.Body) |
| 143 | + err = fmt.Errorf("gcp: failed to upload part: %s (status: %d)", string(body), resp.StatusCode) |
| 144 | + return "", resp.StatusCode, err |
| 145 | + } |
| 146 | + |
| 147 | + etag := resp.Header.Get("ETag") |
| 148 | + if etag == "" { |
| 149 | + return "", http.StatusInternalServerError, errors.New("gcp: no ETag in response") |
| 150 | + } |
| 151 | + |
| 152 | + if cmn.Rom.V(5, cos.ModBackend) { |
| 153 | + nlog.Infof("[put_mpt_part] %s, part: %d, etag: %s", cloudBck.Cname(lom.ObjName), partNum, etag) |
| 154 | + } |
| 155 | + |
| 156 | + return etag, 0, nil |
| 157 | +} |
| 158 | + |
| 159 | +func (gsbp *gsbp) CompleteMpt(lom *core.LOM, _ *http.Request, uploadID string, _ []byte, parts apc.MptCompletedParts) (version, etag string, _ int, _ error) { |
| 160 | + cloudBck := lom.Bck().RemoteBck() |
| 161 | + |
| 162 | + // Build XML body with completed parts |
| 163 | + completeMpt := completeMultipartUpload{ |
| 164 | + Parts: make([]completedPart, len(parts)), |
| 165 | + } |
| 166 | + for i, part := range parts { |
| 167 | + completeMpt.Parts[i] = completedPart{ |
| 168 | + PartNumber: int32(part.PartNumber), |
| 169 | + ETag: part.ETag, |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + xmlBody, err := xml.Marshal(completeMpt) |
| 174 | + if err != nil { |
| 175 | + return "", "", http.StatusInternalServerError, fmt.Errorf("failed to marshal XML: %w", err) |
| 176 | + } |
| 177 | + |
| 178 | + reqArgs := cmn.AllocHra() |
| 179 | + { |
| 180 | + reqArgs.Method = http.MethodPost |
| 181 | + reqArgs.Base = gcpXMLEndpoint |
| 182 | + reqArgs.Path = cos.JoinPath(cloudBck.Name, lom.ObjName) |
| 183 | + reqArgs.Body = xmlBody |
| 184 | + reqArgs.Header = http.Header{ |
| 185 | + cos.HdrContentType: []string{cos.ContentXML}, |
| 186 | + cos.HdrContentLength: []string{strconv.Itoa(len(xmlBody))}, |
| 187 | + } |
| 188 | + reqArgs.Query = url.Values{ |
| 189 | + apc.QparamMptUploadID: []string{uploadID}, |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + req, err := reqArgs.Req() |
| 194 | + if err != nil { |
| 195 | + cmn.FreeHra(reqArgs) |
| 196 | + return "", "", http.StatusInternalServerError, err |
| 197 | + } |
| 198 | + |
| 199 | + resp, err := gsbp.httpClient.Do(req) |
| 200 | + cmn.FreeHra(reqArgs) |
| 201 | + cmn.HreqFree(req) |
| 202 | + |
| 203 | + if err != nil { |
| 204 | + return "", "", http.StatusInternalServerError, err |
| 205 | + } |
| 206 | + defer resp.Body.Close() |
| 207 | + |
| 208 | + if resp.StatusCode != http.StatusOK { |
| 209 | + body, _ := io.ReadAll(resp.Body) |
| 210 | + err = fmt.Errorf("gcp: failed to complete multipart upload: %s (status: %d)", string(body), resp.StatusCode) |
| 211 | + return "", "", resp.StatusCode, err |
| 212 | + } |
| 213 | + |
| 214 | + var result completeMptUploadResult |
| 215 | + if err = xml.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 216 | + return "", "", http.StatusInternalServerError, fmt.Errorf("failed to decode response: %w", err) |
| 217 | + } |
| 218 | + |
| 219 | + // Decode ETag |
| 220 | + if result.ETag != "" { |
| 221 | + if e, ok := cmn.BackendHelpers.Google.EncodeETag(result.ETag); ok { |
| 222 | + etag = e |
| 223 | + } else { |
| 224 | + etag = result.ETag |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + if cmn.Rom.V(5, cos.ModBackend) { |
| 229 | + nlog.Infof("[complete_mpt] %s, version: %s, etag: %s", cloudBck.Cname(lom.ObjName), version, etag) |
| 230 | + } |
| 231 | + |
| 232 | + return version, etag, 0, nil |
| 233 | +} |
0 commit comments