/**
* 本地文件移动到OSS服务器
* @param $sPath
* @param string $sFileName 文件名称 如有二级目录以‘/’分割 'images/20180606/1'
* @return string|void
*/
function fileMoveOSS($sPath, $sFileName = '')
{
// Initialize Variable
$sBucket = 'tribute'; // 储存空间名称
$sAccessKeyId = OSS_ACCESS_KEY;
$sAccessSecret = OSS_ACCESS_SECRET;
$sEndpoint = 'https://oss-cn-hangzhou.aliyuncs.com/';
$sFileName = empty($sFileName) ? '' : $sFileName; // 文件名称
try{
$ossClient = new OssClient($sAccessKeyId, $sAccessSecret, $sEndpoint);
$ossClient->uploadFile($sBucket, $sFileName, HOST_PATH . $sPath);
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
// 删除本地存储
unlink($sPath);
return 'https://img.nmsmt.cn/' . $sFileName;
}// END fileMoveOSS
/**
* 删除图片或文件信息 这里有个坑就签名算法这块
* 这个删除是单一文件删除,估计批量删除可以就没有问题了
* 单一图片删除使用delete 所以传递的内容为空,就不要使用md5加密
* 然后删除成功了,OSS服务不返回任务内容
* 地址在算签名的时候一定要加个bucketname
* @param $sPath
* @return array
*/
function deleteFileOSS( $sPath )
{
if (strpos($sPath, UPLOAD_HOST) === false)
{
$sPath = trim($sPath,'/');
$sUrl = UPLOAD_HOST.$sPath;
$sPath = '/tribute/'.$sPath; //一定要加上 bucketname 官方没有说明
}
else
{
$sUrl = $sPath;
$sPath = str_replace(UPLOAD_HOST, '', $sPath);
$sPath = trim($sPath, '/');
$sPath = '/tribute/'.$sPath;
}
$sExpiration = gmdate("D, d M Y H:i:s").' GMT'; // 一定要使用 http 1.1 标准时间格式
// 签名算法不多说官网的例子没有PHP版的 本人这个可以使用验证通过,可以正常删除文件
$sSign = base64_encode(hash_hmac('sha1',"DELETE\n\ntext/html\n".$sExpiration."\n".$sPath, OSS_ACCESS_SECRET, true));
// 传递头这里也是坑 上面使用了 text/html 在协议头里还得加上,要不然会提示出错。
$headers = [
'Authorization: OSS '.OSS_ACCESS_KEY.':'.$sSign,
'Date:'.$sExpiration, // 时间也得带上
'Content-Type: text/html', // 传递类型要与上面签名算法一直
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_exec($ch);
//OSS删除文件不返回结果,没有返回结果就表示删除成功,反之会有删除出错信息
}// END deleteFileOSS
/**
* Uploads a local file
*
* @param string $bucket bucket name
* @param string $object object name
* @param string $file local file path
* @param array $options
* @return null
* @throws OssException
*/
public function uploadFile($bucket, $object, $file, $options = NULL)
{
$this->precheckCommon($bucket, $object, $options);
OssUtil::throwOssExceptionWithMessageIfEmpty($file, "file path is invalid");
$file = OssUtil::encodePath($file);
if (!file_exists($file)) {
throw new OssException($file . " file does not exist");
}
$options[self::OSS_FILE_UPLOAD] = $file;
$file_size = filesize($options[self::OSS_FILE_UPLOAD]);
$is_check_md5 = $this->isCheckMD5($options);
if ($is_check_md5) {
$content_md5 = base64_encode(md5_file($options[self::OSS_FILE_UPLOAD], true));
$options[self::OSS_CONTENT_MD5] = $content_md5;
}
if (!isset($options[self::OSS_CONTENT_TYPE])) {
$options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object, $file);
}
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_OBJECT] = $object;
$options[self::OSS_CONTENT_LENGTH] = $file_size;
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* 通过PostObject方式将图片上传到OOS服务器
* @param string $sPath 上传文件路径
* @return array
*/
function uploadOSS($sPath = 'images/')
{
// Initialize Variable
$nDate = date('Y-m-d', time() + 86400); // 最晚上传时间 延迟一天
$nTime = date('H:i:s');
$sBucket = 'tribute'; // 存储空间名称
$sExpiration = "{$nDate}T{$nTime}.000Z"; // 拼接 UTC 通用标准时
$sFilePath = $sPath . time() . rand(10000, 99999); // 文件路径
$arrOptions = [
'expiration' => $sExpiration,
'conditions' =>
[
['bucket' => $sBucket],
['content-length-range', 0, 1048575]
]
];
/**
* 规定该次Post请求中表单域的合法值
* 创建一个 UTF-8 编码的 policy
* 将 policy 进行 base64 编码 其值即为 policy 表单域该填入的值,将该值作为将要签名的字符串
* 使用 AccessKeySecret 对要签名的字符串进行签名 签名方法与Head中签名的计算方法相同(将要签名的字符串替换为 policy 即可) 请参见在Header中包含签名
*/
$sPolicy = base64_encode(stripslashes(json_encode($arrOptions)));
$sSign = base64_encode(hash_hmac('sha1',$sPolicy,OSS_ACCESS_SECRET, true));
return ['policy' => $sPolicy, 'sign' => $sSign, 'file_path' => $sFilePath];
}// END uploadOSS