/** * Uploads the source stream data to the append blob. If the blob already exists on the service, it will be * overwritten. * <p> * If you want to append data to an already existing blob, please see {@link #appendBlock(InputStream, long)}. * <p> * If you are doing writes in a single writer scenario, please look at * {@link BlobRequestOptions#setAbsorbConditionalErrorsOnRetry(Boolean)} and see if setting this flag * to <code>true</code> is acceptable for you. * * @param sourceStream * An {@link InputStream} object to read from. * @param length * A <code>long</code> which represents the length, in bytes, of the stream data, or -1 if unknown. * * @throws IOException * If an I/O exception occurred. * @throws StorageException * If a storage service error occurred. */ @Override @DoesServiceRequest public void upload(final InputStream sourceStream, final long length) throws StorageException, IOException { this.upload(sourceStream, length, null /* accessCondition */, null /* options */, null /* opContext */); }
@Test public void testBlobUploadWithoutMD5Validation() throws URISyntaxException, StorageException, IOException { final String appendBlobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testAppendBlob"); final CloudAppendBlob appendBlobRef = this.container .getAppendBlobReference(appendBlobName); final int length = 2 * 1024; ByteArrayInputStream srcStream = BlobTestHelper .getRandomDataStream(length); BlobRequestOptions options = new BlobRequestOptions(); options.setDisableContentMD5Validation(false); options.setStoreBlobContentMD5(false); appendBlobRef.upload(srcStream, length, null, options, null); appendBlobRef.downloadAttributes(); appendBlobRef.getProperties().setContentMD5("MDAwMDAwMDA="); appendBlobRef.uploadProperties(null, options, null); try { appendBlobRef.download(new ByteArrayOutputStream(), null, options, null); fail(); } catch (StorageException ex) { assertEquals(306, ex.getHttpStatusCode()); assertEquals("InvalidMd5", ex.getErrorCode()); } options.setDisableContentMD5Validation(true); appendBlobRef .download(new ByteArrayOutputStream(), null, options, null); }
/** * Uploads the source stream data to the append blob. If the blob already exists on the service, it will be * overwritten. * <p> * If you want to append data to an already existing blob, please see {@link #appendBlock(InputStream, long)}. * <p> * If you are doing writes in a single writer scenario, please look at * {@link BlobRequestOptions#setAbsorbConditionalErrorsOnRetry(Boolean)} and see if setting this flag * to <code>true</code> is acceptable for you. * * @param sourceStream * An {@link InputStream} object to read from. * @param length * A <code>long</code> which represents the length, in bytes, of the stream data, or -1 if unknown. * * @throws IOException * If an I/O exception occurred. * @throws StorageException * If a storage service error occurred. */ @Override @DoesServiceRequest public void upload(final InputStream sourceStream, final long length) throws StorageException, IOException { this.upload(sourceStream, length, null /* accessCondition */, null /* options */, null /* opContext */); }
ByteArrayInputStream stream = new ByteArrayInputStream(buffer); source.upload(stream, buffer.length);
@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())); }
source.upload(stream, buffer.length); source.getMetadata().put("Test", "value"); source.uploadMetadata();
@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()); }
byte[] buffer = BlobTestHelper.getRandomBuffer(512); ByteArrayInputStream stream = new ByteArrayInputStream(buffer); source.upload(stream, buffer.length); source.getMetadata().put("Test", "value"); source.uploadMetadata();
@Test public void testAppendBlobDownloadRangeTest() throws URISyntaxException, StorageException, IOException { byte[] buffer = BlobTestHelper.getRandomBuffer(2 * 1024); CloudAppendBlob blob = this.container.getAppendBlobReference("blob1"); ByteArrayInputStream wholeBlob = new ByteArrayInputStream(buffer); BlobRequestOptions opt = new BlobRequestOptions(); opt.setStoreBlobContentMD5(false); blob.upload(wholeBlob, 2 * 1024, null, opt, null); ByteArrayOutputStream blobStream = new ByteArrayOutputStream(); try { blob.downloadRange(0, new Long(0), blobStream); } catch (IndexOutOfBoundsException ex) { } blob.downloadRange(0, new Long(1024), blobStream); assertEquals(blobStream.size(), 1024); BlobTestHelper.assertStreamsAreEqualAtIndex(new ByteArrayInputStream( blobStream.toByteArray()), wholeBlob, 0, 0, 1024, 2 * 1024); CloudAppendBlob blob2 = this.container.getAppendBlobReference("blob1"); try { blob.downloadRange(1024, new Long(0), blobStream); } catch (IndexOutOfBoundsException ex) { } ByteArrayOutputStream blobStream2 = new ByteArrayOutputStream(); blob2.downloadRange(1024, new Long(1024), blobStream2); BlobTestHelper.assertStreamsAreEqualAtIndex(new ByteArrayInputStream( blobStream2.toByteArray()), wholeBlob, 1024, 1024, 1024, 2 * 1024); BlobTestHelper.assertAreEqual(blob, blob2); }
@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 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); }
@Test public void testBlobEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException { final String appendBlobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testAppendBlob"); final CloudAppendBlob appendBlobRef = this.container .getAppendBlobReference(appendBlobName); final int length = 2 * 1024; ByteArrayInputStream srcStream = BlobTestHelper .getRandomDataStream(length); OperationContext context = new OperationContext(); context.getSendingRequestEventHandler().addListener( new StorageEvent<SendingRequestEvent>() { @Override public void eventOccurred(SendingRequestEvent eventArg) { HttpURLConnection connection = (HttpURLConnection) eventArg .getConnectionObject(); connection.setRequestProperty("x-ms-foo", ""); } }); appendBlobRef.upload(srcStream, length, null, null, context); appendBlobRef .download(new ByteArrayOutputStream(), null, null, context); }
ByteArrayInputStream srcStream = BlobTestHelper .getRandomDataStream(length); blob1.upload(srcStream, length, accessCondition, null, null); .getProperties().getEtag()); try { blob1.upload(srcStream, length, accessCondition, null, null); } catch (StorageException ex) { assertEquals(HttpURLConnection.HTTP_PRECON_FAILED, accessCondition = AccessCondition.generateIfMatchCondition(blob1 .getProperties().getEtag()); blob1.upload(srcStream, length, accessCondition, null, null); .getProperties().getEtag()); try { blob1.upload(srcStream, length, accessCondition, null, null); } catch (StorageException ex) { assertEquals(HttpURLConnection.HTTP_PRECON_FAILED, accessCondition = AccessCondition.generateIfNoneMatchCondition(blob2 .getProperties().getEtag()); blob1.upload(srcStream, length, accessCondition, null, null);
@Test public void testAppendBlobInputStream() throws URISyntaxException, StorageException, IOException { final int blobLength = 16 * 1024; final Random randGenerator = new Random(); String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); final CloudAppendBlob blobRef = this.container .getAppendBlobReference(blobName); final byte[] buff = new byte[blobLength]; randGenerator.nextBytes(buff); buff[0] = -1; buff[1] = -128; final ByteArrayInputStream sourceStream = new ByteArrayInputStream(buff); final BlobRequestOptions options = new BlobRequestOptions(); final OperationContext operationContext = new OperationContext(); options.setTimeoutIntervalInMs(90000); options.setRetryPolicyFactory(new RetryNoRetry()); blobRef.upload(sourceStream, blobLength, null, options, operationContext); BlobInputStream blobStream = blobRef.openInputStream(); for (int i = 0; i < blobLength; i++) { int data = blobStream.read(); assertTrue(data >= 0); assertEquals(buff[i], (byte) data); } assertEquals(-1, blobStream.read()); blobRef.delete(); }
/** * 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); }