@@ -3,6 +3,7 @@ package buildkitelogs
33import (
44 "context"
55 "fmt"
6+ "io"
67 "os"
78 "runtime"
89 "time"
@@ -15,7 +16,6 @@ import (
1516// BlobStorage provides an abstraction over blob storage backends
1617type BlobStorage struct {
1718 bucket * blob.Bucket
18- ctx context.Context
1919}
2020
2121// BlobMetadata contains metadata for cached blobs
@@ -46,7 +46,6 @@ func NewBlobStorage(ctx context.Context, storageURL string) (*BlobStorage, error
4646
4747 return & BlobStorage {
4848 bucket : bucket ,
49- ctx : ctx ,
5049 }, nil
5150}
5251
@@ -115,12 +114,12 @@ func GenerateBlobKey(org, pipeline, build, job string) string {
115114}
116115
117116// Exists checks if a blob exists in storage
118- func (bs * BlobStorage ) Exists (key string ) (bool , error ) {
119- return bs .bucket .Exists (bs . ctx , key )
117+ func (bs * BlobStorage ) Exists (ctx context. Context , key string ) (bool , error ) {
118+ return bs .bucket .Exists (ctx , key )
120119}
121120
122121// WriteWithMetadata writes data to blob storage with metadata
123- func (bs * BlobStorage ) WriteWithMetadata (key string , data []byte , metadata * BlobMetadata ) error {
122+ func (bs * BlobStorage ) WriteWithMetadata (ctx context. Context , key string , data []byte , metadata * BlobMetadata ) error {
124123 opts := & blob.WriterOptions {}
125124
126125 if metadata != nil {
@@ -136,7 +135,7 @@ func (bs *BlobStorage) WriteWithMetadata(key string, data []byte, metadata *Blob
136135 }
137136 }
138137
139- writer , err := bs .bucket .NewWriter (bs . ctx , key , opts )
138+ writer , err := bs .bucket .NewWriter (ctx , key , opts )
140139 if err != nil {
141140 return fmt .Errorf ("failed to create blob writer: %w" , err )
142141 }
@@ -150,23 +149,11 @@ func (bs *BlobStorage) WriteWithMetadata(key string, data []byte, metadata *Blob
150149}
151150
152151// ReadWithMetadata reads data from blob storage with metadata
153- func (bs * BlobStorage ) ReadWithMetadata (key string ) ([]byte , * BlobMetadata , error ) {
154- reader , err := bs .bucket .NewReader (bs .ctx , key , nil )
155- if err != nil {
156- return nil , nil , fmt .Errorf ("failed to create blob reader: %w" , err )
157- }
158- defer reader .Close ()
159-
160- // Read data
161- data := make ([]byte , reader .Size ())
162- if _ , err := reader .Read (data ); err != nil {
163- return nil , nil , fmt .Errorf ("failed to read blob data: %w" , err )
164- }
165-
152+ func (bs * BlobStorage ) ReadWithMetadata (ctx context.Context , key string ) (* BlobMetadata , error ) {
166153 // Get blob attributes for metadata
167- attrs , err := bs .bucket .Attributes (bs . ctx , key )
154+ attrs , err := bs .bucket .Attributes (ctx , key )
168155 if err != nil {
169- return nil , nil , fmt .Errorf ("failed to get blob attributes: %w" , err )
156+ return nil , fmt .Errorf ("failed to get blob attributes: %w" , err )
170157 }
171158
172159 // Extract metadata
@@ -190,21 +177,27 @@ func (bs *BlobStorage) ReadWithMetadata(key string) ([]byte, *BlobMetadata, erro
190177 }
191178 }
192179
193- return data , metadata , nil
180+ return metadata , nil
181+ }
182+
183+ // Reader returns an io.ReadCloser for streaming blob data from the specified key.
184+ // The caller is responsible for closing the returned reader when done.
185+ func (bs * BlobStorage ) Reader (ctx context.Context , key string ) (io.ReadCloser , error ) {
186+ return bs .bucket .NewReader (ctx , key , nil )
194187}
195188
196189// GetModTime returns the modification time of a blob
197- func (bs * BlobStorage ) GetModTime (key string ) (time.Time , error ) {
198- attrs , err := bs .bucket .Attributes (bs . ctx , key )
190+ func (bs * BlobStorage ) GetModTime (ctx context. Context , key string ) (time.Time , error ) {
191+ attrs , err := bs .bucket .Attributes (ctx , key )
199192 if err != nil {
200193 return time.Time {}, fmt .Errorf ("failed to get blob attributes: %w" , err )
201194 }
202195 return attrs .ModTime , nil
203196}
204197
205198// Delete removes a blob from storage
206- func (bs * BlobStorage ) Delete (key string ) error {
207- return bs .bucket .Delete (bs . ctx , key )
199+ func (bs * BlobStorage ) Delete (ctx context. Context , key string ) error {
200+ return bs .bucket .Delete (ctx , key )
208201}
209202
210203// Close closes the blob storage connection
0 commit comments