コード例 #1
0
 public function copyToBlob(BlobFile $objFile)
 {
     if ($this->exists_blob($objFile)) {
         $this->outLog('exists_blob');
         $objBlobMetadataResult = $this->blobRestProxy->getBlobMetadata($this->containerName, 'save_image/' . $objFile->file_name);
         $arrMetadata = $objBlobMetadataResult->getMetadata();
         if ($arrMetadata['mtime'] == $objFile->getMtime()) {
             $this->outLog('equals');
             return;
         } elseif ($arrMetadata['mtime'] > $objFile->getMtime()) {
             $result = file_put_contents(IMAGE_SAVE_REALDIR . $objFile->file_name, ENDPOINT_PROTOCOL . '://' . AZURE_BLOB_ACCOUNT_NAME . '.blob.core.windows.net/' . $this->containerName . '/save_image/' . $objFile->file_name);
             $this->outLog($result);
         } else {
         }
     } else {
         try {
             $mimeType = $objFile->getMimeType();
             $this->outLog('create: ' . $objFile->file_name);
             $this->outLog('mimeType: ' . $mimeType);
             $createBlobOptions = new CreateBlobOptions();
             $createBlobOptions->setBlobContentType($mimeType);
             $createBlobOptions->setMetadata(array('mtime' => $objFile->getMtime()));
             $this->blobRestProxy->createBlockBlob($this->containerName, 'save_image/' . $objFile->file_name, $objFile->getResources(), $createBlobOptions);
         } catch (ServiceException $e) {
             $code = $e->getCode();
             $error_message = $e->getMessage();
             $this->outLog($code . ": " . $error_message . "<br />");
         }
     }
 }
コード例 #2
0
 public function pn_blob_cache_set($key, $data, $expire, $headers)
 {
     $options = new CreateBlobOptions();
     //Set metadata and content-type for blob if header array was provided
     if ($this->prepare_headers($headers)) {
         if (isset($this->content_type)) {
             $options->setBlobContentType($this->content_type);
         }
         $options->setCacheControl("max-age=" . $expire);
         $options->setMetadata(array('Projectnamicacheduration' => $expire, 'Headers' => $this->headers_as_json));
     } else {
         $options->setMetadata(array('Projectnamicacheduration' => $expire));
     }
     try {
         //Upload blob
         $this->blob_service->createBlockBlob($this->container, $key, $data, $options);
         return true;
     } catch (ServiceException $e) {
         $this->pn_handle_exception($e);
     }
 }
コード例 #3
0
 /**
  * @covers WindowsAzure\Blob\BlobRestProxy::getBlob
  * @covers WindowsAzure\Blob\BlobRestProxy::_addOptionalRangeHeader
  * @covers WindowsAzure\Blob\Models\GetBlobResult::create
  */
 public function testGetBlobGarbage()
 {
     // Setup
     $name = 'getblobwithgarbage' . $this->createSuffix();
     $blob = 'myblob';
     $metadata = array('m1' => 'v1', 'm2' => 'v2');
     $contentType = 'text/plain; charset=UTF-8';
     $contentStream = chr(0);
     $this->createContainer($name);
     $options = new CreateBlobOptions();
     $options->setContentType($contentType);
     $options->setMetadata($metadata);
     $this->restProxy->createBlockBlob($name, $blob, $contentStream, $options);
     // Test
     $result = $this->restProxy->getBlob($name, $blob);
     // Assert
     $this->assertEquals(BlobType::BLOCK_BLOB, $result->getProperties()->getBlobType());
     $this->assertEquals($metadata, $result->getMetadata());
     $this->assertEquals($contentStream, stream_get_contents($result->getContentStream()));
 }
コード例 #4
0
 /**
  * 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;
     }
 }
コード例 #5
0
 public static function getInterestingCreateBlobOptions()
 {
     $ret = array();
     $options = new CreateBlobOptions();
     array_push($ret, $options);
     $options = new CreateBlobOptions();
     $options->setTimeout(10);
     array_push($ret, $options);
     $options = new CreateBlobOptions();
     $options->setTimeout(-10);
     array_push($ret, $options);
     $options = new CreateBlobOptions();
     $metadata = array('foo' => 'bar', 'foo2' => 'bar2', 'foo3' => 'bar3');
     $options->setMetadata($metadata);
     $options->setTimeout(10);
     array_push($ret, $options);
     $options = new CreateBlobOptions();
     $metadata = array('foo' => 'bar');
     $options->setMetadata($metadata);
     $options->setTimeout(-10);
     array_push($ret, $options);
     return $ret;
 }
コード例 #6
0
 /**
  * @covers WindowsAzure\Blob\Models\CreateBlobOptions::getMetadata
  */
 public function testGetMetadata()
 {
     // Setup
     $container = new CreateBlobOptions();
     $expected = array('key1' => 'value1', 'key2' => 'value2');
     $container->setMetadata($expected);
     // Test
     $actual = $container->getMetadata();
     // Assert
     $this->assertEquals($expected, $actual);
 }
コード例 #7
0
 /**
  * @covers WindowsAzure\Blob\BlobRestProxy::createPageBlob
  * @covers WindowsAzure\Blob\BlobRestProxy::getBlobMetadata
  */
 public function testGetBlobMetadataWorks()
 {
     // Act
     $container = self::$_test_container_for_blobs;
     $blob = 'test';
     $opts = new CreateBlobOptions();
     $metadata = $opts->getMetadata();
     $metadata['test'] = 'bar';
     $metadata['blah'] = 'bleah';
     $opts->setMetadata($metadata);
     $this->restProxy->createPageBlob($container, $blob, 4096, $opts);
     $props = $this->restProxy->getBlobMetadata($container, $blob);
     // Assert
     $this->assertNotNull($props, '$props');
     $this->assertNotNull($props->getETag(), '$props->getETag()');
     $this->assertNotNull($props->getMetadata(), '$props->getMetadata()');
     $this->assertEquals(2, count($props->getMetadata()), 'count($props->getMetadata())');
     $this->assertTrue(Utilities::arrayKeyExistsInsensitive('test', $props->getMetadata()), 'Utilities::arrayKeyExistsInsensitive(\'test\', $props->getMetadata())');
     $this->assertTrue(!(array_search('bar', $props->getMetadata()) === FALSE), '!(array_search(\'bar\', $props->getMetadata()) === FALSE)');
     $this->assertTrue(Utilities::arrayKeyExistsInsensitive('blah', $props->getMetadata()), 'Utilities::arrayKeyExistsInsensitive(\'blah\', $props->getMetadata())');
     $this->assertTrue(!(array_search('bleah', $props->getMetadata()) === FALSE), '!(array_search(\'bleah\', $props->getMetadata()) === FALSE)');
     $this->assertNotNull($props->getLastModified(), '$props->getLastModified()');
 }
コード例 #8
0
 /**
  * Upload the given file to an azure storage container as a block blob.
  * Block blobs let us upload large blobs efficiently. Block blobs are comprised of blocks,
  * each of which is identified by a block ID. This allows create (or modify) a block blob
  * by writing a set of blocks and committing them by their block IDs.
  * If we are writing a block blob that is no more than 64 MB in size, you can upload it
  * in its entirety with a single write operation.
  * 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      Container name
  *
  * @param string            $blobName           Blob name
  *
  * @param string            $localFileName      Path to local file to be uploaded
  *
  * @param string            $blobContentType    Content type of the blob
  *
  * @param array             $metadata           Array of metadata
  *
  * @return void
  *
  * @throws ServiceException
  */
 public static function putBlockBlob($containerName, $blobName, $localFileName, $blobContentType = null, $metadata = array())
 {
     $copyBlobResult = null;
     // Open file
     $handle = fopen($localFileName, 'r');
     if ($handle === false) {
         throw new Exception('Could not open the local file ' . localFileName);
     }
     $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 {
             // 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++) {
                 $blocks[$i] = new Block();
                 $blocks[$i]->setBlockId(self::_generateBlockId($i));
                 $blocks[$i]->setType(BlobBlockType::LATEST_TYPE);
             }
             // Upload blocks
             for ($i = 0; $i < $numberOfBlocks; $i++) {
                 // 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, $blocks[$i]->getBlockId(), $fileContents);
                 // Dispose file contents
                 $fileContents = null;
                 unset($fileContents);
             }
             // 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);
         }
     } catch (ServiceException $exception) {
         if (!$handle) {
             fclose($handle);
         }
         throw $exception;
     }
 }