/** * Returns a reference to a {@link CloudPageBlob} object that represents a page blob in this container. * * @param blobName * A <code>String</code> that represents the name of the blob. * * @return A {@link CloudPageBlob} object that represents a reference to the specified page blob. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ public CloudPageBlob getPageBlobReference(final String blobName) throws URISyntaxException, StorageException { return this.getPageBlobReference(blobName, null); }
/** * Returns a reference to a {@link CloudPageBlob} object that represents a page blob in this container. * * @param blobName * A <code>String</code> that represents the name of the blob. * * @return A {@link CloudPageBlob} object that represents a reference to the specified page blob. * * @throws StorageException * If a storage service error occurred. * @throws URISyntaxException * If the resource URI is invalid. */ public CloudPageBlob getPageBlobReference(final String blobName) throws URISyntaxException, StorageException { return this.getPageBlobReference(blobName, null); }
@Override public CloudBlobWrapper getPageBlobReference(String relativePath) throws URISyntaxException, StorageException { return new CloudPageBlobWrapperImpl( container.getPageBlobReference(relativePath)); }
private void uploadFile(CloudBlobContainer container, String fileKey, File sourceFile) { PageBlobOutputStream pageBlobOutputStream = null; SnappyOutputStream compress = null; BufferedOutputStream bufferedOutputStream = null; try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFile))) { logger.info("Initiating upload for file: {} | key: {}", sourceFile.getAbsolutePath(), fileKey); final CloudPageBlob blob = container.getPageBlobReference(fileKey); pageBlobOutputStream = new PageBlobOutputStream(blob); bufferedOutputStream = new BufferedOutputStream(pageBlobOutputStream); compress = new SnappyOutputStream(bufferedOutputStream, DEFAULT_PART_SIZE_UPLOAD); IOUtils.copy(inputStream, compress, DEFAULT_PART_SIZE_UPLOAD); } catch (StorageException | URISyntaxException | IOException e) { logger.error("Unable to store blob", e); } finally { IOUtils.closeQuietly(compress); // super important that the compress close is called first in order to flush IOUtils.closeQuietly(bufferedOutputStream); IOUtils.closeQuietly(pageBlobOutputStream); } }
private static void populateContainer(CloudBlobContainer container, String delimiter) throws URISyntaxException, StorageException { for (int i = 1; i < 3; i++) { for (int j = 1; j < 3; j++) { for (int k = 1; k < 3; k++) { String directory = "TopDir" + i + delimiter + "MidDir" + j + delimiter + "EndDir" + k + delimiter + "EndBlob" + k; CloudPageBlob blob1 = container.getPageBlobReference(directory); blob1.create(0); } } CloudPageBlob blob2 = container.getPageBlobReference("TopDir" + i + delimiter + "Blob" + i); blob2.create(0); } }
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.parse(storageConnectionString); CloudBlobClient cloudBlobClient = cloudStorageAccount.createCloudBlobClient(); Iterable<CloudBlobContainer> containersList= cloudBlobClient.listContainers(); for(CloudBlobContainer container:containersList) { String currentContainerName = container.getName(); logger.debug("Current Container Name : "+currentContainerName); CloudBlobContainer blobContainer = cloudBlobClient.getContainerReference(currentContainerName); CloudPageBlob pageBlob = blobContainer.getPageBlobReference(diskName); if(pageBlob.exists()) return pageBlob.deleteIfExists(); }
@NotNull private static CloudBlob getCloudBlob(@NotNull CloudBlobContainer container, @NotNull BlobFile blobFile) throws URISyntaxException, StorageException { CloudBlob blob; if (blobFile.getType().equals(BlobType.BLOCK_BLOB.toString())) { blob = container.getBlockBlobReference(blobFile.getPath()); } else { blob = container.getPageBlobReference(blobFile.getPath()); } return blob; }
@Test public void testCloudPageBlobDownloadRangeToByteArrayNegativeTest() throws URISyntaxException, StorageException, IOException { CloudPageBlob blob = this.container.getPageBlobReference(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; }
@Test public void testPageBlobUploadFromStreamTest() throws URISyntaxException, StorageException, IOException { final String pageBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testPageBlob"); final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(pageBlobName); int length = 2 * 1024; ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); pageBlobRef.upload(srcStream, length); ByteArrayOutputStream dstStream = new ByteArrayOutputStream(); pageBlobRef.download(dstStream); BlobTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); length = 5 * 1024 * 1024; srcStream = BlobTestHelper.getRandomDataStream(length); pageBlobRef.upload(srcStream, length); dstStream = new ByteArrayOutputStream(); pageBlobRef.download(dstStream); BlobTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); }
@Override public CloudBlobWrapper getPageBlobReference(String relativePath) throws URISyntaxException, StorageException { try { CloudPageBlob blob = (sasKeyGenerator!=null) ? new CloudPageBlob(sasKeyGenerator.getRelativeBlobSASUri(storageAccount, getName(), relativePath)) : container.getPageBlobReference(relativePath); blob.getServiceClient().setDefaultRequestOptions( container.getServiceClient().getDefaultRequestOptions()); return new SASCloudPageBlobWrapperImpl(blob); } catch (SASKeyGenerationException sasEx) { String errorMsg = "Encountered SASKeyGeneration exception while " + "generating SAS Key for relativePath : " + relativePath + " inside container : " + getName() + " Storage account : " + storageAccount; LOG.error(errorMsg); throw new StorageException(SAS_ERROR_CODE, errorMsg, sasEx); } } }
@Test public void testUploadDownloadFromFile() throws IOException, StorageException, URISyntaxException { String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); final CloudPageBlob blob = this.container.getPageBlobReference(blobName); this.doUploadDownloadFileTest(blob, 512); this.doUploadDownloadFileTest(blob, 4096); this.doUploadDownloadFileTest(blob, 5 * 1024 * 1024); this.doUploadDownloadFileTest(blob, 11 * 1024 * 1024); }
@Test public void testUploadFromByteArray() throws Exception { String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); final CloudPageBlob blob = this.container.getPageBlobReference(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 testCloudPageBlobDownloadToByteArray() throws URISyntaxException, StorageException, IOException { CloudPageBlob blob = this.container.getPageBlobReference("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 testBlobEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException { final String pageBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testPageBlob"); final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(pageBlobName); 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", ""); } }); pageBlobRef.upload(srcStream, length, null, null, context); pageBlobRef.download(new ByteArrayOutputStream(), null, null, context); }
/** * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void testPageBlobNamePlusEncodingTest() throws StorageException, URISyntaxException, IOException, InterruptedException { final int length = 1 * 1024; final CloudPageBlob originalBlob = (CloudPageBlob) BlobTestHelper.uploadNewBlob(this.container, BlobType.PAGE_BLOB, "a+b.txt", length, null); final CloudPageBlob copyBlob = this.container.getPageBlobReference(originalBlob.getName() + "copyed"); copyBlob.startCopy(originalBlob); BlobTestHelper.waitForCopy(copyBlob); copyBlob.downloadAttributes(); }
private void testGetParent(String delimiter, CloudBlobContainer container) throws URISyntaxException, StorageException { CloudPageBlob blob = container.getPageBlobReference("Dir1" + delimiter + "Blob1"); blob.create(0); try { assertTrue(blob.exists()); assertEquals(blob.getName(), "Dir1" + delimiter + "Blob1"); CloudBlobDirectory parent = blob.getParent(); assertEquals("Dir1" + delimiter, parent.getPrefix()); } finally { blob.deleteIfExists(); } }
@Test public void testOpenOutputStreamNoArgs() throws URISyntaxException, StorageException { String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); CloudPageBlob pageBlob = this.container.getPageBlobReference(blobName); try { pageBlob.openWriteExisting(); } catch (StorageException ex) { assertEquals("The specified blob does not exist.", ex.getMessage()); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode()); } pageBlob.openWriteNew(1024); pageBlob.openWriteExisting(); CloudPageBlob pageBlob2 = this.container.getPageBlobReference(blobName); pageBlob2.downloadAttributes(); assertEquals(1024, pageBlob2.getProperties().getLength()); assertEquals(BlobType.PAGE_BLOB, pageBlob2.getProperties().getBlobType()); }
/** * Create a blob and try to download a range of its contents * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void testPageBlobDownloadRangeValidationTest() throws StorageException, URISyntaxException, IOException{ final int length = 5 * 1024 * 1024; final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob"); final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(blockBlobName); pageBlobRef.upload(BlobTestHelper.getRandomDataStream(length), length); //Download full blob pageBlobRef.download(new ByteArrayOutputStream()); assertEquals(length, pageBlobRef.getProperties().getLength()); //Download blob range. byte[] downloadBuffer = new byte[100]; int downloadLength = pageBlobRef.downloadRangeToByteArray(0, (long) 100, downloadBuffer, 0); assertEquals(length, pageBlobRef.getProperties().getLength()); assertEquals(100, downloadLength); }
private void testDelimitersInARow(String delimiter, CloudBlobContainer container) throws URISyntaxException, StorageException { CloudPageBlob blob = container.getPageBlobReference(delimiter + delimiter + delimiter + "Blob1"); // Traverse from leaf to root CloudBlobDirectory directory1 = blob.getParent(); assertEquals(delimiter + delimiter + delimiter, directory1.getPrefix()); CloudBlobDirectory directory2 = directory1.getParent(); assertEquals(delimiter + delimiter, directory2.getPrefix()); CloudBlobDirectory directory3 = directory2.getParent(); assertEquals(delimiter, directory3.getPrefix()); // Traverse from root to leaf CloudBlobDirectory directory4 = container.getDirectoryReference(delimiter); CloudBlobDirectory directory5 = directory4.getDirectoryReference(delimiter); assertEquals(delimiter + delimiter, directory5.getPrefix()); CloudBlobDirectory directory6 = directory5.getDirectoryReference(delimiter); assertEquals(delimiter + delimiter + delimiter, directory6.getPrefix()); CloudPageBlob blob2 = directory6.getPageBlobReference("Blob1"); assertEquals(delimiter + delimiter + delimiter + "Blob1", blob2.getName()); assertEquals(blob.getUri(), blob2.getUri()); }