示例#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;
 }
示例#2
0
 /**
  * @covers MicrosoftAzure\Storage\Blob\Models\Block::setType
  * @covers MicrosoftAzure\Storage\Blob\Models\Block::getType
  */
 public function testSetType()
 {
     // Setup
     $block = new Block();
     $expected = 'BlockType';
     // Test
     $block->setType($expected);
     // Assert
     $this->assertEquals($expected, $block->getType());
 }
示例#3
0
 /**
  * @covers MicrosoftAzure\Storage\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 MicrosoftAzure\Storage\Blob\BlobRestProxy::commitBlobBlocks
  * @covers MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlobBlock
  * @covers MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlockBlob
  * @covers MicrosoftAzure\Storage\Blob\BlobRestProxy::listBlobBlocks
  */
 public function testCommitBlobBlocksWithArrayWorks()
 {
     // Act
     $container = self::$_test_container_for_blobs;
     $blob = 'test14a';
     $blockId1 = base64_encode('1fedcba');
     $blockId2 = base64_encode('2abcdef');
     $blockId3 = base64_encode('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())');
 }
 /**
  * @covers MicrosoftAzure\Storage\Blob\BlobRestProxy::commitBlobBlocks
  * @covers MicrosoftAzure\Storage\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());
 }
示例#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);
                 }
             }
             if (!empty($body)) {
                 $block = new Block();
                 $block->setBlockId(base64_encode(str_pad($counter++, 6, '0', STR_PAD_LEFT)));
                 $block->setType('Uncommitted');
                 array_push($blockIds, $block);
                 $this->createBlobBlock($container, $blob, $block->getBlockId(), $body);
             }
         }
         $response = $this->commitBlobBlocks($container, $blob, $blockIds, $options);
     }
     return CopyBlobResult::create(HttpFormatter::formatHeaders($response->getHeaders()));
 }