專業(yè)做網(wǎng)站的都知道,OSS存儲是個好東西,如果你想網(wǎng)站上放個視頻,如果采用第三方優(yōu)酷、土豆視頻,有很多廣告,放在自己網(wǎng)站上非常不友好。如果直接放在網(wǎng)站服務器,則占用很多帶寬,增加服務器負擔。所以OSS存儲的出現(xiàn),解決了這個問題,速度快又不占用服務器帶寬,又便宜。
還可以利用OSS存儲,實現(xiàn)多服務器部署網(wǎng)站。
國外網(wǎng)站如何將資料存在oss,除了阿里云在亞馬遜也同樣有類似的服務Amazon S3對象存儲
這里用的的是 thinkphp3.2 和 亞馬遜s3接口
接口實例地址:
新建一個class
引入api文件
Vendor('Amazon.autoloader');
use Aws\S3\S3Client;
use Aws\S3\MultipartUploader;
class AwsFile
{
public $key;
public $secret;
//鏈接亞馬遜服務器
/**
* Aws類初始化
*
*/
public function __construct()
{
$this->key ='xxxxxXXXX' ;//key
$this->secret = 'xxxxxXXXXXXXX';//secret
$this->region = "ap-southeast-1";//區(qū)域
$this->version = 'latest';//版本號
$this->endpoint ='http://s3.ap-southeast-1.amazonaws.com';//公網(wǎng)訪問地址
$this->bucket = 'xxxxx';//桶
try {
$credentials = new \Aws\Credentials\Credentials($this->key, $this->secret);
$this->client = new \Aws\S3\S3Client([
'version' => $this->version,
'region' => $this->region,
'credentials' => $credentials,
'endpoint' => $this->endpoint,
//設置訪問權限 公開,不然訪問不了
'ACL' => 'public-read',
// 'debug' => true
]);
} catch (Exception $e) {
$msg = $e->getMessage();
Log::add(__PUBLIC_ . '|s3ImageConstruct', $msg);
return false;
}
return true;
}
//基礎上傳
/**
* upload file 基礎上傳
* name 文件名
* fileUrl 文件路徑(絕對地址)
*/
public function uploadFile($file_name, $file_path,$dir)
{
$key = $file_name;
$fileUrl = $file_path;
if (!file_exists($fileUrl)) {
return "當前目錄中,文件" . $fileUrl . "不存在";
}
try {
$result = $this->client->putObject([
'Bucket' => $this->bucket,
'Key' => trim($dir.$key),
'Body' => fopen($fileUrl, 'rb'),
'ACL' => 'public-read',
]);
$fileUrl = $result->get('ObjectURL');
return $fileUrl;
} catch (Exception $e) {
$msg = $e->getMessage();
return $msg;
}
}
/**
* 自定義分段上傳
*/
public function multipartUploader($file_name, $file_path)
{
$source = $file_path;
//多部件上傳
$uploader = new MultipartUploader($this->client, $source, [
//存儲桶
'bucket' => $this->bucket,
//上傳后的新地址
'key' => $file_name,
//設置訪問權限 公開,不然訪問不了
'ACL' => 'public-read',
//分段上傳
'before_initiate' => function (\Aws\Command $command) {
// $command 是CreateMultipartUpload操作
$command['CacheControl'] = 'max-age=3600';
},
'before_upload' => function (\Aws\Command $command) {
// $command 是一個UploadPart操作
$command['RequestPayer'] = 'requester';
},
'before_complete' => function (\Aws\Command $command) {
// $command 是一個CompleteMultipartUpload操作
$command['RequestPayer'] = 'requester';
},
]);
try {
$result = $uploader->upload();
//上傳成功--返回上傳后的地址
$resultOne = $this->client->getObjectUrl($this->bucket, $file_name);
$data = [
'type' => '1',
'data' => urldecode($result['ObjectURL']),
'resultOne' => $resultOne,
];
} catch (Aws\Exception\MultipartUploadException $e) {
//上傳失敗--返回錯誤信息
$uploader = new MultipartUploader($this->client, $source, [
'state' => $e->getState(),
]);
$data = [
'type' => '0',
'data' => $e->getMessage(),
];
}
return $data;
}
/**
* s3根據(jù)文件名稱獲取url
* fileName 文件名稱
* publicPath 證書路徑
* expire 過期時間
* $result = $this->client->getObjectUrl($this->bucket, $name);//此方法將返回給定存儲桶和密鑰的未簽名 URL。
*/
public function getFileUrl($fileName, $publicPath, $expire = 1)
{
if (empty($this->bucket)) {
return "";
}
try {
//創(chuàng)建預簽名url
$cmd = $this->client->getCommand('GetObject', [
'Bucket' => $this->bucket,
'Key' => trim($fileName)
]);
$request = $this->client->createPresignedRequest($cmd, '+' . $expire . 'weeks');
$presignedUrl = (string)$request->getUri();//獲取簽名對象的 URL
//檢驗訪問url是否有效
$array = get_headers($presignedUrl, 1);
//dump($array);
if (preg_match('/200/', $array[0])) {
//Log::add(__PUBLIC_ . '|s3GetFileUrlSuccess', "下載證書文件成功,url:".$presignedUrl."fileName".$fileName);
return $presignedUrl;
} else {
return $presignedUrl ;
exit;
}
} catch (Aws\S3\Exception\S3Exception $e) {
$msg = $e->getMessage();
return false;
}
}
如沒特殊注明,文章均為方維網(wǎng)絡原創(chuàng),轉載請注明來自http://pdcharm.com/news/6294.html