Skip to content

Commit

Permalink
feat: support read to io.Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
OasisLCrypto committed Nov 24, 2024
1 parent 5c2bf42 commit ac65b39
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The **walrus-go** SDK provides a Go client for interacting with the [Walrus](htt
- [Read](#read)
- [ReadToFile](#readtofile)
- [GetAPISpec](#getapispec)
- [ReadToReader](#readtoreader)
- [Contributing](#contributing)
- [License](#license)

Expand Down Expand Up @@ -235,6 +236,39 @@ if err != nil {
fmt.Println("File retrieved successfully")
```

### ReadToReader

Retrieves a blob and returns an io.ReadCloser for streaming the content.

```go
func (c *Client) ReadToReader(blobID string) (io.ReadCloser, error)
```

**Parameters:**

- `blobID string`: The blob ID to retrieve.

**Returns:**

- `io.ReadCloser`: A reader containing the blob content. Remember to close it after use.
- `error`: Error if the operation fails.

**Example:**

```go
reader, err := client.ReadToReader("your-blob-id")
if err != nil {
log.Fatalf("Error getting reader: %v", err)
}
defer reader.Close()

// Use the reader as needed
_, err = io.Copy(os.Stdout, reader)
if err != nil {
log.Fatalf("Error reading content: %v", err)
}
```

## API Reference

### Client
Expand Down Expand Up @@ -357,6 +391,39 @@ func (c *Client) GetAPISpec(isAggregator bool) ([]byte, error)
- `[]byte`: The API specification data.
- `error`: Error if the operation fails.

#### ReadToReader

Retrieves a blob and returns an io.ReadCloser for streaming the content.

```go
func (c *Client) ReadToReader(blobID string) (io.ReadCloser, error)
```

**Parameters:**

- `blobID string`: The blob ID to retrieve.

**Returns:**

- `io.ReadCloser`: A reader containing the blob content. Remember to close it after use.
- `error`: Error if the operation fails.

**Example:**

```go
reader, err := client.ReadToReader("your-blob-id")
if err != nil {
log.Fatalf("Error getting reader: %v", err)
}
defer reader.Close()

// Use the reader as needed
_, err = io.Copy(os.Stdout, reader)
if err != nil {
log.Fatalf("Error reading content: %v", err)
}
```

## Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Expand Down
18 changes: 18 additions & 0 deletions walrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,21 @@ func (c *Client) Head(blobID string) (*BlobMetadata, error) {

return metadata, nil
}

// ReadToReader retrieves a blob and writes it to the provided io.Writer
func (c *Client) ReadToReader(blobID string) (io.ReadCloser, error) {
urlStr := fmt.Sprintf("%s/v1/%s", c.AggregatorURL, url.PathEscape(blobID))

resp, err := c.httpClient.Get(urlStr)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
resp.Body.Close()
respData, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("failed to read blob: %s", string(respData))
}

return resp.Body, nil
}

0 comments on commit ac65b39

Please sign in to comment.