/** * @covers WindowsAzure\Blob\Models\Block::setBlockId * @covers WindowsAzure\Blob\Models\Block::getBlockId */ public function testSetBlockId() { // Setup $block = new Block(); $expected = '1234'; // Test $block->setBlockId($expected); // Assert $this->assertEquals($expected, $block->getBlockId()); }
/** * @covers WindowsAzure\Blob\Models\BlockList::create */ public function testCreate() { // Setup $block1 = new Block(); $block1->setBlockId('123'); $block1->setType(BlobBlockType::COMMITTED_TYPE); $block2 = new Block(); $block2->setBlockId('223'); $block2->setType(BlobBlockType::UNCOMMITTED_TYPE); $block3 = new Block(); $block3->setBlockId('333'); $block3->setType(BlobBlockType::LATEST_TYPE); // Test $blockList = BlockList::create(array($block1, $block2, $block3)); // Assert $this->assertCount(3, $blockList->getEntries()); $b1 = $blockList->getEntry($block1->getBlockId()); $b2 = $blockList->getEntry($block2->getBlockId()); $b3 = $blockList->getEntry($block3->getBlockId()); $this->assertEquals($block1, $b1); $this->assertEquals($block2, $b2); $this->assertEquals($block3, $b3); }
/** * Upload the given file to an Azure Storage container as a block blob. * * Block blobs are comprised of blocks, each of which is identified by a block ID. * This allows creation or modification of a block blob by writing a set of blocks * and committing them by their block IDs, resulting in an overall efficient upload. * * If writing a block blob that is no more than 64MB in size, upload it * in its entirety with a single write operation. Otherwise, chunk the blob into discrete * blocks and upload each of them, then commit the blob ID to signal to Azure that they * should be combined into a blob. Files over 64MB are then deleted from temporary local storage. * * When you upload a block to a blob in your storage account, it is associated with the * specified block blob, but it does not become part of the blob until you commit a list * of blocks that includes the new block's ID. * * @param string $containerName The container to add the blob to. * @param string $blobName The name of the blob to upload. * @param string $localFileName The full path to local file to be uploaded. * @param string $blobContentType Optional. Content type of the blob. * @param array $metadata Optional. Metadata to describe the blob. * * @throws \Exception|ServiceException Exception if local file can't be read; * ServiceException if response code is incorrect. */ public static function putBlockBlob($containerName, $blobName, $localFileName, $blobContentType = null, $metadata = array()) { $copyBlobResult = null; $is_large_file = false; // Open file $handle = fopen($localFileName, 'r'); if ($handle === false) { throw new Exception('Could not open the local file ' . $localFileName); } /** @var \WindowsAzure\Blob\BlobRestProxy $blobRestProxy */ $blobRestProxy = WindowsAzureStorageUtil::getStorageClient(); try { if (filesize($localFileName) < self::MAX_BLOB_SIZE) { $createBlobOptions = new CreateBlobOptions(); $createBlobOptions->setBlobContentType($blobContentType); $createBlobOptions->setMetadata($metadata); $blobRestProxy->createBlockBlob($containerName, $blobName, $handle, $createBlobOptions); fclose($handle); } else { $is_large_file = true; // Determine number of page blocks $numberOfBlocks = ceil(filesize($localFileName) / self::MAX_BLOB_TRANSFER_SIZE); // Generate block id's $blocks = array(); for ($i = 0; $i < $numberOfBlocks; $i++) { /** @var WindowsAzure\Blob\Models\Block */ $block = new Block(); $block->setBlockId(self::_generateBlockId($i)); $block->setType(BlobBlockType::LATEST_TYPE); // Seek position in file fseek($handle, $i * self::MAX_BLOB_TRANSFER_SIZE); // Read contents $fileContents = fread($handle, self::MAX_BLOB_TRANSFER_SIZE); // Put block $blobRestProxy->createBlobBlock($containerName, $blobName, $block->getBlockId(), $fileContents); // Save it for later $blocks[$i] = $block; } // Close file fclose($handle); // Set Block Blob's content type and metadata $commitBlockBlobOptions = new CommitBlobBlocksOptions(); $commitBlockBlobOptions->setBlobContentType($blobContentType); $commitBlockBlobOptions->setMetadata($metadata); // Commit the block list $blobRestProxy->commitBlobBlocks($containerName, $blobName, $blocks, $commitBlockBlobOptions); if ($is_large_file) { // Delete large temp files when we're done try { //TODO: add option to keep this file if so desired if (self::blob_exists_in_container($blobName, $containerName)) { wp_delete_file($localFileName); // Dispose file contents $fileContents = null; unset($fileContents); } else { throw new Exception(sprintf(__('The blob %1$2 was not uploaded to container %2$2. Please try again.', 'windows-azure-storage'), $blobName, $containerName)); } } catch (Exception $ex) { echo '<p class="notice">' . esc_html($ex->getMessage()) . '</p>'; } } } } catch (ServiceException $exception) { if (!$handle) { fclose($handle); } throw $exception; } }