1. 程式人生 > 其它 >物件儲存OSS使用PHP SDK建立目錄示例

物件儲存OSS使用PHP SDK建立目錄示例

/**
* @info 檢查目錄是否存在,若不存在則建立目錄
* @param object $ossClient
* @param string $ossFolder
* @param string $filePathWithName
* @return bool
* @author Daisy
* @date 2021/6/28 16:42
*/
function checkDirExists($ossClient, $ossFolder, $filePath)
{
global $ossBucket;
$ossName = strstr($filePath, $ossFolder);
$ossPath = substr($ossName, 0, strpos($ossName, basename($ossName)) - 1);
$options = ['prefix' => $ossPath];

$resList = $ossClient->listObjects($ossBucket, $options);
if (is_object($resList['content'])) {
$fileExists = $resList['content']->getPrefixList();
if ($fileExists == $ossPath) {
return true;//目錄存在,無需建立
}
}

try {
$ossClient->createObjectDir($ossBucket, $ossPath);
} catch (\Exception $ex) {
writeLog('建立目錄失敗:' . $ossPath);
return false;
}
return true;
}

參考資料1:
https://help.aliyun.com/document_detail/88510.html?spm=a2c4g.11186623.6.1039.6370531cLLHu55
參考資料2:http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/pdf/oss-sdk-intl-zh-2017-06-28.pdf