所以,我已经下载了Azure SDK for php并启动了模拟器。一切都好。
然后我从Microsoft复制并粘贴代码,这样我就可以创建一个新的测试容器了。
require_once 'vendor\autoload.php';
use WindowsAzure\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions;
use MicrosoftAzure\Storage\Blob\Models\PublicAccessType;
use MicrosoftAzure\Storage\Common\ServiceException;
// Create blob REST proxy.
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService('UseDevelopmentStorage=true');
// OPTIONAL: Set public access policy and metadata.
// Create container options object.
$createContainerOptions = new CreateContainerOptions();
// Set public access policy. Possible values are
// PublicAccessType::CONTAINER_AND_BLOBS and PublicAccessType::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.
$createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS);
// Set container metadata.
$createContainerOptions->addMetaData("key1", "value1");
$createContainerOptions->addMetaData("key2", "value2");
try {
// Create container.
$blobRestProxy->createContainer("mycontainer", $createContainerOptions);
} catch (ServiceException $e) {
// Handle exception based on error codes and messages.
// Error codes and messages are here:
// http://msdn.microsoft.com/library/azure/dd179439.aspx
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code . ": " . $error_message . "<br />";
}
当我运行此代码时,我收到一条很好的错误消息。
404:失败:
代码:404
值:指定的资源不存在。
这有什么问题?我的想法已经不多了。首先,我有一个稍微不同的代码也没有用,所以现在我尝试直接从MS使用这个样本,没有运气。
CLI显示模拟器正在运行,并且端点也正确。
答案 0 :(得分:1)
我使用Fiddler捕获SDK生成的http请求,网址路径为/testcontainer?restype=container
。根据Rest API指南https://msdn.microsoft.com/en-us/library/azure/dd179468.aspx,网址路径应为/devstoreaccount1/mycontainer?restype=container
。
目前,有一种解决方法可以在本地模拟器上使用Azure存储进行开发。每当我们使用容器名称时,我们都可以添加本地帐户名devstoreaccount1
,例如
$blobRestProxy->createContainer("devstoreaccount1/testcontainer");
$blobRestProxy->createBlockBlob("devstoreaccount1/testcontainer", "testblob", "test string");
$blobRestProxy->listBlobs("devstoreaccount1/testcontainer");
如有任何疑问,请随时告诉我。