/** * Returns a reference to a {@link CloudAppendBlob} object that represents an append blob in this container. * * @param blobName * A <code>String</code> that represents the name of the blob. * * @return A {@link CloudAppendBlob} object that represents a reference to the specified append blob. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ public CloudAppendBlob getAppendBlobReference(final String blobName) throws URISyntaxException, StorageException { return this.getAppendBlobReference(blobName, null); }
/** * Returns a reference to a {@link CloudAppendBlob} object that represents an append blob in this container. * * @param blobName * A <code>String</code> that represents the name of the blob. * * @return A {@link CloudAppendBlob} object that represents a reference to the specified append blob. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ public CloudAppendBlob getAppendBlobReference(final String blobName) throws URISyntaxException, StorageException { return this.getAppendBlobReference(blobName, null); }
private CloudAppendBlob getBlob() throws URISyntaxException, StorageException { return container.getAppendBlobReference("test-blob"); }
private void roll() { try { date = new Date(); String stamp = new SimpleDateFormat("yyyy-MM-dd").format(date); String blobName = baseBlobName + "_" + stamp + ".log"; blob = container.getAppendBlobReference(blobName); if (!blob.exists()) { blob.createOrReplace(); HashMap<String, String> metadata = new HashMap<>(); metadata.put("date", stamp); metadata.put("name", baseBlobName); blob.setMetadata(metadata); blob.uploadMetadata(); } } catch (StorageException ex) { throw new RuntimeException(ex); } catch (URISyntaxException ex) { throw new RuntimeException(ex); } }
@Test public void testCloudAppendBlobDownloadRangeToByteArrayNegativeTest() throws URISyntaxException, StorageException, IOException { CloudAppendBlob blob = this.container .getAppendBlobReference(BlobTestHelper .generateRandomBlobNameWithPrefix("downloadrangenegative")); BlobTestHelper.doDownloadRangeToByteArrayNegativeTests(blob); }
public static CloudBlob getBlobReference(BlobType type, CloudBlobContainer container, String name) throws URISyntaxException, StorageException { CloudBlob blob = null; switch (type) { case APPEND_BLOB: blob = container.getAppendBlobReference(name); break; case BLOCK_BLOB: blob = container.getBlockBlobReference(name); break; case PAGE_BLOB: blob = container.getPageBlobReference(name); break; case UNSPECIFIED: fail(); } return blob; }
protected JournalReader createJournalReader(String s) throws IOException { try { CloudAppendBlob blob = container.getAppendBlobReference("journal/journal.log.001"); blob.createOrReplace(); blob.appendText(s); return new JournalReader(new AzureJournalFile(container.getDirectoryReference("journal"), "journal.log")); } catch (StorageException | URISyntaxException e) { throw new IOException(e); } } }
@Test public void testAppendBlobUploadFromStreamTest() throws URISyntaxException, StorageException, IOException { final String appendBlobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testAppendBlob"); final CloudAppendBlob appendBlobRef = this.container .getAppendBlobReference(appendBlobName); int length = 2 * 1024; ByteArrayInputStream srcStream = BlobTestHelper .getRandomDataStream(length); appendBlobRef.upload(srcStream, length); ByteArrayOutputStream dstStream = new ByteArrayOutputStream(); appendBlobRef.download(dstStream); BlobTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); length = 5 * 1024 * 1024; srcStream = BlobTestHelper.getRandomDataStream(length); appendBlobRef.upload(srcStream, length); dstStream = new ByteArrayOutputStream(); appendBlobRef.download(dstStream); BlobTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); }
public void testCloudAppendBlobDownloadToByteArray() throws URISyntaxException, StorageException, IOException { CloudAppendBlob blob = this.container.getAppendBlobReference("blob1"); BlobTestHelper.doDownloadTest(blob, 1 * 512, 2 * 512, 0); BlobTestHelper.doDownloadTest(blob, 1 * 512, 2 * 512, 1 * 512); BlobTestHelper.doDownloadTest(blob, 2 * 512, 4 * 512, 1 * 512); BlobTestHelper .doDownloadTest(blob, 5 * 1024 * 1024, 5 * 1024 * 1024, 0); BlobTestHelper.doDownloadTest(blob, 5 * 1024 * 1024, 6 * 1024 * 1024, 512); }
@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(); }
@Test public void testAppendBlobMaxSizeCondition() throws URISyntaxException, StorageException, IOException { final int blobLength = 16 * 1024; String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); final CloudAppendBlob blobRef = this.container .getAppendBlobReference(blobName); final byte[] buff = BlobTestHelper.getRandomBuffer(blobLength); ByteArrayInputStream sourceStream = new ByteArrayInputStream(buff); // Max length position failure AccessCondition cond = new AccessCondition(); cond.setIfMaxSizeLessThanOrEqual(blobLength - 1L); try { blobRef.upload(sourceStream, blobLength, cond, null, null); fail("Expected IOException for exceeding the max size"); } catch (IOException ex) { assertEquals(SR.INVALID_BLOCK_SIZE, ex.getMessage()); } }
@Test public void testUploadFromByteArray() throws Exception { String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); final CloudAppendBlob blob = this.container .getAppendBlobReference(blobName); this.doUploadFromByteArrayTest(blob, 4 * 512, 0, 4 * 512); this.doUploadFromByteArrayTest(blob, 4 * 512, 0, 2 * 512); this.doUploadFromByteArrayTest(blob, 4 * 512, 1 * 512, 2 * 512); this.doUploadFromByteArrayTest(blob, 4 * 512, 2 * 512, 2 * 512); }
@Test public void testUploadDownloadFromFile() throws IOException, StorageException, URISyntaxException { String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); final CloudAppendBlob blob = this.container .getAppendBlobReference(blobName); this.doUploadDownloadFileTest(blob, 512); this.doUploadDownloadFileTest(blob, 4096); this.doUploadDownloadFileTest(blob, 5 * 1024 * 1024); this.doUploadDownloadFileTest(blob, 11 * 1024 * 1024); }
@Test public void testAppendBlobUploadNegativeLength() throws URISyntaxException, StorageException, IOException { final int blobLength = 16 * 1024; String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); final CloudAppendBlob blobRef = this.container .getAppendBlobReference(blobName); final byte[] buff = BlobTestHelper.getRandomBuffer(blobLength); ByteArrayInputStream sourceStream = new ByteArrayInputStream(buff); blobRef.upload(sourceStream, -1); assertTrue(blobRef.exists()); assertEquals(blobRef.getProperties().getLength(), blobLength); }
/** * 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 testAppendBlobUploadFromStream() throws StorageException, IOException, URISyntaxException { byte[] buffer = BlobTestHelper.getRandomBuffer(6 * 1024 * 1024); CloudAppendBlob blob = this.container.getAppendBlobReference("blob1"); blob.upload(new ByteArrayInputStream(buffer), buffer.length); blob.downloadAttributes(); assertEquals(6 * 1024 * 1024, blob.getProperties().getLength()); blob.upload(new ByteArrayInputStream(buffer), buffer.length); blob.downloadAttributes(); assertEquals(6 * 1024 * 1024, blob.getProperties().getLength()); blob.upload(new ByteArrayInputStream(buffer), buffer.length, null /* accessCondition */, null /* options */, null /* operationContext */); blob.downloadAttributes(); assertEquals(6 * 1024 * 1024, blob.getProperties().getLength()); }
@Test public void testAppendBlobAppendFromStream() throws StorageException, IOException, URISyntaxException { // Every time append a buffer that is bigger than a single block. byte[] buffer = BlobTestHelper.getRandomBuffer(6 * 1024 * 1024); CloudAppendBlob blob = this.container.getAppendBlobReference("blob1"); blob.createOrReplace(); blob.append(new ByteArrayInputStream(buffer), buffer.length); blob.downloadAttributes(); assertEquals(6 * 1024 * 1024, blob.getProperties().getLength()); blob.append(new ByteArrayInputStream(buffer), buffer.length); blob.downloadAttributes(); assertEquals(12 * 1024 * 1024, blob.getProperties().getLength()); blob.append(new ByteArrayInputStream(buffer), buffer.length, null /* accessCondition */, null /* options */, null /* operationContext */); blob.downloadAttributes(); assertEquals(18 * 1024 * 1024, blob.getProperties().getLength()); }
@Test public void testAppendBlobAppendFromStreamWithLength() throws StorageException, IOException, URISyntaxException { // Every time append a buffer that is bigger than a single block. byte[] buffer = BlobTestHelper.getRandomBuffer(6 * 1024 * 1024); CloudAppendBlob blob = this.container.getAppendBlobReference("blob1"); blob.createOrReplace(); blob.append(new ByteArrayInputStream(buffer), 5 * 1024 * 1024); blob.downloadAttributes(); assertEquals(5 * 1024 * 1024, blob.getProperties().getLength()); blob.append(new ByteArrayInputStream(buffer), 5 * 1024 * 1024); blob.downloadAttributes(); assertEquals(10 * 1024 * 1024, blob.getProperties().getLength()); blob.append(new ByteArrayInputStream(buffer), 5 * 1024 * 1024, null /* accessCondition */, null /* options */, null /* operationContext */); blob.downloadAttributes(); assertEquals(15 * 1024 * 1024, blob.getProperties().getLength()); } }
@Test public void testOpenOutputStreamNoArgs() throws URISyntaxException, StorageException { String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); CloudAppendBlob appendBlob = this.container .getAppendBlobReference(blobName); try { appendBlob.openWriteExisting(); } catch (StorageException ex) { assertEquals("The specified blob does not exist.", ex.getMessage()); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode()); } appendBlob.openWriteNew(); appendBlob.openWriteExisting(); CloudAppendBlob appendBlob2 = this.container .getAppendBlobReference(blobName); appendBlob2.downloadAttributes(); assertEquals(0, appendBlob2.getProperties().getLength()); assertEquals(BlobType.APPEND_BLOB, appendBlob2.getProperties().getBlobType()); }
/** * Create a blob and try to download a range of its contents */ @Test public void testAppendBlobDownloadRangeValidationTest() throws StorageException, URISyntaxException, IOException { final int length = 5 * 1024 * 1024; final String appendBlobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testBlockBlob"); final CloudAppendBlob appendBlobRef = this.container .getAppendBlobReference(appendBlobName); appendBlobRef .upload(BlobTestHelper.getRandomDataStream(length), length); // Download full blob appendBlobRef.download(new ByteArrayOutputStream()); assertEquals(length, appendBlobRef.getProperties().getLength()); // Download blob range. byte[] downloadBuffer = new byte[100]; int downloadLength = appendBlobRef.downloadRangeToByteArray(0, (long) 100, downloadBuffer, 0); assertEquals(length, appendBlobRef.getProperties().getLength()); assertEquals(100, downloadLength); }