@@ -194,14 +194,12 @@ func TestBlobClientSASKey_FGetObject(t *testing.T) {
194
194
localPath := filepath .Join (tempDir , testFile )
195
195
196
196
// use the shared key client to create a SAS key for the account
197
- sasKey , err := client .GetSASToken (azblob.AccountSASResourceTypes {Object : true , Container : true },
197
+ sasKey , err := client .ServiceClient . GetSASURL (azblob.AccountSASResourceTypes {Object : true , Container : true },
198
198
azblob.AccountSASPermissions {List : true , Read : true },
199
- azblob.AccountSASServices {Blob : true },
200
199
time .Now (),
201
200
time .Now ().Add (48 * time .Hour ))
202
201
g .Expect (err ).ToNot (HaveOccurred ())
203
202
g .Expect (sasKey ).ToNot (BeEmpty ())
204
-
205
203
// the sdk returns the full SAS url e.g test.blob.core.windows.net/?<actual-sas-token>
206
204
sasKey = strings .TrimPrefix (sasKey , testBucket .Spec .Endpoint + "/" )
207
205
testSASKeySecret := corev1.Secret {
@@ -213,9 +211,14 @@ func TestBlobClientSASKey_FGetObject(t *testing.T) {
213
211
sasKeyClient , err := NewClient (testBucket .DeepCopy (), testSASKeySecret .DeepCopy ())
214
212
g .Expect (err ).ToNot (HaveOccurred ())
215
213
216
- // Test if blob exists using sasKey.
214
+ // Test if bucket and blob exists using sasKey.
217
215
ctx , timeout = context .WithTimeout (context .Background (), testTimeout )
218
216
defer timeout ()
217
+
218
+ ok , err := sasKeyClient .BucketExists (ctx , testContainer )
219
+ g .Expect (err ).ToNot (HaveOccurred ())
220
+ g .Expect (ok ).To (BeTrue ())
221
+
219
222
_ , err = sasKeyClient .FGetObject (ctx , testContainer , testFile , localPath )
220
223
221
224
g .Expect (err ).ToNot (HaveOccurred ())
@@ -224,6 +227,66 @@ func TestBlobClientSASKey_FGetObject(t *testing.T) {
224
227
g .Expect (f ).To (Equal ([]byte (testFileData )))
225
228
}
226
229
230
+ func TestBlobClientContainerSASKey_BucketExists (t * testing.T ) {
231
+ g := NewWithT (t )
232
+
233
+ // create a client with the shared key
234
+ client , err := NewClient (testBucket .DeepCopy (), testSecret .DeepCopy ())
235
+ g .Expect (err ).ToNot (HaveOccurred ())
236
+ g .Expect (client ).ToNot (BeNil ())
237
+
238
+ g .Expect (client .CanGetAccountSASToken ()).To (BeTrue ())
239
+
240
+ // Generate test container name.
241
+ testContainer := generateString (testContainerGenerateName )
242
+
243
+ // Create test container.
244
+ ctx , timeout := context .WithTimeout (context .Background (), testTimeout )
245
+ defer timeout ()
246
+ g .Expect (createContainer (ctx , client , testContainer )).To (Succeed ())
247
+ t .Cleanup (func () {
248
+ g .Expect (deleteContainer (context .Background (), client , testContainer )).To (Succeed ())
249
+ })
250
+
251
+ // Create test blob.
252
+ ctx , timeout = context .WithTimeout (context .Background (), testTimeout )
253
+ defer timeout ()
254
+ g .Expect (createBlob (ctx , client , testContainer , testFile , testFileData ))
255
+
256
+ // use the container client to create a container-level SAS key for the account
257
+ containerClient , err := client .ServiceClient .NewContainerClient (testContainer )
258
+ g .Expect (err ).ToNot (HaveOccurred ())
259
+ // sasKey
260
+ sasKey , err := containerClient .GetSASURL (azblob.ContainerSASPermissions {Read : true , List : true },
261
+ time .Now (),
262
+ time .Now ().Add (48 * time .Hour ))
263
+ g .Expect (err ).ToNot (HaveOccurred ())
264
+ g .Expect (sasKey ).ToNot (BeEmpty ())
265
+ // the sdk returns the full SAS url e.g test.blob.core.windows.net/<container-name>?<actual-sas-token>
266
+ sasKey = strings .TrimPrefix (sasKey , testBucket .Spec .Endpoint + "/" + testContainer )
267
+ testSASKeySecret := corev1.Secret {
268
+ Data : map [string ][]byte {
269
+ sasKeyField : []byte (sasKey ),
270
+ },
271
+ }
272
+
273
+ sasKeyClient , err := NewClient (testBucket .DeepCopy (), testSASKeySecret .DeepCopy ())
274
+ g .Expect (err ).ToNot (HaveOccurred ())
275
+
276
+ ctx , timeout = context .WithTimeout (context .Background (), testTimeout )
277
+ defer timeout ()
278
+
279
+ // Test if bucket and blob exists using sasKey.
280
+ ok , err := sasKeyClient .BucketExists (ctx , testContainer )
281
+ g .Expect (err ).ToNot (HaveOccurred ())
282
+ g .Expect (ok ).To (BeTrue ())
283
+
284
+ // Test that this still works if bucket doesn't exist
285
+ ok , err = client .BucketExists (ctx , "non-existent" )
286
+ g .Expect (err ).ToNot (HaveOccurred ())
287
+ g .Expect (ok ).To (BeFalse ())
288
+ }
289
+
227
290
func TestBlobClient_FGetObject_NotFoundErr (t * testing.T ) {
228
291
g := NewWithT (t )
229
292
@@ -340,8 +403,15 @@ func createContainer(ctx context.Context, client *BlobClient, name string) error
340
403
}
341
404
342
405
func createBlob (ctx context.Context , client * BlobClient , containerName , name , data string ) error {
343
- container := client .NewContainerClient (containerName )
344
- blob := container .NewAppendBlobClient (name )
406
+ container , err := client .NewContainerClient (containerName )
407
+ if err != nil {
408
+ return err
409
+ }
410
+
411
+ blob , err := container .NewAppendBlobClient (name )
412
+ if err != nil {
413
+ return err
414
+ }
345
415
346
416
ctx , timeout := context .WithTimeout (context .Background (), testTimeout )
347
417
defer timeout ()
@@ -350,7 +420,7 @@ func createBlob(ctx context.Context, client *BlobClient, containerName, name, da
350
420
}
351
421
352
422
hash := md5 .Sum ([]byte (data ))
353
- if _ , err := blob .AppendBlock (ctx , streaming .NopCloser (strings .NewReader (data )), & azblob.AppendBlockOptions {
423
+ if _ , err := blob .AppendBlock (ctx , streaming .NopCloser (strings .NewReader (data )), & azblob.AppendBlobAppendBlockOptions {
354
424
TransactionalContentMD5 : hash [:16 ],
355
425
}); err != nil {
356
426
return err
0 commit comments