OssInUe.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/1/16 0016
  6. * Time: 15:25
  7. */
  8. if (is_file('../../../../vendor/autoload.php')) {
  9. require_once '../../../../vendor/autoload.php';
  10. }
  11. use OSS\OssClient;
  12. use OSS\Core\OssException;
  13. /**
  14. * Created by PhpStorm.
  15. * User: crjy
  16. * Date: 2017/10/13
  17. * Time: 15:46
  18. */
  19. class OssInUe{
  20. public function __construct(){
  21. }
  22. function uploadToAliOSS($file,$fileType){
  23. $entension = $fileType; //上传文件的后缀
  24. $newName = time().mt_rand(100000,999999).$entension;//上传到oss的文件名 文件重命名
  25. $accessKeyId = 'LTAI4FmF5UEfXfw29zULbJJY';
  26. $accessKeySecret ='SO8aAM4pslKO1IE2gDMv6yI2ARuFN3';
  27. $endpoint = 'oss-cn-chengdu.aliyuncs.com';//域名,端点名
  28. $bucket= 'shopkdimg';//" <您使用的Bucket名字,注意命名规范>";
  29. $object ='image/'.date('y-m',time()).'/'.$newName;//" <您使用的Object名字,注意命名规范>"; (可以写到.env文件中调用)
  30. $content = $file["tmp_name"];//上传的文件
  31. try {
  32. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint,false);
  33. $ossClient->setTimeout(3600); /* seconds */
  34. $ossClient->setConnectTimeout(10); /* seconds */
  35. // $ossClient->putObject($bucket, $object, $content);
  36. // 先把本地的example.jpg上传到指定$bucket, 命名为$object
  37. $ossClient->uploadFile($bucket, $object, $content);
  38. $signedUrl = $ossClient->signUrl($bucket, $object);
  39. $path = explode('?',$signedUrl)[0];
  40. $obj['status'] = true;
  41. $obj['path'] = $path;
  42. } catch (OssException $e) {
  43. $obj['status'] = false;
  44. $obj['path'] = "";
  45. print $e->getMessage();
  46. }
  47. return $obj;
  48. }
  49. }