Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for listing R2 buckets #1063

Merged
merged 3 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1063.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
r2: Add support for listing R2 buckets
```
50 changes: 50 additions & 0 deletions r2_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudflare

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand All @@ -11,6 +12,55 @@ var (
ErrMissingBucketName = errors.New("require bucket name missing")
)

// R2Bucket defines a container for objects stored in R2 Storage.
type R2Bucket struct {
Name string `json:"name"`
CreationDate string `json:"creation_date,omitempty"`
}

// R2Buckets represents the map of buckets response from
// the R2 buckets endpoint.
type R2Buckets struct {
Buckets []R2Bucket `json:"buckets"`
}

// R2BucketListResponse represents the response from the list
// R2 buckets endpoint.
type R2BucketListResponse struct {
Result R2Buckets `json:"result"`
Response
}

type ListR2BucketsParams struct {
Name string `url:"name_contains,omitempty"`
StartAfter string `url:"start_after,omitempty"`
PerPage int64 `url:"per_page,omitempty"`
Order string `url:"order,omitempty"`
Direction string `url:"direction,omitempty"`
Cursor string `url:"cursor,omitempty"`
}

// ListR2Buckets Lists R2 buckets.
func (api *API) ListR2Buckets(ctx context.Context, rc *ResourceContainer, params ListR2BucketsParams) ([]R2Bucket, error) {
if rc.Identifier == "" {
return []R2Bucket{}, ErrMissingAccountID
}

uri := buildURI(fmt.Sprintf("/accounts/%s/r2/buckets", rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []R2Bucket{}, err
}

var r2BucketListResponse R2BucketListResponse
err = json.Unmarshal(res, &r2BucketListResponse)
if err != nil {
return []R2Bucket{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return r2BucketListResponse.Result.Buckets, nil
}

type CreateR2BucketParameters struct {
Name string `json:"name,omitempty"`
}
Expand Down
34 changes: 34 additions & 0 deletions r2_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ import (

const testBucketName = "example-bucket"

func TestR2_ListBuckets(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/accounts/%s/r2/buckets", testAccountID), func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"buckets": [
{
"name": "example-bucket",
"creation_date": "2022-06-24T19:58:49.477Z"
}
]
}
}`)
})

want := []R2Bucket{
{
Name: "example-bucket",
CreationDate: "2022-06-24T19:58:49.477Z",
},
}
actual, err := client.ListR2Buckets(context.Background(), AccountIdentifier(testAccountID), ListR2BucketsParams{})
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

func TestR2_CreateBucket(t *testing.T) {
setup()
defer teardown()
Expand Down