You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
254 lines
9.2 KiB
PHP
254 lines
9.2 KiB
PHP
<?php
|
|
|
|
class OssClient {
|
|
|
|
private static function getLog() {
|
|
static $log;
|
|
if (!empty($log) && ($log instanceof ZcLog)) {
|
|
return $log;
|
|
}
|
|
$log = Zc::getLog('tool/oss_client/index');
|
|
return $log;
|
|
}
|
|
|
|
public static function uploadImmutableObjectByContent($bucket, &$objectPath, $data, $idc = null) {
|
|
$idc = $idc ?: self::getCurrentIdc();
|
|
$objectPath = self::clearObjectPathSuffix($objectPath);
|
|
|
|
if ($idc == OssConst::idcDcloud) {
|
|
$objectPath = OssConst::getIdcDcloudOssPathPrefix() . $objectPath;
|
|
$ret = self::dcloudUploadObject($objectPath, $data);
|
|
} else {
|
|
$ret = self::aliyunUploadObject($bucket, $objectPath, $data);
|
|
}
|
|
$objectPath = self::addOssPathSuffix($objectPath, $idc);
|
|
|
|
if (CommonTool::isSuccessRet($ret)) {
|
|
$ret['objectPath'] = $objectPath;
|
|
$ret['objectUrl'] = OssTool::getPubOssUrlByOssPath($objectPath, null, $bucket);
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
protected static function isNeedRetryGetImmutableObject($bucket, $objectPath) {
|
|
if ($bucket == Zc::C('fc-pub.bucket')) {
|
|
return false;
|
|
}
|
|
if ($bucket == Zc::C('node-video.bucket')) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static function getImmutableObjectByContent($bucket, $objectPath) {
|
|
$needRetry = self::isNeedRetryGetImmutableObject($bucket, $objectPath);
|
|
$objectPath = str_replace(Zc::C('honor-pub.domain'), '', $objectPath);
|
|
$objectPath = str_replace(Zc::C('honor-pub.internal.domain'), '', $objectPath);
|
|
if (preg_match('#/open/common/get_dcloud_oss_content\?objectKey=([^&]*)#', $objectPath, $matches)) {
|
|
$objectPath = $matches[1];
|
|
}
|
|
|
|
$idc = self::getIdcByOssUrl($objectPath);
|
|
$ret = self::getObjectContentByIdc($bucket, $objectPath, $idc);
|
|
if (CommonTool::isFailRet($ret) && $needRetry) {
|
|
$ret = self::getObjectContentByIdc($bucket, $objectPath, $idc == OssConst::idcAliyun ? OssConst::idcDcloud : OssConst::idcAliyun);
|
|
}
|
|
|
|
if (CommonTool::isSuccessRet($ret)) {
|
|
return $ret;
|
|
}
|
|
|
|
return CommonTool::failResult('请求失败');
|
|
}
|
|
|
|
public static function deleteImmutableObject($bucket, $objectPath) {
|
|
$objectPath = self::clearObjectPathSuffix($objectPath);
|
|
$ret = self::aliyunDeleteObject($bucket, $objectPath);
|
|
if (AppConst::isOpenDcloudOss() && self::isNeedRetryGetImmutableObject($bucket, $objectPath)) {
|
|
$ret = self::dcloudDeleteObject($objectPath);
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public static function getIdcByOssUrl($ossUrl) {
|
|
if (stripos($ossUrl, OssConst::idcDcloudOssPathSuffix) !== false) {
|
|
return OssConst::idcDcloud;
|
|
}
|
|
|
|
return OssConst::idcAliyun;
|
|
}
|
|
|
|
/**
|
|
* @return NULL|ALIOSS
|
|
*/
|
|
private static function getAliyunOssService() {
|
|
static $ossService = null;
|
|
if (!empty($ossService)) {
|
|
return $ossService;
|
|
}
|
|
Zc::import('@.vendor.oss_php_sdk_20140625.sdk', '', '.class.php');
|
|
$ossService = new ALIOSS(Zc::C('aliyun.access_key_id'), Zc::C('aliyun.access_key_secret'), Zc::C('aliyun.oss_endpoint'));
|
|
$ossService->set_debug_mode(false);
|
|
$ossService->set_enable_domain_style(true);
|
|
$ossService->set_timeout(120);
|
|
$ossService->set_connect_timeout(10);
|
|
return $ossService;
|
|
}
|
|
|
|
/**
|
|
* @return NULL|DCloudOss
|
|
*/
|
|
private static function getDcloudOssService() {
|
|
static $ossService = null;
|
|
if (!empty($ossService)) {
|
|
return $ossService;
|
|
}
|
|
Zc::import('@.vendor.dcloud_oss_php_sdk.sdk', '', '.class.php');
|
|
$ossService = new DCloudOss(Zc::c('clientId'), Zc::c('clientSecret'), 120);
|
|
return $ossService;
|
|
}
|
|
|
|
protected static function aliyunUploadObject($bucket, $objectPath, $data) {
|
|
$ossService = self::getAliyunOssService();
|
|
$options = array (
|
|
ALIOSS::OSS_CONTENT => $data,
|
|
ALIOSS::OSS_HEADERS => array (
|
|
'Expires' => '2099-10-01 23:23:59'
|
|
)
|
|
);
|
|
$md5 = strtolower(md5($data));
|
|
|
|
for ($tt = 1; $tt <= 3; $tt++) {
|
|
try {
|
|
$resp = $ossService->upload_file_by_content($bucket, $objectPath, $options);
|
|
$resp = ZcArrayHelper::objectToArray($resp);
|
|
|
|
if (($resp['status'] == 200) && (stripos($resp['header']['etag'], $md5) !== false)) {
|
|
CommonTool::markSuccess($resp);
|
|
break;
|
|
} else {
|
|
self::getLog()->error("upload fail - $tt - $objectPath: " . $resp);
|
|
CommonTool::markFail($resp);
|
|
}
|
|
} catch (Exception $ex) {
|
|
self::getLog()->error("upload fail - $tt - $objectPath: " . $ex);
|
|
$resp = CommonTool::failResult('上传失败,原因:' . $ex->getMessage());
|
|
}
|
|
}
|
|
return $resp;
|
|
}
|
|
|
|
protected static function dcloudUploadObject($objectPath, $data) {
|
|
$server = self::getDcloudOssService();
|
|
for ($tt = 1; $tt <= 3; $tt++) {
|
|
try {
|
|
$ret = $server->uploadObject($objectPath, $data);
|
|
if (CommonTool::isSuccessRet($ret)) {
|
|
break;
|
|
}
|
|
} catch (Exception $ex) {
|
|
self::getLog()->error("upload fail - $tt - $objectPath: " . $ex);
|
|
$ret = CommonTool::failResult('上传失败,原因:' . $ex->getMessage());
|
|
}
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
protected static function aliyunGetObjectContent($bucket, $objectPath) {
|
|
$ossService = self::getAliyunOssService();
|
|
for ($tt = 1; $tt <= 3; $tt++) {
|
|
try {
|
|
$resp = $ossService->get_object($bucket, $objectPath);
|
|
$resp = ZcArrayHelper::objectToArray($resp);
|
|
if ($resp['status'] == 200) {
|
|
CommonTool::markSuccess($resp);
|
|
break;
|
|
} else {
|
|
self::getLog()->error("get fail - $tt - $objectPath: " . print_r($resp, true));
|
|
CommonTool::markFail($resp);
|
|
}
|
|
} catch (Exception $ex) {
|
|
self::getLog()->error("get fail - $tt - $objectPath: " . $ex->getMessage());
|
|
$resp = CommonTool::failResult('获取失败,原因:' . $ex->getMessage());
|
|
}
|
|
}
|
|
return $resp;
|
|
}
|
|
|
|
public static function dcloudGetObjectContent($objectKey) {
|
|
$server = self::getDcloudOssService();
|
|
return $server->getObjectContent($objectKey);
|
|
}
|
|
|
|
protected static function getObjectContentByIdc($bucket, $objectPath, $idc) {
|
|
$objectPath = self::clearObjectPathSuffix($objectPath);
|
|
if ($idc == OssConst::idcDcloud) {
|
|
if (!AppConst::isDuoDuoCloud()) {
|
|
$realUrl = OssTool::getPubOssUrlByOssPath($objectPath, $idc, $bucket);
|
|
$data = file_get_contents($realUrl);
|
|
if ($data) {
|
|
return CommonTool::successResult('body', $data);
|
|
}
|
|
} else {
|
|
return self::dcloudGetObjectContent(OssTool::getRealDcloudOssPath($objectPath));
|
|
}
|
|
} else {
|
|
return self::aliyunGetObjectContent($bucket, $objectPath);
|
|
}
|
|
|
|
return CommonTool::failResult('获取失败');
|
|
}
|
|
|
|
protected static function aliyunDeleteObject($bucket, $objectPath) {
|
|
$ossService = self::getAliyunOssService();
|
|
for ($tt = 1; $tt <= 3; $tt++) {
|
|
try {
|
|
$resp = $ossService->delete_object($bucket, $objectPath);
|
|
$resp = ZcArrayHelper::objectToArray($resp);
|
|
if ($resp['status'] == 204) {
|
|
CommonTool::markSuccess($resp);
|
|
break;
|
|
} else {
|
|
self::getLog()->error("get fail - $tt - $objectPath: " . $resp);
|
|
CommonTool::markFail($resp);
|
|
}
|
|
} catch (Exception $ex) {
|
|
self::getLog()->error("get fail - $tt - $objectPath: " . $ex);
|
|
$resp = CommonTool::failResult('获取失败,原因:' . $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
return $resp;
|
|
}
|
|
|
|
protected static function dcloudDeleteObject($objectKey) {
|
|
$objectKey = OssTool::getRealDcloudOssPath($objectKey);
|
|
$server = self::getDcloudOssService();
|
|
return $server->deleteObject($objectKey);
|
|
}
|
|
|
|
private static function addOssPathSuffix($ossPath, $idc) {
|
|
if ($idc === OssConst::idcDcloud) {
|
|
if (stripos($ossPath, OssConst::idcDcloudOssPathSuffix) === false) {
|
|
$ossPath = $ossPath . OssConst::idcDcloudOssPathSuffix;
|
|
}
|
|
}
|
|
|
|
return $ossPath;
|
|
}
|
|
|
|
private static function clearObjectPathSuffix($objectPath) {
|
|
return str_ireplace(OssConst::idcDcloudOssPathSuffix, '', $objectPath);
|
|
}
|
|
|
|
private static function getCurrentIdc() {
|
|
if (!AppConst::isOpenDcloudOss()) {
|
|
return OssConst::idcAliyun;
|
|
}
|
|
|
|
if (AppConst::isDuoDuoCloud()) {
|
|
return OssConst::idcDcloud;
|
|
}
|
|
return OssConst::idcAliyun;
|
|
}
|
|
} |