/** * Downloads a blob to a string using the specified encoding. * * @param charsetName * A <code>String</code> which represents the name of the charset to use to encode the content. * If null, the platform's default encoding is used. * @param accessCondition * An {@link AccessCondition} object that represents the access conditions for the blob. * @param options * A {@link BlobRequestOptions} object that specifies any additional options for the request. Specifying * <code>null</code> will use the default request options from the associated service client ( * {@link CloudBlobClient}). * @param opContext * An {@link OperationContext} object that represents the context for the current operation. This object * is used to track requests to the storage service, and to provide additional runtime information about * the operation. * * @return A <code>String</code> which represents the blob's contents. * * @throws StorageException * If a storage service error occurred. * @throws IOException */ public String downloadText(final String charsetName, final AccessCondition accessCondition, BlobRequestOptions options, OperationContext opContext) throws StorageException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); this.download(baos, accessCondition, options, opContext); return charsetName == null ? baos.toString() : baos.toString(charsetName); }
blockBlobRef.download(new ByteArrayOutputStream()); assertEquals(length, blockBlobRef.getProperties().getLength());
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testBlobUploadWithoutMD5Validation() throws URISyntaxException, StorageException, IOException { final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob"); final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName); final int length = 2 * 1024; ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); BlobRequestOptions options = new BlobRequestOptions(); options.setDisableContentMD5Validation(false); options.setStoreBlobContentMD5(false); blockBlobRef.upload(srcStream, -1, null, options, null); blockBlobRef.downloadAttributes(); blockBlobRef.getProperties().setContentMD5("MDAwMDAwMDA="); blockBlobRef.uploadProperties(null, options, null); try { blockBlobRef.download(new ByteArrayOutputStream(), null, options, null); fail(); } catch (StorageException ex) { assertEquals(306, ex.getHttpStatusCode()); assertEquals("InvalidMd5", ex.getErrorCode()); } options.setDisableContentMD5Validation(true); blockBlobRef.download(new ByteArrayOutputStream(), null, options, null); final CloudBlockBlob blockBlobRef2 = this.container.getBlockBlobReference(blockBlobName); assertNull(blockBlobRef2.getProperties().getContentMD5()); byte[] target = new byte[4]; blockBlobRef2.downloadRangeToByteArray(0L, 4L, target, 0); assertEquals("MDAwMDAwMDA=", blockBlobRef2.properties.getContentMD5()); }
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testUploadDownloadBlobProperties() throws URISyntaxException, StorageException, IOException { final int length = 0; // do this to make sure the set MD5 can be compared without an exception being thrown BlobRequestOptions options = new BlobRequestOptions(); options.setDisableContentMD5Validation(true); // with explicit upload/download of properties String blockBlobName1 = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob"); CloudBlockBlob blockBlobRef1 = this.container.getBlockBlobReference(blockBlobName1); blockBlobRef1.upload(BlobTestHelper.getRandomDataStream(length), length); // this is not set by upload (it is for page blob!), so set this manually blockBlobRef1.getProperties().setLength(length); BlobTestHelper.setBlobProperties(blockBlobRef1); BlobProperties props1 = blockBlobRef1.getProperties(); blockBlobRef1.uploadProperties(); blockBlobRef1.downloadAttributes(null, options, null); BlobProperties props2 = blockBlobRef1.getProperties(); Assert.assertEquals(props1.getLength(), props2.getLength()); BlobTestHelper.assertAreEqual(props1, props2); // by uploading/downloading the blob blockBlobName1 = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob"); blockBlobRef1 = this.container.getBlockBlobReference(blockBlobName1); BlobTestHelper.setBlobProperties(blockBlobRef1); props1 = blockBlobRef1.getProperties(); blockBlobRef1.upload(BlobTestHelper.getRandomDataStream(length), length); blockBlobRef1.download(new ByteArrayOutputStream(), null, options, null); props2 = blockBlobRef1.getProperties(); BlobTestHelper.assertAreEqual(props1, props2); }
/** * Downloads a blob to a string using the specified encoding. * * @param charsetName * A <code>String</code> which represents the name of the charset to use to encode the content. * If null, the platform's default encoding is used. * @param accessCondition * An {@link AccessCondition} object that represents the access conditions for the blob. * @param options * A {@link BlobRequestOptions} object that specifies any additional options for the request. Specifying * <code>null</code> will use the default request options from the associated service client ( * {@link CloudBlobClient}). * @param opContext * An {@link OperationContext} object that represents the context for the current operation. This object * is used to track requests to the storage service, and to provide additional runtime information about * the operation. * * @return A <code>String</code> which represents the blob's contents. * * @throws StorageException * If a storage service error occurred. * @throws IOException */ public String downloadText(final String charsetName, final AccessCondition accessCondition, BlobRequestOptions options, OperationContext opContext) throws StorageException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); this.download(baos, accessCondition, options, opContext); return charsetName == null ? baos.toString() : baos.toString(charsetName); }
private class MyTask extends AsyncTask<Void, Void, Void>{ @Override protected Void doInBackground(Void... params) { CloudStorageAccount account = CloudStorageAccount.parse(BuildConfig.AzureConnectionString); mClient = account.createCloudBlobClient(); mAssetContainer = mClient.getContainerReference("assetcontainer"); // Download the blob // For each item in the container for (ListBlobItem blobItem : mAssetContainer.listBlobs()) { // If the item is a blob, not a virtual directory if (blobItem instanceof CloudBlockBlob) { // Download the text CloudBlockBlob retrievedBlob = (CloudBlockBlob) blobItem; try { retrievedBlob.download(new FileOutputStream(mContext.getFilesDir() + "\\assets\\" + ((CloudBlockBlob) blobItem).getName())); } catch(Exception ex){} } } } }
CloudBlockBlob sasBlob = new CloudBlockBlob(uri, blobClient); File fileTarget = new File(localFileName); sasBlob.download(new FileOutputStream(fileTarget)); HashMap<String, String> user = new HashMap<String, String>(); user = sasBlob.getMetadata();
blob.download(out); HashMap<String, String> metaData = blob.getMetadata(); out.flush();
blob.download(out); HashMap<String, String> metaData = blob.getMetadata(); byte[] data = out.toByteArray();
@Test public void testBlobUploadFromStreamAccessConditionTest() throws URISyntaxException, StorageException, IOException { final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob"); final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName); AccessCondition accessCondition = AccessCondition.generateIfNotModifiedSinceCondition(new Date()); int length = 2 * 1024; ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); blockBlobRef.upload(srcStream, -1, accessCondition, null, null); ByteArrayOutputStream dstStream = new ByteArrayOutputStream(); blockBlobRef.download(dstStream); BlobTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); length = 5 * 1024 * 1024; srcStream = BlobTestHelper.getRandomDataStream(length); blockBlobRef.upload(srcStream, length); dstStream = new ByteArrayOutputStream(); blockBlobRef.download(dstStream); BlobTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); }
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testBlobUploadFromStreamTest() throws URISyntaxException, StorageException, IOException { final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob"); final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName); int length = 2 * 1024; ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); blockBlobRef.upload(srcStream, -1); ByteArrayOutputStream dstStream = new ByteArrayOutputStream(); blockBlobRef.download(dstStream); BlobTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); length = 5 * 1024 * 1024; srcStream = BlobTestHelper.getRandomDataStream(length); blockBlobRef.upload(srcStream, length); dstStream = new ByteArrayOutputStream(); blockBlobRef.download(dstStream); BlobTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); }
httpContainer.getServiceClient().getCredentials().getClass().toString()); CloudBlockBlob httpBlob = httpContainer.getBlockBlobReference(this.blob.getName()); httpBlob.download(new ByteArrayOutputStream()); httpsContainer.getServiceClient().getCredentials().getClass().toString()); CloudBlockBlob httpsBlob = httpsContainer.getBlockBlobReference(this.blob.getName()); httpsBlob.download(new ByteArrayOutputStream()); httpContainer.getServiceClient().getCredentials().getClass().toString()); CloudBlockBlob httpBlob = httpContainer.getBlockBlobReference(this.blob.getName()); httpBlob.download(new ByteArrayOutputStream()); httpsContainer.getServiceClient().getCredentials().getClass().toString()); httpsBlob = httpsContainer.getBlockBlobReference(this.blob.getName()); httpsBlob.download(new ByteArrayOutputStream());
@Test @Category(SlowTests.class) public void testBlobSaS() throws InvalidKeyException, IllegalArgumentException, StorageException, URISyntaxException, InterruptedException { SharedAccessBlobPolicy sp = createSharedAccessPolicy( EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.LIST), 3600); BlobContainerPermissions perms = new BlobContainerPermissions(); perms.getSharedAccessPolicies().put("readperm", sp); this.container.uploadPermissions(perms); Thread.sleep(30000); CloudBlockBlob sasBlob = new CloudBlockBlob(new URI(this.blob.getUri().toString() + "?" + this.blob.generateSharedAccessSignature(null, "readperm"))); sasBlob.download(new ByteArrayOutputStream()); // do not give the client and check that the new blob's client has the correct perms CloudBlob blobFromUri = new CloudBlockBlob(PathUtility.addToQuery(this.blob.getStorageUri(), this.blob.generateSharedAccessSignature(null, "readperm"))); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), blobFromUri.getServiceClient() .getCredentials().getClass().toString()); // create credentials from sas StorageCredentials creds = new StorageCredentialsSharedAccessSignature( this.blob.generateSharedAccessSignature(null, "readperm")); CloudBlobClient bClient = new CloudBlobClient(sasBlob.getServiceClient().getStorageUri(), creds); CloudBlockBlob blobFromClient = bClient.getContainerReference(this.blob.getContainer().getName()) .getBlockBlobReference(this.blob.getName()); assertEquals(StorageCredentialsSharedAccessSignature.class.toString(), blobFromClient.getServiceClient() .getCredentials().getClass().toString()); assertEquals(bClient, blobFromClient.getServiceClient()); }
sasBlob.download(new ByteArrayOutputStream(), null, null, context);
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testBlobEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException { final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob"); final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName); 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", ""); } }); blockBlobRef.upload(srcStream, -1, null, null, context); blockBlobRef.download(new ByteArrayOutputStream(), null, null, context); }
blobFromSasContainer.download(new ByteArrayOutputStream());
outStream = new ByteArrayOutputStream(length); blobSnapshotUsingRootUri.download(outStream); retrievedBuff = outStream.toByteArray(); assertEquals(length, retrievedBuff.length);
@Test public void testRetryOn304() throws StorageException, IOException, URISyntaxException { OperationContext operationContext = new OperationContext(); operationContext.getRetryingEventHandler().addListener(new StorageEvent<RetryingEvent>() { @Override public void eventOccurred(RetryingEvent eventArg) { fail("Request should not be retried."); } }); CloudBlobContainer container = BlobTestHelper.getRandomContainerReference(); try { container.create(); CloudBlockBlob blockBlobRef = (CloudBlockBlob) BlobTestHelper.uploadNewBlob(container, BlobType.BLOCK_BLOB, "originalBlob", 1024, null); AccessCondition accessCondition = AccessCondition.generateIfNoneMatchCondition(blockBlobRef.getProperties().getEtag()); blockBlobRef.download(new ByteArrayOutputStream(), accessCondition, null, operationContext); fail("Download should fail with a 304."); } catch (StorageException ex) { assertEquals("The condition specified using HTTP conditional header(s) is not met.", ex.getMessage()); } finally { container.deleteIfExists(); } }