@@ -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 .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,68 @@ 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 .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
+ // BucketExists returns a good error message if the bucket doesn't exist with container level SAS
285
+ // since the error code is AuthenticationFailed.
286
+ ok , err = sasKeyClient .BucketExists (ctx , "non-existent" )
287
+ g .Expect (err ).To (HaveOccurred ())
288
+ g .Expect (err .Error ()).To (ContainSubstring ("Bucket might not exist" ))
289
+ g .Expect (ok ).To (BeFalse ())
290
+ }
291
+
227
292
func TestBlobClient_FGetObject_NotFoundErr (t * testing.T ) {
228
293
g := NewWithT (t )
229
294
@@ -340,8 +405,15 @@ func createContainer(ctx context.Context, client *BlobClient, name string) error
340
405
}
341
406
342
407
func createBlob (ctx context.Context , client * BlobClient , containerName , name , data string ) error {
343
- container := client .NewContainerClient (containerName )
344
- blob := container .NewAppendBlobClient (name )
408
+ container , err := client .NewContainerClient (containerName )
409
+ if err != nil {
410
+ return err
411
+ }
412
+
413
+ blob , err := container .NewAppendBlobClient (name )
414
+ if err != nil {
415
+ return err
416
+ }
345
417
346
418
ctx , timeout := context .WithTimeout (context .Background (), testTimeout )
347
419
defer timeout ()
@@ -350,7 +422,7 @@ func createBlob(ctx context.Context, client *BlobClient, containerName, name, da
350
422
}
351
423
352
424
hash := md5 .Sum ([]byte (data ))
353
- if _ , err := blob .AppendBlock (ctx , streaming .NopCloser (strings .NewReader (data )), & azblob.AppendBlockOptions {
425
+ if _ , err := blob .AppendBlock (ctx , streaming .NopCloser (strings .NewReader (data )), & azblob.AppendBlobAppendBlockOptions {
354
426
TransactionalContentMD5 : hash [:16 ],
355
427
}); err != nil {
356
428
return err
0 commit comments