Last active
March 29, 2021 13:29
-
-
Save guilhem/6911ac77565890f623f1c6f55c8fdf9f to your computer and use it in GitHub Desktop.
azure blob io.ReaderAt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package blob | |
import ( | |
"context" | |
"io" | |
"github.com/Azure/azure-storage-blob-go/azblob" | |
) | |
type BlobReaderAt struct { | |
blobURL azblob.BlobURL | |
Context context.Context | |
cpk azblob.ClientProvidedKeyOptions | |
} | |
func NewBlobReaderAt(context context.Context, blobURL azblob.BlobURL, cpk azblob.ClientProvidedKeyOptions) *BlobReaderAt { | |
return &BlobReaderAt{ | |
blobURL: blobURL, | |
Context: context, | |
cpk: cpk, | |
} | |
} | |
func (b *BlobReaderAt) ReadAt(p []byte, offset int64) (n int, err error) { | |
down, err := b.blobURL.Download(b.Context, offset, int64(len(p)), azblob.BlobAccessConditions{}, false, b.cpk) | |
if err != nil { | |
return 0, err | |
} | |
r := down.Response().Body | |
defer r.Close() | |
n, err = io.ReadFull(r, p) | |
return | |
} | |
func (b *BlobReaderAt) SizeWithError() (int64, error) { | |
resp, err := b.blobURL.GetProperties(b.Context, azblob.BlobAccessConditions{}, b.cpk) | |
if err != nil { | |
return 0, err | |
} | |
return resp.ContentLength(), nil | |
} | |
func (b *BlobReaderAt) Size() int64 { | |
size, err := b.SizeWithError() | |
if err != nil { | |
return 0 | |
} | |
return size | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment