public static List<String> uploadNewBlobs(CloudBlobContainer container, BlobType type, int count, int length, OperationContext context) throws StorageException, IOException, URISyntaxException { CloudBlob blob = null; List<String> blobs = new ArrayList<String>(); for (int i = 0; i < count; i++) { switch (type) { case BLOCK_BLOB: blob = uploadNewBlob(container, type, "bb", length, context); blobs.add(blob.getName()); break; case PAGE_BLOB: blob = uploadNewBlob(container, type, "pb", length, context); blobs.add(blob.getName()); break; case APPEND_BLOB: blob = uploadNewBlob(container, type, "ab", length, context); blobs.add(blob.getName()); break; default: break; } } return blobs; }
.uploadNewBlobs(this.container, BlobType.BLOCK_BLOB, numBlobs, 128, null); BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "pref/blob1", 128, null); blobNames.add("pref/blob1"); BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "pref/blob2", 128, null); blobNames.add("pref/blob2");
private static void testBlobAccess(CloudBlobContainer container, BlobType type, EnumSet<SharedAccessBlobPermissions> permissions, SharedAccessBlobHeaders headers) throws StorageException, IOException, URISyntaxException, InvalidKeyException { CloudBlob blob = BlobTestHelper.uploadNewBlob(container, type, SR.BLOB, 512, null); SharedAccessBlobPolicy policy = createSharedAccessPolicy(permissions, 3600); String sasToken = blob.generateSharedAccessSignature(policy, headers, null); testAccess(sasToken, permissions, headers, null, blob); }
@Before public void blobSasTestMethodSetUp() throws URISyntaxException, StorageException, IOException { this.container = BlobTestHelper.getRandomContainerReference(); this.container.create(); this.blob = (CloudBlockBlob) BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "test", 100, null); }
@Test @Category(SlowTests.class) public void testContainerPublicAccess() throws StorageException, IOException, URISyntaxException, InterruptedException { CloudBlockBlob testBlockBlob = (CloudBlockBlob) BlobTestHelper.uploadNewBlob( this.container, BlobType.BLOCK_BLOB, "blockblob", 64, null); CloudPageBlob testPageBlob = (CloudPageBlob) BlobTestHelper.uploadNewBlob( this.container, BlobType.PAGE_BLOB, "pageblob", 512, null); BlobContainerPermissions permissions = new BlobContainerPermissions(); permissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER); this.container.uploadPermissions(permissions); Thread.sleep(35000); testAccess(null, EnumSet.of(SharedAccessBlobPermissions.LIST, SharedAccessBlobPermissions.READ), null, this.container, testBlockBlob); testAccess(null, EnumSet.of(SharedAccessBlobPermissions.LIST, SharedAccessBlobPermissions.READ), null, this.container, testPageBlob); permissions.setPublicAccess(BlobContainerPublicAccessType.BLOB); this.container.uploadPermissions(permissions); Thread.sleep(30000); testAccess(null, EnumSet.of(SharedAccessBlobPermissions.READ), null, this.container, testBlockBlob); testAccess(null, EnumSet.of(SharedAccessBlobPermissions.READ), null, this.container, testPageBlob); }
CloudBlockBlob blob = (CloudBlockBlob) BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "blockblob", 64, null); testAccess(sasToken, EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE), null, BlobTestHelper.uploadNewBlob(sasContainer, BlobType.BLOCK_BLOB, "blockblob", 64, null); Assert.fail();
@Test public void testAppendBlobNamePlusEncodingTest() throws StorageException, URISyntaxException, IOException, InterruptedException { final int length = 1 * 1024; final CloudAppendBlob originalBlob = (CloudAppendBlob) BlobTestHelper.uploadNewBlob( this.container, BlobType.APPEND_BLOB, "a+b.txt", length, null); final CloudAppendBlob copyBlob = this.container.getAppendBlobReference(originalBlob.getName() + "copyed"); copyBlob.startCopy(originalBlob); BlobTestHelper.waitForCopy(copyBlob); copyBlob.downloadAttributes(); }
CloudBlob blob = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "bb", length, sendingRequestEventContext);
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testCloudBlobContainerReferenceFromServerSAS() throws StorageException, URISyntaxException, IOException, InvalidKeyException { this.container.create(); CloudBlob blob = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, null, 1024, null); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy(); Calendar now = Calendar.getInstance(); now.add(Calendar.MINUTE, 10); policy.setSharedAccessExpiryTime(now.getTime()); policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ)); String token = this.container.generateSharedAccessSignature(policy, null); CloudBlobContainer containerSAS = new CloudBlobContainer(this.container.getStorageUri(), new StorageCredentialsSharedAccessSignature(token)); CloudBlob blobRef = containerSAS.getBlobReferenceFromServer(blob.getName()); assertEquals(blob.getClass(), blobRef.getClass()); assertEquals(blob.getUri(), blobRef.getUri()); }
@Test @Category(SlowTests.class) public void testBlobLeaseRenew() throws StorageException, IOException, InterruptedException, URISyntaxException { final CloudBlob blobRef = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "test", 128, null); // Get Lease final String leaseID = blobRef.acquireLease(15, null); Thread.sleep(1000); AccessCondition leaseCondition = AccessCondition.generateLeaseCondition(leaseID); OperationContext operationContext = new OperationContext(); blobRef.renewLease(leaseCondition, null/* BlobRequestOptions */, operationContext); assertTrue(operationContext.getLastResult().getStatusCode() == HttpURLConnection.HTTP_OK); } }
/** * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void testPageBlobNamePlusEncodingTest() throws StorageException, URISyntaxException, IOException, InterruptedException { final int length = 1 * 1024; final CloudPageBlob originalBlob = (CloudPageBlob) BlobTestHelper.uploadNewBlob(this.container, BlobType.PAGE_BLOB, "a+b.txt", length, null); final CloudPageBlob copyBlob = this.container.getPageBlobReference(originalBlob.getName() + "copyed"); copyBlob.startCopy(originalBlob); BlobTestHelper.waitForCopy(copyBlob); copyBlob.downloadAttributes(); }
/** * Start copying a blob and then abort */ @Test public void testCopyFromAppendBlobAbortTest() throws StorageException, URISyntaxException, IOException { final int length = 512; CloudAppendBlob originalBlob = (CloudAppendBlob) BlobTestHelper.uploadNewBlob( this.container, BlobType.APPEND_BLOB, "originalBlob", length, null); CloudAppendBlob copyBlob = this.container.getAppendBlobReference(originalBlob.getName() + "copyed"); copyBlob.startCopy(originalBlob); try { copyBlob.abortCopy(copyBlob.getProperties().getCopyState().getCopyId()); } catch (StorageException e) { if (!e.getErrorCode().contains("NoPendingCopyOperation")) { throw e; } } }
@Test public void testBlobLeaseBreak() throws StorageException, IOException, URISyntaxException { final CloudBlob blobRef = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "test", 128, null); // Get Lease String leaseID = blobRef.acquireLease(); OperationContext operationContext = new OperationContext(); final AccessCondition leaseCondition = AccessCondition.generateLeaseCondition(leaseID); blobRef.breakLease(0, leaseCondition, null/* BlobRequestOptions */, operationContext); assertTrue(operationContext.getLastResult().getStatusCode() == HttpURLConnection.HTTP_ACCEPTED); }
/** * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class, SlowTests.class }) public void testBlobNamePlusEncodingTest() throws StorageException, URISyntaxException, IOException, InterruptedException { final int length = 1 * 1024; final CloudBlockBlob originalBlob = (CloudBlockBlob) BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "a+b.txt", length, null); final CloudBlockBlob copyBlob = this.container.getBlockBlobReference(originalBlob.getName() + "copyed"); copyBlob.startCopy(originalBlob); BlobTestHelper.waitForCopy(copyBlob); copyBlob.downloadAttributes(); }
/** * Start copying a blob and then abort * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void testCopyFromPageBlobAbortTest() throws StorageException, URISyntaxException, IOException { final int length = 512; CloudPageBlob originalBlob = (CloudPageBlob) BlobTestHelper.uploadNewBlob( this.container, BlobType.PAGE_BLOB, "originalBlob", length, null); CloudPageBlob copyBlob = this.container.getPageBlobReference(originalBlob.getName() + "copyed"); copyBlob.startCopy(originalBlob); try { copyBlob.abortCopy(copyBlob.getProperties().getCopyState().getCopyId()); } catch (StorageException e) { if (!e.getErrorCode().contains("NoPendingCopyOperation")) { throw e; } } }
/** * Start copying a blob and then abort * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testCopyFromBlobAbortTest() throws StorageException, URISyntaxException, IOException { final int length = 128; CloudBlockBlob originalBlob = (CloudBlockBlob) BlobTestHelper.uploadNewBlob( this.container, BlobType.BLOCK_BLOB, "originalBlob", length, null); CloudBlockBlob copyBlob = this.container.getBlockBlobReference(originalBlob.getName() + "copyed"); copyBlob.startCopy(originalBlob); try { copyBlob.abortCopy(copyBlob.getProperties().getCopyState().getCopyId()); } catch (StorageException e) { if (!e.getErrorCode().contains("NoPendingCopyOperation")) { throw e; } } }
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testCloudBlobContainerReferenceFromServer() throws StorageException, URISyntaxException, IOException { this.container.create(); CloudBlob blob = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, null, 1024, null); blob.getProperties().setContentType("application/octet-stream"); blob.getProperties().setLength(1024); CloudBlob blobRef = this.container.getBlobReferenceFromServer(blob.getName()); BlobTestHelper.assertAreEqual(blob, blobRef); blob = BlobTestHelper.uploadNewBlob(this.container, BlobType.PAGE_BLOB, null, 1024, null); blob.getProperties().setContentType("application/octet-stream"); blob.getProperties().setLength(1024); blobRef = this.container.getBlobReferenceFromServer(blob.getName()); BlobTestHelper.assertAreEqual(blob, blobRef); blob = BlobTestHelper.uploadNewBlob(this.container, BlobType.APPEND_BLOB, null, 1024, null); blob.getProperties().setContentType("application/octet-stream"); blob.getProperties().setLength(1024); blobRef = this.container.getBlobReferenceFromServer(blob.getName()); BlobTestHelper.assertAreEqual(blob, blobRef); }
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testCloudBlobContainerReferenceFromServerSnapshot() throws StorageException, URISyntaxException, IOException { this.container.create(); CloudBlob blob = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, null, 1024, null); CloudBlob snapshot = blob.createSnapshot(); snapshot.getProperties().setContentType("application/octet-stream"); snapshot.getProperties().setLength(1024); CloudBlob blobRef = this.container.getBlobReferenceFromServer(snapshot.getName(), snapshot.getSnapshotID(), null, null, null); BlobTestHelper.assertAreEqual(snapshot, blobRef); }
@Test public void testRetryOn304() throws StorageException, IOException, URISyntaxException { OperationContext operationContext = new OperationContext(); operationContext.getRetryingEventHandler().addListener(new StorageEvent<RetryingEvent>() { @Override public void eventOccurred(RetryingEvent eventArg) { fail("Request should not be retried."); } }); CloudBlobContainer container = BlobTestHelper.getRandomContainerReference(); try { container.create(); CloudBlockBlob blockBlobRef = (CloudBlockBlob) BlobTestHelper.uploadNewBlob(container, BlobType.BLOCK_BLOB, "originalBlob", 1024, null); AccessCondition accessCondition = AccessCondition.generateIfNoneMatchCondition(blockBlobRef.getProperties().getEtag()); blockBlobRef.download(new ByteArrayOutputStream(), accessCondition, null, operationContext); fail("Download should fail with a 304."); } catch (StorageException ex) { assertEquals("The condition specified using HTTP conditional header(s) is not met.", ex.getMessage()); } finally { container.deleteIfExists(); } }
@Test public void testBlobLeaseAcquireAndRelease() throws URISyntaxException, StorageException, IOException { final int length = 128; final CloudBlob blobRef = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "test", 128, null); // Get Lease OperationContext operationContext = new OperationContext(); final String leaseID = blobRef.acquireLease(15, null /*proposed lease id */, null /*access condition*/, null/* BlobRequestOptions */, operationContext); final AccessCondition leaseCondition = AccessCondition.generateLeaseCondition(leaseID); assertTrue(operationContext.getLastResult().getStatusCode() == HttpURLConnection.HTTP_CREATED); tryUploadWithBadLease(length, blobRef, null, StorageErrorCodeStrings.LEASE_ID_MISSING); // Try to upload with lease blobRef.upload(BlobTestHelper.getRandomDataStream(length), -1, leaseCondition, null, null); // Release lease blobRef.releaseLease(leaseCondition); // now upload with no lease specified. blobRef.upload(BlobTestHelper.getRandomDataStream(length), -1); }