专业做网站的都知道,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 ='xxxxxXXXX' ;//key
$this->secret = 'xxxxxXXXXXXXX';//secret
$this->region = "ap-southeast-1";//区域
$this->version = 'latest';//版本号
$this->endpoint ='http://s3.ap-southeast-1.amazonaws.com';//公网访问地址
$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根据文件名称获取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 {
//创建预签名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;
}
}
如没特殊注明,文章均为方维网络原创,转载请注明来自https://www.szfangwei.cn/news/6294.html