public function createContainer($containerName, $options = null) { if (is_null($options)) { $options = new CreateContainerOptions(); $options->setPublicAccess('container'); } $this->restProxy->createContainer($containerName, $options); $this->_createdContainers[] = $containerName; }
/** * @covers WindowsAzure\Blob\Models\CreateContainerOptions::setPublicAccess */ public function testSetPublicAccessInvalidValueFail() { // Setup $properties = new CreateContainerOptions(); $expected = new \DateTime(); $this->setExpectedException(get_class(new InvalidArgumentTypeException(''))); // Test $properties->setPublicAccess($expected); }
/** * コンテナーを作成する * * IMPORTANT: * コンテナーの名前は、常に小文字にする必要があります。 * コンテナー名に大文字が含まれている場合や、コンテナーの名前付け規則の他の違反がある場合、400 エラー (無効な要求) が発生することがあります。 * コンテナーの名前付け規則については、https://msdn.microsoft.com/library/azure/dd135715.aspxをご覧ください。 * * @param type $containerName * @param type $publicAccess * @param type $metadata * @throws Exception */ public function createBlobContainer($containerName, $publicAccess = PublicAccessType::NONE, $metadata = null) { $createContainerOptions = new CreateContainerOptions(); $createContainerOptions->setPublicAccess($publicAccess); if ($metadata) { $createContainerOptions->setMetadata($metadata); } try { $this->blobRestProxy->createContainer($containerName, $createContainerOptions); } catch (Exception $e) { throw new Exception($e->getMessage(), $e->getCode()); } }
public function createContainer($container_name, $container_options = null) { if (empty($container_options)) { $container_options = new CreateContainerOptions(); $container_options->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS); } try { $this->getConnection()->createContainer($container_name, $container_options); } catch (ServiceException $ex) { throw new BackendException($ex->getMessage(), $ex->getCode()); } return true; }
public function createContainer($container, $publicAccess = PublicAccessType::BLOBS_ONLY) { if (!preg_match('/^(?!-)(?!.*--)[a-z0-9-]{3,63}+(?<!-)$/', $container)) { throw new HttpException(400, 'Invalid Container Name'); } $container_lists = $this->listContainers(''); foreach ($container_lists as $container_list) { if ($container_list->getName() == $container) { return false; } } $createContainerOptions = new CreateContainerOptions(); $createContainerOptions->setPublicAccess($publicAccess); try { $this->azureProxy->createContainer($container, $createContainerOptions); return true; } catch (ServiceException $e) { $code = $e->getCode(); $error_message = $e->getMessage(); throw new HttpException($code, $error_message); } }
protected function initialize() { $this->connectionString = 'DefaultEndpointsProtocol=' . ENDPOINT_PROTOCOL . ';AccountName=' . AZURE_BLOB_ACCOUNT_NAME . ';AccountKey=' . AZURE_BLOB_ACCOUNT_KEY; $this->containerName = sha1(HTTP_URL); $this->blobRestProxy = ServicesBuilder::getInstance()->createBlobService($this->connectionString); $objListContainersResult = $this->blobRestProxy->listContainers(); foreach ($objListContainersResult->getContainers() as $objContainer) { if ($objContainer->getName() == $this->containerName) { return; } } $createContainerOptions = new CreateContainerOptions(); $createContainerOptions->setPublicAccess(PublicAccessType::BLOBS_ONLY); try { // Create container. $this->blobRestProxy->createContainer($this->containerName, $createContainerOptions); } catch (ServiceException $e) { $code = $e->getCode(); $error_message = $e->getMessage(); echo $code . ": " . $error_message . "<br />"; } }
public function store($filename, $data, $options = array()) { $retrying = isset($options['retry']); $options['container'] = Config::get('storage.container', 'default'); try { $this->createBlockBlob($filename, $data, $options); return $this->getBlobService()->getUri() . '/' . $options['container'] . '/' . $filename; } catch (ServiceException $e) { // // Blob Container doesn't exists yet. Let's create it. // if ($e->getCode() == 404 && !$retrying) { $create_container_options = new CreateContainerOptions(); $create_container_options->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS); $create_container_options->addMetaData('created', date('U')); $this->getBlobService()->createContainer($options['container'], $create_container_options); // Container has been created. Let's retry storing blob file. $options['retry'] = true; return $this->store($filename, $data, $options); } else { throw $e; } } }
require_once 'vendor\\autoload.php'; use WindowsAzure\Common\ServicesBuilder; use WindowsAzure\Blob\Models\CreateContainerOptions; use WindowsAzure\Blob\Models\PublicAccessType; use WindowsAzure\Common\ServiceException; // https://azure.microsoft.com/en-us/documentation/articles/storage-php-how-to-use-blobs/ // Storage의 connection string 제공 $connectionString = "DefaultEndpointsProtocol=http;AccountName=vstechupdate;AccountKey=<어카운트키>"; // REST proxy 생성 $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString); $createContainerOptions = new CreateContainerOptions(); //setPublicAccess 접근 정책 설정 // CONTAINER_AND_BLOBS: // 전체 엑세스 권한 // // BLOBS_ONLY: // blob들에 대해서만 읽기 권한. $createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS); // 컨테이너 메타데이터 설정 $createContainerOptions->addMetaData("VSTechUp", "Visual Studio"); $createContainerOptions->addMetaData("Azure", "Cloud"); try { // 컨테이너 생성 $blobRestProxy->createContainer("phpcontainer", $createContainerOptions); } catch (ServiceException $e) { // 에러 핸들링 $code = $e->getCode(); $error_message = $e->getMessage(); echo $code . ": " . $error_message . "<br />"; }
/** * @covers WindowsAzure\Blob\BlobRestProxy::createContainer */ public function testCreateContainerWithMetadata() { $containerName = 'createcontainerwithmetadata' . $this->createSuffix(); $metadataName = 'Name'; $metadataValue = 'MyName'; $options = new CreateContainerOptions(); $options->addMetadata($metadataName, $metadataValue); $options->setPublicAccess('blob'); // Test $this->createContainer($containerName, $options); // Assert $options = new ListContainersOptions(); $options->setIncludeMetadata(true); $result = $this->restProxy->listContainers($options); $containers = $result->getContainers(); $metadata = $containers[0]->getMetadata(); $this->assertEquals($metadataValue, $metadata[$metadataName]); }
/** * Creates a public container * * @param string $containerName Name of the container to create * * @param BlobRestProxy $storageClient Reference of storage client to use * * @throws ServiceException */ public static function createPublicContainer($containerName, $storageClient = null) { $containerOptions = new CreateContainerOptions(); $containerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS); $blobRestProxy = $null; try { if ($storageClient) { $blobRestProxy = $storageClient; } else { $blobRestProxy = WindowsAzureStorageUtil::getStorageClient(); } $blobRestProxy->createContainer($containerName, $containerOptions); } catch (ServiceException $e) { throw $e; } }
/** * Создать произвольный контейнер * * @param type $name * @throws \WindowsAzure\Common\ServiceException */ public function createContainer($name) { $createContainerOptions = new CreateContainerOptions(); $createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS); try { // Создание контейнера. $this->blobRestProxy->createContainer($name, $createContainerOptions); } catch (ServiceException $e) { //@todo: ничего не делаем если вдруг контейнер уже создан //http://msdn.microsoft.com/ru-ru/library/windowsazure/dd179439.aspx if ($e->getCode() != 409) { //Значит другая ошибка и посему бросаем эксепшен throw $e; } } }
public static function getInterestingCreateContainerOptions() { $ret = array(); $options = new CreateContainerOptions(); array_push($ret, $options); $options = new CreateContainerOptions(); $options->setTimeout(10); array_push($ret, $options); $options = new CreateContainerOptions(); $options->setTimeout(-10); array_push($ret, $options); $options = new CreateContainerOptions(); $options->setPublicAccess('container'); array_push($ret, $options); $options = new CreateContainerOptions(); $options->setPublicAccess('blob'); array_push($ret, $options); $options = new CreateContainerOptions(); $metadata = array('foo' => 'bar', 'boo' => 'baz'); $options->setMetadata($metadata); array_push($ret, $options); return $ret; }
/** * Tries to create new container if it does not exist. * * @param string $container The container name. * * @return none */ private static function _tryCreateContainer($container) { try { $options = new CreateContainerOptions(); $options->setPublicAccess(PublicAccessType::BLOBS_ONLY); self::$_blobRestProxy->createContainer($container, $options); } catch (ServiceException $e) { if ($e->getCode() != 409) { print_r($e); exit; } } }
/** * create a container * @param type $dir name of container * @param type $access can be cb(CONTAINER_AND_BLOBS) or b (BLOBS_ONLY) * CONTAINER_AND_BLOBS: * Specifies full public read access for container and blob data. * proxys can enumerate blobs within the container via anonymous * request, but cannot enumerate containers within the storage account. * * BLOBS_ONLY: * Specifies public read access for blobs. Blob data within this * container can be read via anonymous request, but container data is not * available. proxys cannot enumerate blobs within the container via * anonymous request. * If this value is not specified in the request, container data is * private to the account owner. * @return boolean */ protected function createContainer($dir, $access = 'cb', array $metadata = array()) { $createContainerOptions = new CreateContainerOptions(); switch ($access) { case 'cb': $createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS); break; case 'b': $createContainerOptions->setPublicAccess(PublicAccessType::BLOBS_ONLY); break; default: $createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS); } foreach ($metadata as $key => $value) { $createContainerOptions->addMetaData($key, $value); } try { // Create container. $this->blobRestProxy->createContainer($dir, $createContainerOptions); return true; } catch (ServiceException $e) { // Handle exception based on error codes and messages. // Error codes and messages are here: // http://msdn.microsoft.com/it-it/library/windowsazure/dd179439.aspx $code = $e->getCode(); $error_message = $e->getMessage(); echo $code . ": " . $error_message . "<br />"; return false; } }
/** * @covers WindowsAzure\Blob\BlobRestProxy::createContainer * @covers WindowsAzure\Blob\BlobRestProxy::deleteContainer * @covers WindowsAzure\Blob\BlobRestProxy::getContainerACL * @covers WindowsAzure\Blob\BlobRestProxy::getContainerMetadata * @covers WindowsAzure\Blob\BlobRestProxy::getContainerProperties * @covers WindowsAzure\Blob\BlobRestProxy::listContainers */ public function testCreateContainerWithMetadataWorks() { // Act $opts = new CreateContainerOptions(); $opts->setPublicAccess('blob'); $opts->addMetadata('test', 'bar'); $opts->addMetadata('blah', 'bleah'); $this->restProxy->createContainer(self::$_creatable_container_2, $opts); $prop = $this->restProxy->getContainerMetadata(self::$_creatable_container_2); $prop2 = $this->restProxy->getContainerProperties(self::$_creatable_container_2); $acl = $this->restProxy->getContainerACL(self::$_creatable_container_2)->getContainerACL(); $opts = new ListContainersOptions(); $opts->setPrefix(self::$_creatable_container_2); $opts->setIncludeMetadata(true); $results2 = $this->restProxy->listContainers($opts); $this->restProxy->deleteContainer(self::$_creatable_container_2); // Assert $this->assertNotNull($prop, '$prop'); $this->assertNotNull($prop->getETag(), '$prop->getETag()'); $this->assertNotNull($prop->getLastModified(), '$prop->getLastModified()'); $this->assertNotNull($prop->getMetadata(), '$prop->getMetadata()'); $this->assertEquals(2, count($prop->getMetadata()), 'count($prop->getMetadata())'); $this->assertTrue(Utilities::arrayKeyExistsInsensitive('test', $prop->getMetadata()), 'Utilities::arrayKeyExistsInsensitive(\'test\', $prop->getMetadata())'); $this->assertTrue(!(array_search('bar', $prop->getMetadata()) === FALSE), '!(array_search(\'bar\', $prop->getMetadata()) === FALSE)'); $this->assertTrue(Utilities::arrayKeyExistsInsensitive('blah', $prop->getMetadata()), 'Utilities::arrayKeyExistsInsensitive(\'blah\', $prop->getMetadata())'); $this->assertTrue(!(array_search('bleah', $prop->getMetadata()) === FALSE), '!(array_search(\'bleah\', $prop->getMetadata()) === FALSE)'); $this->assertNotNull($prop2, '$prop2'); $this->assertNotNull($prop2->getETag(), '$prop2->getETag()'); $this->assertNotNull($prop2->getLastModified(), '$prop2->getLastModified()'); $this->assertNotNull($prop2->getMetadata(), '$prop2->getMetadata()'); $this->assertEquals(2, count($prop2->getMetadata()), 'count($prop2->getMetadata())'); $this->assertTrue(Utilities::arrayKeyExistsInsensitive('test', $prop2->getMetadata()), 'Utilities::arrayKeyExistsInsensitive(\'test\', $prop2->getMetadata())'); $this->assertTrue(!(array_search('bar', $prop2->getMetadata()) === FALSE), '!(array_search(\'bar\', $prop2->getMetadata()) === FALSE)'); $this->assertTrue(Utilities::arrayKeyExistsInsensitive('blah', $prop2->getMetadata()), 'Utilities::arrayKeyExistsInsensitive(\'blah\', $prop2->getMetadata())'); $this->assertTrue(!(array_search('bleah', $prop2->getMetadata()) === FALSE), '!(array_search(\'bleah\', $prop2->getMetadata()) === FALSE)'); $this->assertNotNull($results2, '$results2'); $this->assertEquals(1, count($results2->getContainers()), 'count($results2->getContainers())'); $container0 = $results2->getContainers(); $container0 = $container0[0]; // The capitalizaion gets changed. $this->assertTrue(Utilities::arrayKeyExistsInsensitive('test', $container0->getMetadata()), 'Utilities::arrayKeyExistsInsensitive(\'test\', $container0->getMetadata())'); $this->assertTrue(!(array_search('bar', $container0->getMetadata()) === FALSE), '!(array_search(\'bar\', $container0->getMetadata()) === FALSE)'); $this->assertTrue(Utilities::arrayKeyExistsInsensitive('blah', $container0->getMetadata()), 'Utilities::arrayKeyExistsInsensitive(\'blah\', $container0->getMetadata())'); $this->assertTrue(!(array_search('bleah', $container0->getMetadata()) === FALSE), '!(array_search(\'bleah\', $container0->getMetadata()) === FALSE)'); $this->assertNotNull($acl, '$acl'); }