Ejemplo n.º 1
0
 /**
  * Adds new entry to the block list entries.
  * 
  * @param string $blockId The block id.
  * @param string $type    The entry type, you can use BlobBlockType.
  * 
  * @return none
  */
 public function addEntry($blockId, $type)
 {
     Validate::isString($blockId, 'blockId');
     Validate::isTrue(BlobBlockType::isValid($type), sprintf(Resources::INVALID_BTE_MSG, get_class(new BlobBlockType())));
     $block = new Block();
     $block->setBlockId($blockId);
     $block->setType($type);
     $this->_entries[] = $block;
 }
Ejemplo n.º 2
0
 /**
  * @covers WindowsAzure\Blob\Models\Block::setType
  * @covers WindowsAzure\Blob\Models\Block::getType
  */
 public function testSetType()
 {
     // Setup
     $block = new Block();
     $expected = 'BlockType';
     // Test
     $block->setType($expected);
     // Assert
     $this->assertEquals($expected, $block->getType());
 }
Ejemplo n.º 3
0
 /**
  * @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);
 }
 /**
  * @covers WindowsAzure\Blob\BlobRestProxy::commitBlobBlocks
  * @covers WindowsAzure\Blob\Models\BlockList::toXml
  */
 public function testCommitBlobBlocksWithArray()
 {
     // Setup
     $name = 'commitblobblockswitharray' . $this->createSuffix();
     $blob = 'myblob';
     $id1 = 'AAAAAA==';
     $id2 = 'ANAAAA==';
     $block1 = new Block();
     $block1->setBlockId($id1);
     $block1->setType(BlobBlockType::LATEST_TYPE);
     $block2 = new Block();
     $block2->setBlockId($id2);
     $block2->setType(BlobBlockType::LATEST_TYPE);
     $blockList = array($block1, $block2);
     $this->createContainer($name);
     $this->restProxy->createBlobBlock($name, $blob, $id1, 'Hello world');
     $this->restProxy->createBlobBlock($name, $blob, $id2, 'Hello world');
     // Test
     $this->restProxy->commitBlobBlocks($name, $blob, $blockList);
     // Assert
     $result = $this->restProxy->listBlobs($name);
     $this->assertCount(1, $result->getBlobs());
 }
 /**
  * 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;
     }
 }
Ejemplo n.º 6
0
 /**
  * Creates a new block blob or updates the content of an existing block blob.
  * 
  * Updating an existing block blob overwrites any existing metadata on the blob.
  * Partial updates are not supported with createBlockBlob the content of the
  * existing blob is overwritten with the content of the new blob. To perform a
  * partial update of the content o  f a block blob, use the createBlockList
  * method.
  * Note that the default content type is application/octet-stream.
  * 
  * @param string                   $container The name of the container.
  * @param string                   $blob      The name of the blob.
  * @param string|resource          $content   The content of the blob.
  * @param Models\CreateBlobOptions $options   The optional parameters.
  * 
  * @return CopyBlobResult
  * 
  * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179451.aspx
  */
 public function createBlockBlob($container, $blob, $content, $options = null)
 {
     Validate::isString($container, 'container');
     Validate::isString($blob, 'blob');
     Validate::notNullOrEmpty($blob, 'blob');
     Validate::isTrue(is_string($content) || is_resource($content), sprintf(Resources::INVALID_PARAM_MSG, 'content', 'string|resource'));
     $method = Resources::HTTP_PUT;
     $headers = array();
     $postParams = array();
     $queryParams = array();
     $bodySize = false;
     $path = $this->_createPath($container, $blob);
     $statusCode = Resources::STATUS_CREATED;
     if (is_null($options)) {
         $options = new CreateBlobOptions();
     }
     if (is_resource($content)) {
         $cStat = fstat($content);
         // if the resource is a remote file, $cStat will be false
         if ($cStat) {
             $bodySize = $cStat['size'];
         }
     } else {
         $bodySize = strlen($content);
     }
     // if we have a size we can try to one shot this, else failsafe on block upload
     if (is_int($bodySize) && $bodySize <= $this->_SingleBlobUploadThresholdInBytes) {
         $headers = $this->_addCreateBlobOptionalHeaders($options, $headers);
         $this->addOptionalHeader($headers, Resources::X_MS_BLOB_TYPE, BlobType::BLOCK_BLOB);
         $this->addOptionalQueryParam($queryParams, Resources::QP_TIMEOUT, $options->getTimeout());
         // If read file failed for any reason it will throw an exception.
         $body = is_resource($content) ? stream_get_contents($content) : $content;
         $response = $this->send($method, $headers, $queryParams, $postParams, $path, $statusCode, $body);
     } else {
         // This is for large or failsafe upload
         $end = 0;
         $counter = 0;
         $body = '';
         $blockIds = array();
         // if threshold is lower than 4mb, honor threshold, else use 4mb
         $blockSize = $this->_SingleBlobUploadThresholdInBytes < 4194304 ? $this->_SingleBlobUploadThresholdInBytes : 4194304;
         while (!$end) {
             if (is_resource($content)) {
                 $body = fread($content, $blockSize);
                 if (feof($content)) {
                     $end = 1;
                 }
             } else {
                 if (strlen($content) <= $blockSize) {
                     $body = $content;
                     $end = 1;
                 } else {
                     $body = substr($content, 0, $blockSize);
                     $content = substr_replace($content, '', 0, $blockSize);
                 }
             }
             $block = new Block();
             $block->setBlockId(base64_encode(str_pad($counter++, '0', 6)));
             $block->setType('Uncommitted');
             array_push($blockIds, $block);
             $this->createBlobBlock($container, $blob, $block->getBlockId(), $body);
         }
         $response = $this->commitBlobBlocks($container, $blob, $blockIds, $options);
     }
     return CopyBlobResult::create($response->getHeader());
 }
 /**
  * @covers WindowsAzure\Blob\BlobRestProxy::commitBlobBlocks
  * @covers WindowsAzure\Blob\BlobRestProxy::createBlobBlock
  * @covers WindowsAzure\Blob\BlobRestProxy::createBlockBlob
  * @covers WindowsAzure\Blob\BlobRestProxy::listBlobBlocks
  */
 public function testCommitBlobBlocksWithArrayWorks()
 {
     // Act
     $container = self::$_test_container_for_blobs;
     $blob = 'test14a';
     $blockId1 = '1fedcba';
     $blockId2 = '2abcdef';
     $blockId3 = '3zzzzzz';
     $this->restProxy->createBlockBlob($container, $blob, '');
     $this->restProxy->createBlobBlock($container, $blob, $blockId1, str_pad('', 256));
     $this->restProxy->createBlobBlock($container, $blob, $blockId2, str_pad('', 512));
     $this->restProxy->createBlobBlock($container, $blob, $blockId3, str_pad('', 195));
     $block1 = new Block();
     $block1->setBlockId($blockId1);
     $block1->setType(BlobBlockType::UNCOMMITTED_TYPE);
     $block3 = new Block();
     $block3->setBlockId($blockId3);
     $block3->setType(BlobBlockType::LATEST_TYPE);
     $blockList = array($block1, $block3);
     $this->restProxy->commitBlobBlocks($container, $blob, $blockList);
     $opts = new ListBlobBlocksOptions();
     $opts->setIncludeCommittedBlobs(true);
     $opts->setIncludeUncommittedBlobs(true);
     $result = $this->restProxy->listBlobBlocks($container, $blob, $opts);
     // Assert
     $this->assertNotNull($result, '$result');
     $this->assertNotNull($result->getLastModified(), '$result->getLastModified()');
     $this->assertNotNull($result->getETag(), '$result->getETag()');
     $this->assertEquals(256 + 195, $result->getContentLength(), '$result->getContentLength()');
     $this->assertNotNull($result->getCommittedBlocks(), '$result->getCommittedBlocks()');
     $this->assertEquals(2, count($result->getCommittedBlocks()), 'count($result->getCommittedBlocks())');
     $comblk = $result->getCommittedBlocks();
     $keys = array_keys($comblk);
     $this->assertEquals($blockId1, $keys[0], '$keys[0]');
     $this->assertEquals(256, $comblk[$keys[0]], '$comblk[$keys[0]]');
     $this->assertEquals($blockId3, $keys[1], '$keys[1]');
     $this->assertEquals(195, $comblk[$keys[1]], '$comblk[$keys[1]]');
     $this->assertNotNull($result->getUncommittedBlocks(), '$result->getUncommittedBlocks()');
     $this->assertEquals(0, count($result->getUncommittedBlocks()), 'count($result->getUncommittedBlocks())');
 }
    $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
    createContainerIfNotExists($blobRestProxy);
    echo "Using the '" . CONTAINERNAME . "' container and the '" . BLOCKBLOBNAME . "' blob.\n";
    echo "Using file '" . FILENAME . "'\n";
    if (!file_exists(FILENAME)) {
        echo "The '" . FILENAME . "' file does not exist. Exiting program.\n";
        exit;
    }
    $handle = fopen(FILENAME, "r");
    // Upload the blob using blocks.
    $counter = 1;
    $blockIds = array();
    while (!feof($handle)) {
        $blockId = str_pad($counter, PADLENGTH, "0", STR_PAD_LEFT);
        echo "Processing block {$blockId}.\n";
        $block = new Block();
        $block->setBlockId(base64_encode($blockId));
        $block->setType("Uncommitted");
        array_push($blockIds, $block);
        $data = fread($handle, BLOCKSIZE);
        // Upload the block.
        $blobRestProxy->createBlobBlock(CONTAINERNAME, BLOCKBLOBNAME, base64_encode($blockId), $data);
        $counter++;
    }
    // Done creating the blocks. Close the file and commit the blocks.
    fclose($handle);
    echo "Commiting the blocks.\n";
    $blobRestProxy->commitBlobBlocks(CONTAINERNAME, BLOCKBLOBNAME, $blockIds);
    echo "Done processing.\n";
} catch (ServiceException $serviceException) {
    // Handle exception based on error codes and messages.