Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit 7c4a588

Browse files
committed
Add appender support
1 parent eb8c811 commit 7c4a588

File tree

5 files changed

+214
-7
lines changed

5 files changed

+214
-7
lines changed

generated.go

Lines changed: 143 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ name = "azblob"
66
required = ["credential", "endpoint"]
77
optional = ["default_service_pairs", "http_client_options"]
88

9+
[namespace.storage]
10+
implement = ["appender"]
11+
912
[namespace.storage.new]
1013
required = ["name"]
1114
optional = ["default_storage_pairs", "pair_policy", "work_dir"]
@@ -19,6 +22,12 @@ optional = ["offset", "io_callback", "size", "encryption_key", "encryption_scope
1922
[namespace.storage.op.write]
2023
optional = ["content_md5", "content_type", "io_callback", "access_tier", "encryption_key", "encryption_scope"]
2124

25+
[namespace.storage.op.create_append]
26+
optional = ["encryption_key", "encryption_scope"]
27+
28+
[namespace.storage.op.write_append]
29+
optional = ["encryption_key", "encryption_scope"]
30+
2231
[namespace.storage.op.stat]
2332
optional = ["encryption_key", "encryption_scope"]
2433

storage.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@ func (s *Storage) create(path string, opt pairStorageCreate) (o *Object) {
2121
return o
2222
}
2323

24+
func (s *Storage) createAppend(ctx context.Context, path string, opt pairStorageCreateAppend) (o *Object, err error) {
25+
rp := s.getAbsPath(path)
26+
27+
var cpk azblob.ClientProvidedKeyOptions
28+
if opt.HasEncryptionKey {
29+
cpk, err = calculateEncryptionHeaders(opt.EncryptionKey, opt.EncryptionScope)
30+
if err != nil {
31+
return
32+
}
33+
}
34+
_, err = s.bucket.NewAppendBlobURL(rp).Create(ctx, azblob.BlobHTTPHeaders{}, nil,
35+
azblob.BlobAccessConditions{}, nil, cpk)
36+
37+
if err != nil {
38+
return
39+
}
40+
41+
o = s.newObject(true)
42+
o.Mode = ModeRead | ModeAppend
43+
o.ID = rp
44+
o.Path = path
45+
o.SetAppendOffset(0)
46+
return o, nil
47+
}
48+
2449
func (s *Storage) delete(ctx context.Context, path string, opt pairStorageDelete) (err error) {
2550
rp := s.getAbsPath(path)
2651

@@ -257,3 +282,32 @@ func (s *Storage) write(ctx context.Context, path string, r io.Reader, size int6
257282
}
258283
return size, nil
259284
}
285+
286+
func (s *Storage) writeAppend(ctx context.Context, o *Object, r io.Reader, size int64, opt pairStorageWriteAppend) (n int64, err error) {
287+
rp := o.GetID()
288+
289+
var cpk azblob.ClientProvidedKeyOptions
290+
if opt.HasEncryptionKey {
291+
cpk, err = calculateEncryptionHeaders(opt.EncryptionKey, opt.EncryptionScope)
292+
if err != nil {
293+
return
294+
}
295+
}
296+
297+
appendResp, err := s.bucket.NewAppendBlobURL(rp).AppendBlock(
298+
ctx, iowrap.SizedReadSeekCloser(r, size),
299+
azblob.AppendBlobAccessConditions{}, nil, cpk)
300+
301+
if err != nil {
302+
return
303+
}
304+
305+
offset, err := strconv.ParseInt(appendResp.BlobAppendOffset(), 10, 64)
306+
if err != nil {
307+
return
308+
}
309+
offset += size
310+
o.SetAppendOffset(offset)
311+
312+
return offset, nil
313+
}

tests/storage_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ func TestStorage(t *testing.T) {
1313
}
1414
tests.TestStorager(t, setupTest(t))
1515
}
16+
17+
func TestAppend(t *testing.T) {
18+
if os.Getenv("STORAGE_AZBLOB_INTEGRATION_TEST") != "on" {
19+
t.Skipf("STORAGE_AZBLOB_INTEGRATION_TEST is not 'on', skipped")
20+
}
21+
tests.TestAppender(t, setupTest(t))
22+
}

utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Storage struct {
4646
pairPolicy typ.PairPolicy
4747

4848
typ.UnimplementedStorager
49+
typ.UnimplementedAppender
4950
}
5051

5152
// String implements Storager.String

0 commit comments

Comments
 (0)