|
15 | 15 |
|
16 | 16 | package com.microsoft.azure.storage.blob; |
17 | 17 |
|
18 | | -import com.microsoft.azure.storage.Constants; |
19 | | -import com.microsoft.azure.storage.core.PathUtility; |
20 | | -import com.microsoft.azure.storage.core.SR; |
21 | | -import com.microsoft.azure.storage.IPRange; |
22 | | -import com.microsoft.azure.storage.OperationContext; |
23 | | -import com.microsoft.azure.storage.ResponseReceivedEvent; |
24 | | -import com.microsoft.azure.storage.SecondaryTests; |
25 | | -import com.microsoft.azure.storage.SendingRequestEvent; |
26 | | -import com.microsoft.azure.storage.SharedAccessProtocols; |
27 | | -import com.microsoft.azure.storage.StorageCredentials; |
28 | | -import com.microsoft.azure.storage.StorageCredentialsAnonymous; |
29 | | -import com.microsoft.azure.storage.StorageCredentialsSharedAccessSignature; |
30 | | -import com.microsoft.azure.storage.StorageEvent; |
31 | | -import com.microsoft.azure.storage.StorageException; |
32 | | -import com.microsoft.azure.storage.TestRunners; |
33 | | - |
34 | 18 | import junit.framework.Assert; |
35 | 19 |
|
36 | | -import org.junit.After; |
37 | | -import org.junit.Before; |
38 | | -import org.junit.Test; |
39 | | -import org.junit.experimental.categories.Category; |
40 | | - |
| 20 | +import java.io.ByteArrayInputStream; |
41 | 21 | import java.io.ByteArrayOutputStream; |
42 | 22 | import java.io.IOException; |
43 | 23 | import java.net.HttpURLConnection; |
|
54 | 34 | import java.util.NoSuchElementException; |
55 | 35 | import java.util.TimeZone; |
56 | 36 |
|
| 37 | +import org.junit.After; |
| 38 | +import org.junit.Before; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.experimental.categories.Category; |
| 41 | + |
| 42 | +import com.microsoft.azure.storage.Constants; |
| 43 | +import com.microsoft.azure.storage.core.PathUtility; |
| 44 | +import com.microsoft.azure.storage.core.SR; |
| 45 | +import com.microsoft.azure.storage.IPRange; |
| 46 | +import com.microsoft.azure.storage.OperationContext; |
| 47 | +import com.microsoft.azure.storage.ResponseReceivedEvent; |
| 48 | +import com.microsoft.azure.storage.SecondaryTests; |
| 49 | +import com.microsoft.azure.storage.SendingRequestEvent; |
| 50 | +import com.microsoft.azure.storage.SharedAccessProtocols; |
| 51 | +import com.microsoft.azure.storage.SharedAccessAccountPermissions; |
| 52 | +import com.microsoft.azure.storage.SharedAccessAccountPolicy; |
| 53 | +import com.microsoft.azure.storage.SharedAccessAccountResourceType; |
| 54 | +import com.microsoft.azure.storage.SharedAccessAccountService; |
| 55 | +import com.microsoft.azure.storage.SharedAccessProtocols; |
| 56 | +import com.microsoft.azure.storage.StorageCredentials; |
| 57 | +import com.microsoft.azure.storage.StorageCredentialsAnonymous; |
| 58 | +import com.microsoft.azure.storage.StorageCredentialsSharedAccessSignature; |
| 59 | +import com.microsoft.azure.storage.StorageEvent; |
| 60 | +import com.microsoft.azure.storage.StorageException; |
57 | 61 | import com.microsoft.azure.storage.TestHelper; |
| 62 | +import com.microsoft.azure.storage.TestRunners; |
58 | 63 | import com.microsoft.azure.storage.TestRunners.CloudTests; |
59 | 64 | import com.microsoft.azure.storage.TestRunners.DevFabricTests; |
60 | 65 | import com.microsoft.azure.storage.TestRunners.DevStoreTests; |
@@ -487,6 +492,179 @@ public void eventOccurred(SendingRequestEvent eventArg) { |
487 | 492 | sasBlob.download(new ByteArrayOutputStream(), null, null, context); |
488 | 493 | } |
489 | 494 |
|
| 495 | + @Test |
| 496 | + public void testAppendBlobCopyWithSasAndSnapshot() |
| 497 | + throws URISyntaxException, StorageException, InterruptedException, IOException, InvalidKeyException { |
| 498 | + String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); |
| 499 | + CloudAppendBlob source = this.container.getAppendBlobReference(blobName); |
| 500 | + source.createOrReplace(); |
| 501 | + byte[] buffer = BlobTestHelper.getRandomBuffer(512); |
| 502 | + ByteArrayInputStream stream = new ByteArrayInputStream(buffer); |
| 503 | + source.upload(stream, buffer.length); |
| 504 | + source.getMetadata().put("Test", "value"); |
| 505 | + source.uploadMetadata(); |
| 506 | + |
| 507 | + SharedAccessBlobPolicy policy = createSharedAccessPolicy( |
| 508 | + EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE, |
| 509 | + SharedAccessBlobPermissions.LIST, SharedAccessBlobPermissions.DELETE), 5000); |
| 510 | + |
| 511 | + CloudAppendBlob copy = this.container.getAppendBlobReference("copy"); |
| 512 | + String sasToken = copy.generateSharedAccessSignature(policy, null); |
| 513 | + CloudAppendBlob copySas = new CloudAppendBlob(new URI(copy.getUri().toString() + "?" + sasToken)); |
| 514 | + |
| 515 | + // Generate account SAS for the source |
| 516 | + // Cannot generate a SAS directly on a snapshot and the SAS for the destination is only for the destination |
| 517 | + SharedAccessAccountPolicy accountPolicy = new SharedAccessAccountPolicy(); |
| 518 | + accountPolicy.setPermissions(EnumSet.of(SharedAccessAccountPermissions.READ, SharedAccessAccountPermissions.WRITE)); |
| 519 | + accountPolicy.setServices(EnumSet.of(SharedAccessAccountService.BLOB)); |
| 520 | + accountPolicy.setResourceTypes(EnumSet.of(SharedAccessAccountResourceType.OBJECT, SharedAccessAccountResourceType.CONTAINER)); |
| 521 | + accountPolicy.setSharedAccessExpiryTime(policy.getSharedAccessExpiryTime()); |
| 522 | + final CloudBlobClient sasClient = TestHelper.createCloudBlobClient(accountPolicy, false); |
| 523 | + |
| 524 | + CloudAppendBlob snapshot = (CloudAppendBlob) source.createSnapshot(); |
| 525 | + CloudAppendBlob sasBlob = (CloudAppendBlob) sasClient.getContainerReference(container.getName()) |
| 526 | + .getBlobReferenceFromServer(snapshot.getName(), snapshot.snapshotID, null, null, null); |
| 527 | + sasBlob.exists(); |
| 528 | + |
| 529 | + String copyId = copySas.startCopy(BlobTestHelper.defiddler(sasBlob)); |
| 530 | + BlobTestHelper.waitForCopy(copySas); |
| 531 | + |
| 532 | + copySas.downloadAttributes(); |
| 533 | + BlobProperties prop1 = copySas.getProperties(); |
| 534 | + BlobProperties prop2 = sasBlob.getProperties(); |
| 535 | + |
| 536 | + assertEquals(prop1.getCacheControl(), prop2.getCacheControl()); |
| 537 | + assertEquals(prop1.getContentEncoding(), prop2.getContentEncoding()); |
| 538 | + assertEquals(prop1.getContentDisposition(), |
| 539 | + prop2.getContentDisposition()); |
| 540 | + assertEquals(prop1.getContentLanguage(), prop2.getContentLanguage()); |
| 541 | + assertEquals(prop1.getContentMD5(), prop2.getContentMD5()); |
| 542 | + assertEquals(prop1.getContentType(), prop2.getContentType()); |
| 543 | + |
| 544 | + assertEquals("value", copySas.getMetadata().get("Test")); |
| 545 | + assertEquals(copyId, copySas.getCopyState().getCopyId()); |
| 546 | + |
| 547 | + snapshot.delete(); |
| 548 | + source.delete(); |
| 549 | + copySas.delete(); |
| 550 | + } |
| 551 | + |
| 552 | + @Test |
| 553 | + public void testBlockBlobCopyWithSasAndSnapshot() |
| 554 | + throws URISyntaxException, StorageException, InterruptedException, IOException, InvalidKeyException { |
| 555 | + String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); |
| 556 | + CloudBlockBlob source = this.container.getBlockBlobReference(blobName); |
| 557 | + String data = "String data"; |
| 558 | + source.uploadText(data, Constants.UTF8_CHARSET, null, null, null); |
| 559 | + |
| 560 | + byte[] buffer = BlobTestHelper.getRandomBuffer(512); |
| 561 | + ByteArrayInputStream stream = new ByteArrayInputStream(buffer); |
| 562 | + source.upload(stream, buffer.length); |
| 563 | + source.getMetadata().put("Test", "value"); |
| 564 | + source.uploadMetadata(); |
| 565 | + |
| 566 | + SharedAccessBlobPolicy policy = createSharedAccessPolicy( |
| 567 | + EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE, |
| 568 | + SharedAccessBlobPermissions.LIST, SharedAccessBlobPermissions.DELETE), 5000); |
| 569 | + |
| 570 | + CloudBlockBlob copy = this.container.getBlockBlobReference("copy"); |
| 571 | + String sasToken = copy.generateSharedAccessSignature(policy, null); |
| 572 | + CloudBlockBlob copySas = new CloudBlockBlob(new URI(copy.getUri().toString() + "?" + sasToken)); |
| 573 | + |
| 574 | + // Generate account SAS for the source |
| 575 | + // Cannot generate a SAS directly on a snapshot and the SAS for the destination is only for the destination |
| 576 | + SharedAccessAccountPolicy accountPolicy = new SharedAccessAccountPolicy(); |
| 577 | + accountPolicy.setPermissions(EnumSet.of(SharedAccessAccountPermissions.READ, SharedAccessAccountPermissions.WRITE)); |
| 578 | + accountPolicy.setServices(EnumSet.of(SharedAccessAccountService.BLOB)); |
| 579 | + accountPolicy.setResourceTypes(EnumSet.of(SharedAccessAccountResourceType.OBJECT, SharedAccessAccountResourceType.CONTAINER)); |
| 580 | + accountPolicy.setSharedAccessExpiryTime(policy.getSharedAccessExpiryTime()); |
| 581 | + final CloudBlobClient sasClient = TestHelper.createCloudBlobClient(accountPolicy, false); |
| 582 | + |
| 583 | + CloudBlockBlob snapshot = (CloudBlockBlob) source.createSnapshot(); |
| 584 | + CloudBlockBlob sasBlob = (CloudBlockBlob) sasClient.getContainerReference(container.getName()) |
| 585 | + .getBlobReferenceFromServer(snapshot.getName(), snapshot.snapshotID, null, null, null); |
| 586 | + sasBlob.exists(); |
| 587 | + |
| 588 | + String copyId = copySas.startCopy(BlobTestHelper.defiddler(sasBlob)); |
| 589 | + BlobTestHelper.waitForCopy(copySas); |
| 590 | + |
| 591 | + copySas.downloadAttributes(); |
| 592 | + BlobProperties prop1 = copySas.getProperties(); |
| 593 | + BlobProperties prop2 = sasBlob.getProperties(); |
| 594 | + |
| 595 | + assertEquals(prop1.getCacheControl(), prop2.getCacheControl()); |
| 596 | + assertEquals(prop1.getContentEncoding(), prop2.getContentEncoding()); |
| 597 | + assertEquals(prop1.getContentDisposition(), |
| 598 | + prop2.getContentDisposition()); |
| 599 | + assertEquals(prop1.getContentLanguage(), prop2.getContentLanguage()); |
| 600 | + assertEquals(prop1.getContentMD5(), prop2.getContentMD5()); |
| 601 | + assertEquals(prop1.getContentType(), prop2.getContentType()); |
| 602 | + |
| 603 | + assertEquals("value", copySas.getMetadata().get("Test")); |
| 604 | + assertEquals(copyId, copySas.getCopyState().getCopyId()); |
| 605 | + |
| 606 | + snapshot.delete(); |
| 607 | + source.delete(); |
| 608 | + copySas.delete(); |
| 609 | + } |
| 610 | + |
| 611 | + @Test |
| 612 | + public void testPageBlobCopyWithSasAndSnapshot() |
| 613 | + throws URISyntaxException, StorageException, InterruptedException, IOException, InvalidKeyException { |
| 614 | + String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); |
| 615 | + CloudPageBlob source = this.container.getPageBlobReference(blobName); |
| 616 | + source.create(1024); |
| 617 | + byte[] buffer = BlobTestHelper.getRandomBuffer(512); |
| 618 | + ByteArrayInputStream stream = new ByteArrayInputStream(buffer); |
| 619 | + source.upload(stream, buffer.length); |
| 620 | + source.getMetadata().put("Test", "value"); |
| 621 | + source.uploadMetadata(); |
| 622 | + |
| 623 | + SharedAccessBlobPolicy policy = createSharedAccessPolicy( |
| 624 | + EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE, |
| 625 | + SharedAccessBlobPermissions.LIST, SharedAccessBlobPermissions.DELETE), 5000); |
| 626 | + |
| 627 | + CloudPageBlob copy = this.container.getPageBlobReference("copy"); |
| 628 | + String sasToken = copy.generateSharedAccessSignature(policy, null); |
| 629 | + CloudPageBlob copySas = new CloudPageBlob(new URI(copy.getUri().toString() + "?" + sasToken)); |
| 630 | + |
| 631 | + // Generate account SAS for the source |
| 632 | + // Cannot generate a SAS directly on a snapshot and the SAS for the destination is only for the destination |
| 633 | + SharedAccessAccountPolicy accountPolicy = new SharedAccessAccountPolicy(); |
| 634 | + accountPolicy.setPermissions(EnumSet.of(SharedAccessAccountPermissions.READ, SharedAccessAccountPermissions.WRITE)); |
| 635 | + accountPolicy.setServices(EnumSet.of(SharedAccessAccountService.BLOB)); |
| 636 | + accountPolicy.setResourceTypes(EnumSet.of(SharedAccessAccountResourceType.OBJECT, SharedAccessAccountResourceType.CONTAINER)); |
| 637 | + accountPolicy.setSharedAccessExpiryTime(policy.getSharedAccessExpiryTime()); |
| 638 | + final CloudBlobClient sasClient = TestHelper.createCloudBlobClient(accountPolicy, false); |
| 639 | + |
| 640 | + CloudPageBlob snapshot = (CloudPageBlob) source.createSnapshot(); |
| 641 | + CloudPageBlob sasBlob = (CloudPageBlob) sasClient.getContainerReference(container.getName()) |
| 642 | + .getBlobReferenceFromServer(snapshot.getName(), snapshot.snapshotID, null, null, null); |
| 643 | + sasBlob.exists(); |
| 644 | + |
| 645 | + String copyId = copySas.startCopy(BlobTestHelper.defiddler(sasBlob)); |
| 646 | + BlobTestHelper.waitForCopy(copySas); |
| 647 | + |
| 648 | + copySas.downloadAttributes(); |
| 649 | + BlobProperties prop1 = copySas.getProperties(); |
| 650 | + BlobProperties prop2 = sasBlob.getProperties(); |
| 651 | + |
| 652 | + assertEquals(prop1.getCacheControl(), prop2.getCacheControl()); |
| 653 | + assertEquals(prop1.getContentEncoding(), prop2.getContentEncoding()); |
| 654 | + assertEquals(prop1.getContentDisposition(), |
| 655 | + prop2.getContentDisposition()); |
| 656 | + assertEquals(prop1.getContentLanguage(), prop2.getContentLanguage()); |
| 657 | + assertEquals(prop1.getContentMD5(), prop2.getContentMD5()); |
| 658 | + assertEquals(prop1.getContentType(), prop2.getContentType()); |
| 659 | + |
| 660 | + assertEquals("value", copySas.getMetadata().get("Test")); |
| 661 | + assertEquals(copyId, copySas.getCopyState().getCopyId()); |
| 662 | + |
| 663 | + snapshot.delete(); |
| 664 | + source.delete(); |
| 665 | + copySas.delete(); |
| 666 | + } |
| 667 | + |
490 | 668 | private final static SharedAccessBlobPolicy createSharedAccessPolicy(EnumSet<SharedAccessBlobPermissions> sap, |
491 | 669 | int expireTimeInSeconds) { |
492 | 670 |
|
|
0 commit comments