@Override public void writeLine(String line) throws IOException { try { if (!gcJournal.exists()) { gcJournal.createOrReplace(); } gcJournal.appendText(line + "\n", Charsets.UTF_8.name(), null, null, null); } catch (StorageException e) { throw new IOException(e); } }
public AzureJournalWriter() throws IOException { List<CloudAppendBlob> blobs = getJournalBlobs(); if (blobs.isEmpty()) { try { currentBlob = directory.getAppendBlobReference(getJournalFileName(1)); currentBlob.createOrReplace(); } catch (URISyntaxException | StorageException e) { throw new IOException(e); } } else { currentBlob = blobs.get(0); } Integer bc = currentBlob.getProperties().getAppendBlobCommittedBlockCount(); blockCount = bc == null ? 0 : bc; }
/** * Opens an output stream object to write data to the append blob. The append blob does not need to yet exist. If * the blob already exists, this will replace it. * <p> * To avoid overwriting and instead throw an error, please use the * {@link #openWriteNew(AccessCondition, BlobRequestOptions, OperationContext)} overload with the appropriate * {@link AccessCondition}. * <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. * * @return A {@link BlobOutputStream} object used to write data to the blob. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public BlobOutputStream openWriteNew() throws StorageException { return this.openWriteNew(null /* accessCondition */, null /* options */, null /* opContext */); }
@Override public List<String> readLines() throws IOException { try { if (!gcJournal.exists()) { return Collections.emptyList(); } byte[] data = new byte[(int) gcJournal.getProperties().getLength()]; gcJournal.downloadToByteArray(data, 0); return IOUtils.readLines(new ByteArrayInputStream(data), Charset.defaultCharset()); } catch (StorageException e) { throw new IOException(e); } } }
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); } }
source.upload(stream, buffer.length); source.getMetadata().put("Test", "value"); source.uploadMetadata(); CloudAppendBlob snapshot = (CloudAppendBlob) source.createSnapshot(); source.getMetadata().put("Test", "newvalue"); source.uploadMetadata(); source.getProperties().setContentMD5(null); source.upload(stream2, buffer.length); source.download(outputStream); snapshot.download(snapshotStream); BlobTestHelper.assertStreamsAreEqual(stream2, new ByteArrayInputStream( outputStream.toByteArray())); source.downloadAttributes(); snapshot.downloadAttributes(); assertFalse(source.getMetadata().get("Test") .equals(snapshot.getMetadata().get("Test"))); String copyId = copy.startCopy(BlobTestHelper.defiddler(snapshot)); BlobTestHelper.waitForCopy(copy); copy.download(copyStream); assertEquals(CopyStatus.SUCCESS, copy.getCopyState().getStatus());
String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); CloudAppendBlob source = this.container.getAppendBlobReference(blobName); source.createOrReplace(); byte[] buffer = BlobTestHelper.getRandomBuffer(512); ByteArrayInputStream stream = new ByteArrayInputStream(buffer); source.upload(stream, buffer.length); source.getMetadata().put("Test", "value"); source.uploadMetadata(); String sasToken = copy.generateSharedAccessSignature(policy, null); CloudAppendBlob copySas = new CloudAppendBlob(new URI(copy.getUri().toString() + "?" + sasToken)); CloudAppendBlob snapshot = (CloudAppendBlob) source.createSnapshot(); CloudAppendBlob sasBlob = (CloudAppendBlob) sasClient.getContainerReference(container.getName()) .getBlobReferenceFromServer(snapshot.getName(), snapshot.snapshotID, null, null, null); sasBlob.exists(); String copyId = copySas.startCopy(BlobTestHelper.defiddler(sasBlob)); BlobTestHelper.waitForCopy(copySas); copySas.downloadAttributes(); BlobProperties prop1 = copySas.getProperties(); BlobProperties prop2 = sasBlob.getProperties(); assertEquals("value", copySas.getMetadata().get("Test")); assertEquals(copyId, copySas.getCopyState().getCopyId()); snapshot.delete(); source.delete();
@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 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); }
@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()); }
private void createNextFile(int suffix) throws IOException { try { currentBlob = directory.getAppendBlobReference(getJournalFileName(suffix + 1)); currentBlob.createOrReplace(); blockCount = 0; } catch (URISyntaxException | StorageException e) { throw new IOException(e); } }
.getAppendBlobReference(appendBlobName1); appendBlobRef1.upload(BlobTestHelper.getRandomDataStream(length), length); appendBlobRef1.getProperties().setLength(length); BlobProperties props1 = appendBlobRef1.getProperties(); appendBlobRef1.uploadProperties(); appendBlobRef1.downloadAttributes(null, options, null); BlobProperties props2 = appendBlobRef1.getProperties(); props1 = appendBlobRef1.getProperties(); appendBlobRef1.upload(BlobTestHelper.getRandomDataStream(length), length); appendBlobRef1.getProperties().setLength(length); appendBlobRef1.download(new ByteArrayOutputStream(), null, options, null); props2 = appendBlobRef1.getProperties();
.getAppendBlobReference(containerService.getKey(file)); if(StringUtils.isNotBlank(status.getMime())) { blob.getProperties().setContentType(status.getMime()); blob.setMetadata(headers); blob.getProperties().setCacheControl(headers.get(HttpHeaders.CACHE_CONTROL)); headers.remove(HttpHeaders.CACHE_CONTROL); blob.getProperties().setContentType(headers.get(HttpHeaders.CONTENT_TYPE)); headers.remove(HttpHeaders.CONTENT_TYPE); case md5: try { blob.getProperties().setContentMD5(Base64.toBase64String(Hex.decodeHex(status.getChecksum().hash.toCharArray()))); headers.remove(HttpHeaders.CONTENT_MD5); if(status.isAppend()) { options.setStoreBlobContentMD5(false); out = blob.openWriteExisting(AccessCondition.generateEmptyCondition(), options, context); out = blob.openWriteNew(AccessCondition.generateEmptyCondition(), options, context);
ByteArrayInputStream srcStream = BlobTestHelper .getRandomDataStream(length); blob1.upload(srcStream, length, accessCondition, null, null); blob1.createOrReplace(); accessCondition = AccessCondition.generateIfNoneMatchCondition(blob1 .getProperties().getEtag()); try { blob1.upload(srcStream, length, accessCondition, null, null); } catch (StorageException ex) { assertEquals(HttpURLConnection.HTTP_PRECON_FAILED, .getProperties().getEtag()); blob1.upload(srcStream, length, accessCondition, null, null); blob2.createOrReplace(); accessCondition = AccessCondition.generateIfMatchCondition(blob1 .getProperties().getEtag()); try { blob1.upload(srcStream, length, accessCondition, null, null); } catch (StorageException ex) { assertEquals(HttpURLConnection.HTTP_PRECON_FAILED, .getProperties().getEtag()); blob1.upload(srcStream, length, accessCondition, null, null);
.generateRandomBlobNameWithPrefix("testblob"); CloudAppendBlob blob = container.getAppendBlobReference(blobName); blob.createOrReplace(); final ByteArrayInputStream sourceStream = new ByteArrayInputStream(buffer); blob.appendBlock(sourceStream, blobSize); BlobOutputStream str = blob.openWriteExisting(); BlobOutputStream str2 = blob.openWriteExisting();
@Test public void testOpenOutputStream() throws URISyntaxException, StorageException, IOException { int blobLengthToUse = 8 * 512; byte[] buffer = BlobTestHelper.getRandomBuffer(8 * 512); String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); final CloudAppendBlob blobRef = this.container .getAppendBlobReference(blobName); blobRef.createOrReplace(); BlobOutputStream blobOutputStream = blobRef.openWriteNew(); ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer); blobOutputStream = blobRef.openWriteNew(); inputStream = new ByteArrayInputStream(buffer); blobOutputStream.write(inputStream, 512); inputStream = new ByteArrayInputStream(buffer, 512, 3 * 512); blobOutputStream.write(inputStream, 3 * 512); blobOutputStream.close(); byte[] result = new byte[blobLengthToUse]; blobRef.downloadToByteArray(result, 0); int i = 0; for (; i < 4 * 512; i++) { assertEquals(buffer[i], result[i]); } for (; i < 8 * 512; i++) { assertEquals(0, result[i]); } }
assertNoWriteOperationForSnapshot(); cipher = options.getEncryptionPolicy().createAndSetEncryptionContext(this.getMetadata(), false /* noPadding */); this.createOrReplace(accessCondition, options, opContext); } else { if (options.getStoreBlobContentMD5()) { this.downloadAttributes(accessCondition, options, opContext);
assertNoWriteOperationForSnapshot(); this.createOrReplace(accessCondition, modifiedOptions, opContext); } else { if (modifiedOptions.getStoreBlobContentMD5()) { this.downloadAttributes(accessCondition, modifiedOptions, opContext);
@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()); }