m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

Azure Strage File ApiをPHPでBLOBを取得

仕事がやっと

落ち着いた感じがする。
特別忙しいというわけではないがタイミングなどの問題もあってブログを書くことを忘れてしまっているのはマズイ
忘れていることもあるので勉強を再開してみる。

Azure Strage File

簡単にいうとAWSのS3と思う、厳密には違うと思うけど一部のデータを保存したり取得したりするのでそのレベルの認識で覚えておく
今回は設定して登録したデータをphpで取得することを行う。

事前準備

Windows Azureへアカウントを作成しておく
Linuxの環境(vagrantなどを作成しておく)

Azureの設定

ストレージアカウントを選択する

f:id:m_shige1979:20170717081218p:plain

追加ボタンを押下

f:id:m_shige1979:20170717081231p:plain

アカウント名と他の設定を行い、作成ボタンを押下

f:id:m_shige1979:20170717081242p:plain
f:id:m_shige1979:20170717081254p:plain

作成されたことを確認し、選択する

f:id:m_shige1979:20170717081305p:plain

コンテナを追加する

f:id:m_shige1979:20170717081318p:plain

コンテナ名を設定してOKを押下

f:id:m_shige1979:20170717081330p:plain

アクセスキーをコピーする

f:id:m_shige1979:20170717081341p:plain

準備

php
sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
sudo yum install --enablerepo=remi,remi-php70 php php-devel php-mbstring php-pdo php-gd php-xml
compoer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
compoer.json
{
  "require": {
    "microsoft/windowsazure": "^0.4"
  }
}

設定情報

setting.php
<?php

$connectionString = "DefaultEndpointsProtocol=https;AccountName=mshige1979sample1;AccountKey=事前にコピーしたアクセスキー";

ファイルを追加

sample_add.php
<?php
require_once 'vendor/autoload.php';
require_once 'setting.php';

use WindowsAzure\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Common\ServiceException;

// 接続文字列を使用して、Blobサービスのインスタンスを作成する
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);

// 取得処理
try {
    // ファイル名
    $date = "testdata_" . date("YmdHis") . ".txt";
    echo $date . "\n";

    // データの中身
    $data = md5(uniqid());;
    echo $data . "\n";

    // 追加処理
    $blobRestProxy->createBlockBlob("test01", $date, $data);

} catch(ServiceException $e) {
    // 例外処理
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."\n";
}

$ php sample_add.php
testdata_20170716233116.txt
08a8e1964d2c0dadc558bd4f54b3de5a
$ php sample_add.php
testdata_20170716233127.txt
6ff9937a10cbc58bee43367299f49a80
$ 

一覧取得

sample1.php
<?php
require_once 'vendor/autoload.php';
require_once 'setting.php';

use WindowsAzure\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Common\ServiceException;

// 接続文字列を使用して、Blobサービスのインスタンスを作成する
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);

// 取得処理
try {
    // コンテナのオブジェクトを取得
    $blob_list = $blobRestProxy->listBlobs("test01");
    $blobs = $blob_list->getBlobs();

    // 一覧を出力
    foreach ($blobs as $blob) {
        echo $blob->getName().": ".$blob->getUrl()."\n";
    }
} catch(ServiceException $e) {
    // 例外処理
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."\n";
}

$ php sample_list.php
testdata_20170716233116.txt: https://mshige1979sample1.blob.core.windows.net/test01/testdata_20170716233116.txt
testdata_20170716233127.txt: https://mshige1979sample1.blob.core.windows.net/test01/testdata_20170716233127.txt
$

読込処理

sample_get.php
<?php
require_once 'vendor/autoload.php';
require_once 'setting.php';

use WindowsAzure\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Common\ServiceException;

// 接続文字列を使用して、Blobサービスのインスタンスを作成する
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);

// 取得処理
try {
    // ファイル名
    $date = "testdata_20170716233116.txt";

    // 取得処理
    $blob = $blobRestProxy->getBlob("test01", $date);
    echo stream_get_contents($blob->getContentStream()) . "\n";

} catch(ServiceException $e) {
    // 例外処理
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."\n";
}

$ php sample_get.php
08a8e1964d2c0dadc558bd4f54b3de5a
$

まとめ

AWSとAzureはそれぞれ勝手が違う。
このレベルのことは基本として理解しておきたい、やっていないからわかりませんということは仕方がないにしても
ITに携わるものとして新しい技術に触れ続けていくことは重要と思うので…