/** * @covers WindowsAzure\Blob\BlobRestProxy::listBlobs */ public function testListBlobsWithOptionsWithDelimiter() { $this->skipIfEmulated(); // Setup $name = 'listblobswithoptionswithdelimiter' . $this->createSuffix(); $blob1 = 'blob1'; $blob2 = 'blob2'; $blob3 = 'blob3'; $blob4 = 'lblob1'; $blob5 = 'lblob2'; $blob6 = 'lblob3'; $length = 512; $options = new ListBlobsOptions(); $options->setDelimiter('b'); $options->setIncludeMetadata(true); $options->setIncludeUncommittedBlobs(true); $options->setMaxResults(10); $this->createContainer($name); $this->restProxy->createPageBlob($name, $blob1, $length); $this->restProxy->createPageBlob($name, $blob2, $length); $this->restProxy->createPageBlob($name, $blob3, $length); $this->restProxy->createPageBlob($name, $blob4, $length); $this->restProxy->createPageBlob($name, $blob5, $length); $this->restProxy->createPageBlob($name, $blob6, $length); // Test $result = $this->restProxy->listBlobs($name, $options); // Assert $this->assertCount(0, $result->getBlobs()); $this->assertCount(2, $result->getBlobPrefixes()); }
/** * @covers WindowsAzure\Blob\Models\ListBlobsOptions::getDelimiter */ public function testGetDelimiter() { // Setup $options = new ListBlobsOptions(); $expected = 'mydelimiter'; $options->setDelimiter($expected); // Test $actual = $options->getDelimiter(); // Assert $this->assertEquals($expected, $actual); }
/** * {@inheritdoc} */ public function listContents($directory = '', $recursive = false) { $directory = $this->applyPathPrefix($directory); // Append trailing slash only for directory other than root (which after normalization is an empty string). // Listing for / doesn't work properly otherwise. if (strlen($directory)) { $directory = rtrim($directory, '/') . '/'; } $options = new ListBlobsOptions(); $options->setPrefix($directory); if (!$recursive) { $options->setDelimiter('/'); } /** @var ListBlobsResult $listResults */ $listResults = $this->client->listBlobs($this->container, $options); $contents = []; foreach ($listResults->getBlobs() as $blob) { $contents[] = $this->normalizeBlobProperties($blob->getName(), $blob->getProperties()); } if (!$recursive) { $contents = array_merge($contents, array_map([$this, 'normalizeBlobPrefix'], $listResults->getBlobPrefixes())); } return Util::emulateDirectories($contents); }
/** * List blobs * * @param string $container Container name * @param string $prefix Optional. Filters the results to return only blobs whose name begins with the * specified prefix. * @param string $delimiter Optional. Delimiter, i.e. '/', for specifying folder hierarchy * * @return array * @throws \Exception */ public function listBlobs($container, $prefix = '', $delimiter = '') { $this->checkConnection(); $options = new ListBlobsOptions(); if (!empty($delimiter)) { $options->setDelimiter($delimiter); } if (!empty($prefix)) { $options->setPrefix($prefix); } /** @var ListBlobsResult $results */ $results = $this->blobConn->listBlobs($container, $options); $blobs = $results->getBlobs(); $prefixes = $results->getBlobPrefixes(); $out = []; /** @var \WindowsAzure\Blob\Models\Blob $blob */ foreach ($blobs as $blob) { $name = $blob->getName(); if (0 == strcmp($prefix, $name)) { continue; } $props = $blob->getProperties(); $out[] = ['name' => $name, 'last_modified' => gmdate(static::TIMESTAMP_FORMAT, $props->getLastModified()->getTimestamp()), 'content_length' => $props->getContentLength(), 'content_type' => $props->getContentType(), 'content_encoding' => $props->getContentEncoding(), 'content_language' => $props->getContentLanguage()]; } foreach ($prefixes as $blob) { $out[] = ['name' => $blob->getName(), 'content_type' => null, 'content_length' => 0, 'last_modified' => null]; } return $out; }
/** * BlobのPrefix一覧取得 * @param type $containerName * @return type * @throws Exception */ public function getBobPrefixes($containerName, $prefix = null, $delimiter = null) { $options = new ListBlobsOptions(); $blobPrefixes = array(); while (true) { try { if ($prefix) { $options->setPrefix($prefix); } if ($delimiter) { $options->setDelimiter($delimiter); } $blobList = $this->blobRestProxy->listBlobs($containerName, $options); } catch (Exception $e) { throw new Exception($e->getMessage(), $e->getCode()); } foreach ($blobList->getBlobPrefixes() as $blobPrefix) { $blobPrefixes[] = $blobPrefix->getName(); } $nextMarker = $blobList->getNextMarker(); if (!$nextMarker || strlen($nextMarker) == 0) { break; } $options->setMarker($nextMarker); } return $blobPrefixes; }
/** * @covers WindowsAzure\Blob\BlobRestProxy::createPageBlob * @covers WindowsAzure\Blob\BlobRestProxy::deleteBlob * @covers WindowsAzure\Blob\BlobRestProxy::listBlobs */ public function testListBlobsWithDelimiterWorks() { // Arrange $blobNames = array('myblob1', 'myblob2', 'dir1-blob1', 'dir1-blob2', 'dir2-dir21-blob3', 'dir2-dir22-blob3'); foreach ($blobNames as $blob) { $this->restProxy->createPageBlob(self::$_test_container_for_listing, $blob, 512); } // Act $opts = new ListBlobsOptions(); $opts->setDelimiter('-'); $results = $this->restProxy->listBlobs(self::$_test_container_for_listing, $opts); $opts->setPrefix('dir1-'); $results2 = $this->restProxy->listBlobs(self::$_test_container_for_listing, $opts); $opts->setPrefix('dir2-'); $results3 = $this->restProxy->listBlobs(self::$_test_container_for_listing, $opts); $opts->setPrefix('dir2-dir21-'); $results4 = $this->restProxy->listBlobs(self::$_test_container_for_listing, $opts); $opts->setPrefix('dir2-dir22-'); $results5 = $this->restProxy->listBlobs(self::$_test_container_for_listing, $opts); $opts->setPrefix('dir2-dir44-'); $results6 = $this->restProxy->listBlobs(self::$_test_container_for_listing, $opts); foreach ($blobNames as $blob) { $this->restProxy->deleteBlob(self::$_test_container_for_listing, $blob); } // Assert $this->assertNotNull($results, '$results'); $this->assertEquals(2, count($results->getBlobs()), 'count($results->getBlobs())'); $this->assertEquals(2, count($results->getBlobPrefixes()), 'count($results->getBlobPrefixes())'); $this->assertEquals(2, count($results2->getBlobs()), 'count($results2->getBlobs())'); $this->assertEquals(0, count($results2->getBlobPrefixes()), 'count($results2->getBlobPrefixes())'); $this->assertEquals(0, count($results3->getBlobs()), 'count($results3->getBlobs())'); $this->assertEquals(2, count($results3->getBlobPrefixes()), 'count($results3->getBlobPrefixes())'); $this->assertEquals(1, count($results4->getBlobs()), 'count($results4->getBlobs())'); $this->assertEquals(0, count($results4->getBlobPrefixes()), 'count($results4->getBlobPrefixes())'); $this->assertEquals(1, count($results5->getBlobs()), 'count($results5->getBlobs())'); $this->assertEquals(0, count($results5->getBlobPrefixes()), 'count($results5->getBlobPrefixes())'); $this->assertEquals(0, count($results6->getBlobs()), 'count($results6->getBlobs())'); $this->assertEquals(0, count($results6->getBlobPrefixes()), 'count($results6->getBlobPrefixes())'); }