| 1 | package containers |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/Azure/go-autorest/autorest" |
| 9 | "github.com/Azure/go-autorest/autorest/azure" |
| 10 | "github.com/Azure/go-autorest/autorest/validation" |
| 11 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 12 | ) |
| 13 | |
| 14 | type ListBlobsInput struct { |
| 15 | Delimiter *string |
| 16 | Include *[]Dataset |
| 17 | Marker *string |
| 18 | MaxResults *int |
| 19 | Prefix *string |
| 20 | } |
| 21 | |
| 22 | type ListBlobsResult struct { |
| 23 | autorest.Response |
| 24 | |
| 25 | Delimiter string `xml:"Delimiter"` |
| 26 | Marker string `xml:"Marker"` |
| 27 | MaxResults int `xml:"MaxResults"` |
| 28 | NextMarker *string `xml:"NextMarker,omitempty"` |
| 29 | Prefix string `xml:"Prefix"` |
| 30 | Blobs Blobs `xml:"Blobs"` |
| 31 | } |
| 32 | |
| 33 | type Blobs struct { |
| 34 | Blobs []BlobDetails `xml:"Blob"` |
| 35 | BlobPrefix *BlobPrefix `xml:"BlobPrefix"` |
| 36 | } |
| 37 | |
| 38 | type BlobDetails struct { |
| 39 | Name string `xml:"Name"` |
| 40 | Deleted bool `xml:"Deleted,omitempty"` |
| 41 | MetaData map[string]interface{} `map:"Metadata,omitempty"` |
| 42 | Properties *BlobProperties `xml:"Properties,omitempty"` |
| 43 | Snapshot *string `xml:"Snapshot,omitempty"` |
| 44 | } |
| 45 | |
| 46 | type BlobProperties struct { |
| 47 | AccessTier *string `xml:"AccessTier,omitempty"` |
| 48 | AccessTierInferred *bool `xml:"AccessTierInferred,omitempty"` |
| 49 | AccessTierChangeTime *string `xml:"AccessTierChangeTime,omitempty"` |
| 50 | BlobType *string `xml:"BlobType,omitempty"` |
| 51 | BlobSequenceNumber *string `xml:"x-ms-blob-sequence-number,omitempty"` |
| 52 | CacheControl *string `xml:"Cache-Control,omitempty"` |
| 53 | ContentEncoding *string `xml:"ContentEncoding,omitempty"` |
| 54 | ContentLanguage *string `xml:"Content-Language,omitempty"` |
| 55 | ContentLength *int64 `xml:"Content-Length,omitempty"` |
| 56 | ContentMD5 *string `xml:"Content-MD5,omitempty"` |
| 57 | ContentType *string `xml:"Content-Type,omitempty"` |
| 58 | CopyCompletionTime *string `xml:"CopyCompletionTime,omitempty"` |
| 59 | CopyId *string `xml:"CopyId,omitempty"` |
| 60 | CopyStatus *string `xml:"CopyStatus,omitempty"` |
| 61 | CopySource *string `xml:"CopySource,omitempty"` |
| 62 | CopyProgress *string `xml:"CopyProgress,omitempty"` |
| 63 | CopyStatusDescription *string `xml:"CopyStatusDescription,omitempty"` |
| 64 | CreationTime *string `xml:"CreationTime,omitempty"` |
| 65 | ETag *string `xml:"Etag,omitempty"` |
| 66 | DeletedTime *string `xml:"DeletedTime,omitempty"` |
| 67 | IncrementalCopy *bool `xml:"IncrementalCopy,omitempty"` |
| 68 | LastModified *string `xml:"Last-Modified,omitempty"` |
| 69 | LeaseDuration *string `xml:"LeaseDuration,omitempty"` |
| 70 | LeaseState *string `xml:"LeaseState,omitempty"` |
| 71 | LeaseStatus *string `xml:"LeaseStatus,omitempty"` |
| 72 | RemainingRetentionDays *string `xml:"RemainingRetentionDays,omitempty"` |
| 73 | ServerEncrypted *bool `xml:"ServerEncrypted,omitempty"` |
| 74 | } |
| 75 | |
| 76 | type BlobPrefix struct { |
| 77 | Name string `xml:"Name"` |
| 78 | } |
| 79 | |
| 80 | // ListBlobs lists the blobs matching the specified query within the specified Container |
| 81 | func (client Client) ListBlobs(ctx context.Context, accountName, containerName string, input ListBlobsInput) (result ListBlobsResult, err error) { |
| 82 | if accountName == "" { |
| 83 | return result, validation.NewError("containers.Client", "ListBlobs", "`accountName` cannot be an empty string.") |
| 84 | } |
| 85 | if containerName == "" { |
| 86 | return result, validation.NewError("containers.Client", "ListBlobs", "`containerName` cannot be an empty string.") |
| 87 | } |
| 88 | if input.MaxResults != nil && (*input.MaxResults <= 0 || *input.MaxResults > 5000) { |
| 89 | return result, validation.NewError("containers.Client", "ListBlobs", "`input.MaxResults` can either be nil or between 0 and 5000.") |
| 90 | } |
| 91 | |
| 92 | req, err := client.ListBlobsPreparer(ctx, accountName, containerName, input) |
| 93 | if err != nil { |
| 94 | err = autorest.NewErrorWithError(err, "containers.Client", "ListBlobs", nil, "Failure preparing request") |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | resp, err := client.ListBlobsSender(req) |
| 99 | if err != nil { |
| 100 | result.Response = autorest.Response{Response: resp} |
| 101 | err = autorest.NewErrorWithError(err, "containers.Client", "ListBlobs", resp, "Failure sending request") |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | result, err = client.ListBlobsResponder(resp) |
| 106 | if err != nil { |
| 107 | err = autorest.NewErrorWithError(err, "containers.Client", "ListBlobs", resp, "Failure responding to request") |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | return |
| 112 | } |
| 113 | |
| 114 | // ListBlobsPreparer prepares the ListBlobs request. |
| 115 | func (client Client) ListBlobsPreparer(ctx context.Context, accountName, containerName string, input ListBlobsInput) (*http.Request, error) { |
| 116 | pathParameters := map[string]interface{}{ |
| 117 | "containerName": autorest.Encode("path", containerName), |
| 118 | } |
| 119 | |
| 120 | queryParameters := map[string]interface{}{ |
| 121 | "comp": autorest.Encode("query", "list"), |
| 122 | "restype": autorest.Encode("query", "container"), |
| 123 | } |
| 124 | |
| 125 | if input.Delimiter != nil { |
| 126 | queryParameters["delimiter"] = autorest.Encode("query", *input.Delimiter) |
| 127 | } |
| 128 | if input.Include != nil { |
| 129 | vals := make([]string, 0) |
| 130 | for _, v := range *input.Include { |
| 131 | vals = append(vals, string(v)) |
| 132 | } |
| 133 | include := strings.Join(vals, ",") |
| 134 | queryParameters["include"] = autorest.Encode("query", include) |
| 135 | } |
| 136 | if input.Marker != nil { |
| 137 | queryParameters["marker"] = autorest.Encode("query", *input.Marker) |
| 138 | } |
| 139 | if input.MaxResults != nil { |
| 140 | queryParameters["maxresults"] = autorest.Encode("query", *input.MaxResults) |
| 141 | } |
| 142 | if input.Prefix != nil { |
| 143 | queryParameters["prefix"] = autorest.Encode("query", *input.Prefix) |
| 144 | } |
| 145 | |
| 146 | headers := map[string]interface{}{ |
| 147 | "x-ms-version": APIVersion, |
| 148 | } |
| 149 | |
| 150 | preparer := autorest.CreatePreparer( |
| 151 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 152 | autorest.AsGet(), |
| 153 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 154 | autorest.WithPathParameters("/{containerName}", pathParameters), |
| 155 | autorest.WithQueryParameters(queryParameters), |
| 156 | autorest.WithHeaders(headers)) |
| 157 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 158 | } |
| 159 | |
| 160 | // ListBlobsSender sends the ListBlobs request. The method will close the |
| 161 | // http.Response Body if it receives an error. |
| 162 | func (client Client) ListBlobsSender(req *http.Request) (*http.Response, error) { |
| 163 | return autorest.SendWithSender(client, req, |
| 164 | azure.DoRetryWithRegistration(client.Client)) |
| 165 | } |
| 166 | |
| 167 | // ListBlobsResponder handles the response to the ListBlobs request. The method always |
| 168 | // closes the http.Response Body. |
| 169 | func (client Client) ListBlobsResponder(resp *http.Response) (result ListBlobsResult, err error) { |
| 170 | err = autorest.Respond( |
| 171 | resp, |
| 172 | client.ByInspecting(), |
| 173 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 174 | autorest.ByUnmarshallingXML(&result), |
| 175 | autorest.ByClosing()) |
| 176 | result.Response = autorest.Response{Response: resp} |
| 177 | |
| 178 | return |
| 179 | } |