@Override
    public StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
        try {
            final CloudBlob blob;
            if(status.isExists()) {
                if(new HostPreferences(session.getHost()).getBoolean("azure.upload.snapshot")) {
                    session.getClient().getContainerReference(containerService.getContainer(file).getName())
                            .getBlobReferenceFromServer(containerService.getKey(file)).createSnapshot();
                }
                if(status.isAppend()) {
                    // Existing append blob type
                    blob = session.getClient().getContainerReference(containerService.getContainer(file).getName())
                            .getAppendBlobReference(containerService.getKey(file));
                }
                else {
                    // Existing block blob type
                    final PathAttributes attr = new AzureAttributesFinderFeature(session, context).find(file);
                    if(BlobType.APPEND_BLOB == BlobType.valueOf(attr.getCustom().get(AzureAttributesFinderFeature.KEY_BLOB_TYPE))) {
                        blob = session.getClient().getContainerReference(containerService.getContainer(file).getName())
                                .getAppendBlobReference(containerService.getKey(file));
                    }
                    else {
                        blob = session.getClient().getContainerReference(containerService.getContainer(file).getName())
                                .getBlockBlobReference(containerService.getKey(file));
                    }
                }
            }
            else {
                // Create new blob with default type set in defaults
                switch(blobType) {
                    case APPEND_BLOB:
                        blob = session.getClient().getContainerReference(containerService.getContainer(file).getName())
                                .getAppendBlobReference(containerService.getKey(file));
                        break;
                    default:
                        blob = session.getClient().getContainerReference(containerService.getContainer(file).getName())
                                .getBlockBlobReference(containerService.getKey(file));
                }
            }
            if(StringUtils.isNotBlank(status.getMime())) {
                blob.getProperties().setContentType(status.getMime());
            }
            // Add previous metadata when overwriting file
            final HashMap<String, String> headers = new HashMap<>(status.getMetadata());
            blob.setMetadata(headers);
            // Remove additional headers not allowed in metadata and move to properties
            if(headers.containsKey(HttpHeaders.CACHE_CONTROL)) {
                blob.getProperties().setCacheControl(headers.get(HttpHeaders.CACHE_CONTROL));
                headers.remove(HttpHeaders.CACHE_CONTROL);
            }
            if(headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
                blob.getProperties().setContentType(headers.get(HttpHeaders.CONTENT_TYPE));
                headers.remove(HttpHeaders.CONTENT_TYPE);
            }
            final Checksum checksum = status.getChecksum();
            if(Checksum.NONE != checksum) {
                switch(checksum.algorithm) {
                    case md5:
                        headers.remove(HttpHeaders.CONTENT_MD5);
                        blob.getProperties().setContentMD5(status.getChecksum().base64);
                        break;
                }
            }
            final BlobRequestOptions options = new BlobRequestOptions();
            options.setConcurrentRequestCount(1);
            options.setStoreBlobContentMD5(new HostPreferences(session.getHost()).getBoolean("azure.upload.md5"));
            final BlobOutputStream out;
            if(status.isAppend()) {
                options.setStoreBlobContentMD5(false);
                if(blob instanceof CloudAppendBlob) {
                    out = ((CloudAppendBlob) blob).openWriteExisting(AccessCondition.generateEmptyCondition(), options, context);
                }
                else {
                    throw new NotfoundException(String.format("Unexpected blob type for %s", blob.getName()));
                }
            }
            else {
                if(blob instanceof CloudAppendBlob) {
                    out = ((CloudAppendBlob) blob).openWriteNew(AccessCondition.generateEmptyCondition(), options, context);
                }
                else {
                    out = ((CloudBlockBlob) blob).openOutputStream(AccessCondition.generateEmptyCondition(), options, context);
                }
            }
            return new VoidStatusOutputStream(out) {
                @Override
                protected void handleIOException(final IOException e) throws IOException {
                    if(StringUtils.equals(SR.STREAM_CLOSED, e.getMessage())) {
                        log.warn(String.format("Ignore failure %s", e));
                        return;
                    }
                    final Throwable cause = ExceptionUtils.getRootCause(e);
                    if(cause instanceof StorageException) {
                        throw new IOException(e.getMessage(), new AzureExceptionMappingService().map((StorageException) cause));
                    }
                    throw e;
                }
            };
        }
        catch(StorageException e) {
            throw new AzureExceptionMappingService().map("Upload {0} failed", e, file);
        }
        catch(URISyntaxException e) {
            throw new NotfoundException(e.getMessage(), e);
        }
    }
@Test
    public void testWriteOverrideBlockBlob() throws Exception {
        final OperationContext context
                = new OperationContext();
        final TransferStatus status = new TransferStatus();
        status.setMime("text/plain");
        final byte[] content = RandomUtils.nextBytes(513);
        status.setLength(content.length);
        status.setChecksum(new MD5ChecksumCompute().compute(new ByteArrayInputStream(content), new TransferStatus().withLength(content.length)));
        status.setMetadata(Collections.singletonMap("Cache-Control", "public,max-age=86400"));
        final Path container = new Path("cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
        final Path test = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
        final OutputStream out = new AzureWriteFeature(session, BlobType.BLOCK_BLOB, context).write(test, status, new DisabledConnectionCallback());
        assertNotNull(out);
        new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
        assertTrue(new AzureFindFeature(session, context).find(test));
        final PathAttributes attributes = new AzureAttributesFinderFeature(session, context).find(test);
        assertEquals(content.length, attributes.getSize());
        final Map<String, String> metadata = new AzureMetadataFeature(session, context).getMetadata(test);
        assertEquals("text/plain", metadata.get("Content-Type"));
        assertEquals("public,max-age=86400", metadata.get("Cache-Control"));
        final byte[] buffer = new byte[content.length];
        final InputStream in = new AzureReadFeature(session, context).read(test, new TransferStatus(), new DisabledConnectionCallback());
        IOUtils.readFully(in, buffer);
        in.close();
        assertArrayEquals(content, buffer);
        final OutputStream overwrite = new AzureWriteFeature(session, context).write(test, new TransferStatus().exists(true)
                .withLength("overwrite".getBytes(StandardCharsets.UTF_8).length).withMetadata(Collections.singletonMap("Content-Type", "text/plain")), new DisabledConnectionCallback());
        new StreamCopier(new TransferStatus(), new TransferStatus())
                .transfer(new ByteArrayInputStream("overwrite".getBytes(StandardCharsets.UTF_8)), overwrite);
        overwrite.close();
        // Test double close
        overwrite.close();
        assertEquals("overwrite".getBytes(StandardCharsets.UTF_8).length, new AzureAttributesFinderFeature(session, context).find(test).getSize());
        new AzureDeleteFeature(session, context).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
    }