专业做网站的都晓得,OSS存储是个好工具,假设你想网站上放个视频,假设接纳第三方优酷、土豆视频,有良多告白,放在自己网站上异常不友爱。假设间接放在网站处事器,则占用良多带宽,增添处事器负担。以是OSS存储的浮现,办理了这个成绩,
品牌网站定制公司,速率快又不占用处事器带宽,又廉价。
还能够哄骗OSS存储,完成多处事器布置网站。
外洋网站若何将资料存在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 ='***xxXXXX' ;//key
$this->secret = '***xxXXXXXXXX';//secret
$this->region = "ap-southeast-1";//地区
$this->version = 'latest';//版本号
$this->endpoint ='http://s3.ap-southeast-1.amazonaws.com';//公网会见地点
$this->bucket = '***xx';//桶
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,
&nb