对象存储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
标签:return,ossName,OSS,示例,ossClient,ossPath,resList,PHP,目录 来源: https://www.cnblogs.com/brooklyndawndaisy-20171125/p/14945618.html