LoginSignup
0
0

More than 3 years have passed since last update.

AzureBlobストレージのファイルをphpで削除する

Posted at

本当はディレクトリ指定して下層にあるやつ丸ごと削除したかったが
仮想ディレクトリだから出来ないらしいので
対象のディレクトリ内のファイルリストを取得→1ファイルごとに削除処理するしかないらしい
とてもめんどくさい

public function deleteBlob(){
    $listBlobsOptions = new ListBlobsOptions();
    $listBlobsOptions->setPrefix("対象のディレクトリ名");
    $fileList = $this->blobClient->listBlobs("コンテナ名", $listBlobsOptions);
    foreach ($fileList->getBlobs() as $val){
        $this->blobClient->deleteBlob("コンテナ名",$val->getName());
    }
    return ;
}

解説

ListBlobOptionsでプレフィックスを指定する

コンテナ以下の下層ディレクトリにあたる部分を指定

container/folder1/folder2/test1.txt
container/folder1/folder2/test2.txt
container/folder1/folder2/test3.txt
container/folder1/folder2/test4.txt

↑こういう時、folder2以下にあるテキストファイルを全部削除したい場合は

$listBlobsOptions->setPrefix("folder1/folder2");

と指定しましょう。

指定したPrefix内のファイル一覧を取得

listBlobsで対象のコンテナに対してBlobsOptionを付与
fileListに一覧データが入ります。

1個ずつ取り出して削除処理を実行する

fileList内にBlobsメンバがあるのでfileList->Blobsをforeachでブン回したら
privateだからアクセス出来ないよ!って言われたので、
ソースを読んでみたらgetBlobsってpublicメソッドがあったからそれを使う。
同様にBlob->nameもprivateだったのでgetNameメソッドで取得。

おわり。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0