服务热线 400-660-5555

北京网站建设
首页 站内资讯

北京网站建设

站内资讯
北京网站建设 / 站内资讯 / 行业资讯 / 正文

亚马逊s3-OSS存储文件上传读取接口

来源: All文章
发布时间:2023-04-10 16:08:21

  专业做网站的都知道,OSS存储是个好东西,如果你想网站上放个视频,如果采用第三方优酷、土豆视频,有很多广告,放在自己网站上非常不友好。如果直接放在网站服务器,则占用很多带宽,增加服务器负担。所以OSS存储的出现,解决了这个问题,速度快又不占用服务器带宽,又便宜。

  还可以利用OSS存储,实现多服务器部署网站。

  国外网站如何将资料存在oss,除了中企动力在亚马逊也同样有类似的服务Amazon S3对象存储

  这里用的的是thinkph

  3.2和亚马逊s3接口

  接口实例地址:

  新建一个class

  引入api文件

  Vendor('Amazon.autoloader');

  useAws\S3\S3Client;

  useAws\S3\MultipartUploader;

  classAwsFile

  {

  public$key;

  public$secret;

  //链接亚马逊服务器

  /**

  *Aws类初始化

  *

  */

  publicfunction__construct()

  {

  $this-key='xxxxxXXXX';//key

  $this-secret='xxxxxXXXXXXXX';//secret

  $this-region=ap-southeast-1;//区域

  $this-version='latest';//版本号

  $this-endpoint='http://s

  3.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();

og::add(__PUBLIC_.'|s3ImageConstruct',$msg);

  returnfalse;

  }

  returntrue;

  }

  //基础上传

  /**

  *uploadfile基础上传

  *name文件名

  *fileUrl文件路径(绝对地址)

  */

  publicfunctionuploadFile($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;

  }

  }

  /**

  *自定义分段上传

  */

  publicfunctionmultipartUploader($file_name,$file_path)

  {

  $source=$file_path;

  //多部件上传

  $uploader=newMultipartUploader($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=newMultipartUploader($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。

  */

  publicfunctiongetFileUrl($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();

  returnfalse;

  }

  }

* 文章来源于网络,如有侵权,请联系客服删除处理。
在线 咨询

添加动力小姐姐微信

微信 咨询

电话咨询

400-660-5555

我们联系您

电话 咨询
微信扫码关注动力小姐姐 X
qr