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.

103 lines
3.6 KiB
PHTML

2 years ago
<?php
namespace Home\Controller;
use OSS\OssClient;
use OSS\Core\OSsException;
class OssController extends HomeController {
/**
*上传到OSS
*/
public function upload_game_pak_oss($return_data=null){
/**
* 根据Config配置得到一个OssClient实例
*/
try {
Vendor('OSS.autoload');
$ossClient = new \OSS\OssClient(C("oss_storage.accesskeyid"), C("oss_storage.accesskeysecr"), C("oss_storage.domain"));
} catch (OssException $e) {
$this->error($e->getMessage());
}
$bucket = C('oss_storage.bucket');
$oss_file_path ="icon/". $return_data["savename"];
$avatar = $return_data["path"];
try {
$this->multiuploadFile($ossClient,$bucket,$oss_file_path,$avatar);
return true;
} catch (OssException $e) {
/* 返回JSON数据 */
$this->error($e->getMessage());
}
}
public function multiuploadFile($ossClient, $bucket,$url,$file){
//$file = __FILE__;
$options = array();
try{
#初始化分片上传文件
$uploadId = $ossClient->initiateMultipartUpload($bucket, $url);
//$ossClient->multiuploadFile($bucket, $url, $file, $options);
} catch(OssException $e) {
printf(__FUNCTION__ . ": initiateMultipartUpload FAILED\n");
printf($e->getMessage() . "\n");
return;
}
/*
* step 2. 上传分片
*/
$partSize = 5 * 1000 * 1024;
$uploadFile = $file;
$uploadFileSize = filesize($uploadFile);
$pieces = $ossClient->generateMultiuploadParts($uploadFileSize, $partSize);
$responseUploadPart = array();
$uploadPosition = 0;
$isCheckMd5 = true;
foreach ($pieces as $i => $piece) {
$fromPos = $uploadPosition + (integer)$piece[$ossClient::OSS_SEEK_TO];
$toPos = (integer)$piece[$ossClient::OSS_LENGTH] + $fromPos - 1;
$upOptions = array(
$ossClient::OSS_FILE_UPLOAD => $uploadFile,
$ossClient::OSS_PART_NUM => ($i + 1),
$ossClient::OSS_SEEK_TO => $fromPos,
$ossClient::OSS_LENGTH => $toPos - $fromPos + 1,
$ossClient::OSS_CHECK_MD5 => $isCheckMd5,
);
if ($isCheckMd5) {
$contentMd5 = \OSS\Core\OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos);
$upOptions[$ossClient::OSS_CONTENT_MD5] = $contentMd5;
}
//2. 将每一分片上传到OSS
try {
$responseUploadPart[] = $ossClient->uploadPart($bucket, $url, $uploadId, $upOptions);
} catch(OssException $e) {
printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} FAILED\n");
printf($e->getMessage() . "\n");
return;
}
//printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} OK\n");
}
$uploadParts = array();
foreach ($responseUploadPart as $i => $eTag) {
$uploadParts[] = array(
'PartNumber' => ($i + 1),
'ETag' => $eTag,
);
}
/**
* step 3. 完成上传
*/
try {
$ossClient->completeMultipartUpload($bucket, $url, $uploadId, $uploadParts);
} catch(OssException $e) {
printf(__FUNCTION__ . ": completeMultipartUpload FAILED\n");
printf($e->getMessage() . "\n");
return;
}
}
}