对象存储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
上一篇:Comparator.comparing排序使用示例


下一篇:45. 为被装饰函数保存元数据