/** * Commits a new block of data to the end of the blob. * * @param sourceStream * An {@link InputStream} object that represents the input stream to write to the append blob. * @param length * A <code>long</code> which represents the length, in bytes, of the stream data, or -1 if unknown. * * @return The offset at which the block was appended. * @throws IOException * If an I/O exception occurred. * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public Long appendBlock(final InputStream sourceStream, final long length) throws IOException, StorageException { return this.appendBlock(sourceStream, length, null, null, null); }
/** * Commits a new block of data to the end of the blob. * * @param sourceStream * An {@link InputStream} object that represents the input stream to write to the append blob. * @param length * A <code>long</code> which represents the length, in bytes, of the stream data, or -1 if unknown. * * @return The offset at which the block was appended. * @throws IOException * If an I/O exception occurred. * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public Long appendBlock(final InputStream sourceStream, final long length) throws IOException, StorageException { return this.appendBlock(sourceStream, length, null, null, null); }
private void appendBlock(ByteArrayInputStream blockData, long offset, long writeLength) { final CloudAppendBlob blobRef = (CloudAppendBlob) this.parentBlobRef; this.accessCondition.setIfAppendPositionEqual(offset); int previousResultsCount = this.opContext.getRequestResults().size(); try { blobRef.appendBlock(blockData, writeLength, this.accessCondition, this.options, this.opContext); } catch (final IOException e) { this.lastError = e; } catch (final StorageException e) { if (this.options.getAbsorbConditionalErrorsOnRetry() && e.getHttpStatusCode() == HttpURLConnection.HTTP_PRECON_FAILED && e.getExtendedErrorInformation() != null && e.getErrorCode() != null && (e.getErrorCode() .equals(StorageErrorCodeStrings.INVALID_APPEND_POSITION) || e.getErrorCode().equals(StorageErrorCodeStrings.INVALID_MAX_BLOB_SIZE_CONDITION)) && (this.opContext.getRequestResults().size() - previousResultsCount > 1)) { // Pre-condition failure on a retry should be ignored in a single writer scenario since // the request succeeded in the first attempt. Logger.info(this.opContext, SR.PRECONDITION_FAILURE_IGNORED); } else { this.lastError = Utility.initIOException(e); } } }
private void appendBlock(ByteArrayInputStream blockData, long offset, long writeLength) { final CloudAppendBlob blobRef = (CloudAppendBlob) this.parentBlobRef; this.accessCondition.setIfAppendPositionEqual(offset); int previousResultsCount = this.opContext.getRequestResults().size(); try { blobRef.appendBlock(blockData, writeLength, this.accessCondition, this.options, this.opContext); } catch (final IOException e) { this.lastError = e; } catch (final StorageException e) { if (this.options.getAbsorbConditionalErrorsOnRetry() && e.getHttpStatusCode() == HttpURLConnection.HTTP_PRECON_FAILED && e.getExtendedErrorInformation() != null && e.getExtendedErrorInformation().getErrorCode() != null && (e.getExtendedErrorInformation().getErrorCode() .equals(StorageErrorCodeStrings.INVALID_APPEND_POSITION) || e.getExtendedErrorInformation() .getErrorCode().equals(StorageErrorCodeStrings.INVALID_MAX_BLOB_SIZE_CONDITION)) && (this.opContext.getRequestResults().size() - previousResultsCount > 1)) { // Pre-condition failure on a retry should be ignored in a single writer scenario since // the request succeeded in the first attempt. Logger.info(this.opContext, SR.PRECONDITION_FAILURE_IGNORED); } else { this.lastError = Utility.initIOException(e); } } }
private void updateAppendBlob(Exchange exchange) throws Exception { CloudAppendBlob client = BlobServiceUtil.createAppendBlobClient(getConfiguration()); configureCloudBlobForWrite(client); BlobServiceRequestOptions opts = BlobServiceUtil.getRequestOptions(exchange); if (opts.getAccessCond() == null) { // Default: do not reset the blob content if the blob already exists opts.setAccessCond(AccessCondition.generateIfNotExistsCondition()); } Boolean appendBlobCreated = exchange.getIn().getHeader(BlobServiceConstants.APPEND_BLOCK_CREATED, Boolean.class); if (Boolean.TRUE != appendBlobCreated) { doCreateAppendBlob(client, opts, exchange); } InputStream inputStream = getInputStreamFromExchange(exchange); try { client.appendBlock(inputStream, -1, opts.getAccessCond(), opts.getRequestOpts(), opts.getOpContext()); } finally { closeInputStreamIfNeeded(inputStream); } }
@Test public void testAppendBlockFromStream() throws StorageException, IOException, URISyntaxException { int blobSize = 2 * 1024; String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); CloudAppendBlob blob = container.getAppendBlobReference(blobName); blob.createOrReplace(); // Append a block byte[] buffer = BlobTestHelper.getRandomBuffer(blobSize); ByteArrayInputStream sourceStream = new ByteArrayInputStream(buffer); long pos = blob.appendBlock(sourceStream, -1); assertEquals(0, pos); // Download and verify equality byte[] resultBuffer = new byte[blobSize]; blob.downloadToByteArray(resultBuffer, 0); for (int i = 0; i < blob.getProperties().getLength(); i++) { assertEquals(buffer[i], resultBuffer[i]); } // Append another block to check the position is updated correctly sourceStream = new ByteArrayInputStream(buffer); pos = blob.appendBlock(sourceStream, -1); assertEquals(blobSize, pos); }
blob.appendBlock(sourceStream, -1); accessCondition.setIfMaxSizeLessThanOrEqual(1024*2+1L); sourceStream = new ByteArrayInputStream(buffer); blob.appendBlock(sourceStream, -1, accessCondition, null, null); accessCondition.setIfAppendPositionEqual(1024*2L); sourceStream = new ByteArrayInputStream(buffer); blob.appendBlock(sourceStream, -1, accessCondition, null, null); try { sourceStream = new ByteArrayInputStream(buffer); blob.appendBlock(sourceStream, -1, accessCondition, null, null); fail("Expected a condition failure."); } catch (StorageException ex) { try { sourceStream = new ByteArrayInputStream(buffer); blob.appendBlock(sourceStream, -1, accessCondition, null, null); fail("Expected a condition failure."); } catch (StorageException ex) {
policy.getPermissions().contains(SharedAccessAccountPermissions.WRITE)) { sasBlob.appendBlock(sourceStream, length); sasBlob.appendBlock(sourceStream, length); fail(); assertMessagesMatch(AccountSasTests.INVALID_PERMISSION_MESSAGE, ex); sourceStream = BlobTestHelper.getRandomDataStream(length); blob.appendBlock(sourceStream, length);
permissions.contains(SharedAccessBlobPermissions.WRITE)) { final CloudAppendBlob appBlob = (CloudAppendBlob) blob; appBlob.appendBlock(BlobTestHelper.getRandomDataStream(512), 512); appBlob.appendBlock(BlobTestHelper.getRandomDataStream(512), 512); fail();
blob.createOrReplace(); final ByteArrayInputStream sourceStream = new ByteArrayInputStream(buffer); blob.appendBlock(sourceStream, blobSize);