You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.6 KiB

7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
  1. <?php
  2. namespace App\Common;
  3. use JohnLui\AliyunOSS;
  4. class OSS
  5. {
  6. private $ossClient;
  7. public function __construct($isInternal = false)
  8. {
  9. $serverAddress = $isInternal ? config('app.ossServerInternal') : config('app.ossServer');
  10. $this->ossClient = AliyunOSS::boot(
  11. $serverAddress,
  12. config('app.AccessKeyId'),
  13. config('app.AccessKeySecret')
  14. );
  15. }
  16. public static function upload($ossKey, $filePath, $bucket = '')
  17. {
  18. $isInternal = config('app.isInternal');
  19. !$bucket && $bucket = config('app.ossBucket');
  20. $oss = new OSS($isInternal); // 上传文件使用内网,免流量费
  21. $oss->ossClient->setBucket($bucket);
  22. $oss->ossClient->uploadFile($ossKey, $filePath);
  23. }
  24. /**
  25. * 直接把变量内容上传到oss
  26. *
  27. * @param $osskey
  28. * @param $content
  29. * @param string $bucket
  30. */
  31. public static function uploadContent($osskey, $content, $bucket = '')
  32. {
  33. $isInternal = config('app.isInternal');
  34. !$bucket && $bucket = config('app.ossBucket');
  35. $oss = new OSS($isInternal); // 上传文件使用内网,免流量费
  36. $oss->ossClient->setBucket($bucket);
  37. $oss->ossClient->uploadContent($osskey, $content);
  38. }
  39. /**
  40. * 删除存储在oss中的文件
  41. *
  42. * @param string $ossKey 存储的key(文件路径和文件名)
  43. * @param string $bucket
  44. *
  45. * @return bool
  46. */
  47. public static function deleteObject($ossKey, $bucket = '')
  48. {
  49. $isInternal = config('app.isInternal');
  50. !$bucket && $bucket = config('app.ossBucket');
  51. $oss = new OSS($isInternal); // 上传文件使用内网,免流量费
  52. return $oss->ossClient->deleteObject($bucket, $ossKey);
  53. }
  54. /**
  55. * 复制存储在阿里云OSS中的Object
  56. *
  57. * @param string $sourceBuckt 复制的源Bucket
  58. * @param string $sourceKey - 复制的的源Object的Key
  59. * @param string $destBucket - 复制的目的Bucket
  60. * @param string $destKey - 复制的目的Object的Key
  61. *
  62. * @return Models\CopyObjectResult
  63. */
  64. public function copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey)
  65. {
  66. $oss = new OSS(true); // 上传文件使用内网,免流量费
  67. return $oss->ossClient->copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey);
  68. }
  69. /**
  70. * 移动存储在阿里云OSS中的Object
  71. *
  72. * @param string $sourceBuckt 复制的源Bucket
  73. * @param string $sourceKey - 复制的的源Object的Key
  74. * @param string $destBucket - 复制的目的Bucket
  75. * @param string $destKey - 复制的目的Object的Key
  76. *
  77. * @return Models\CopyObjectResult
  78. */
  79. public function moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey)
  80. {
  81. $oss = new OSS(true); // 上传文件使用内网,免流量费
  82. return $oss->ossClient->moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey);
  83. }
  84. public static function getUrl($ossKey, $bucket = '')
  85. {
  86. !$bucket && $bucket = config('app.ossBucket');
  87. $oss = new OSS();
  88. $oss->ossClient->setBucket($bucket);
  89. return $oss->ossClient->getUrl($ossKey, new \DateTime("+1 day"));
  90. }
  91. public static function createBucket($bucketName)
  92. {
  93. $oss = new OSS();
  94. return $oss->ossClient->createBucket($bucketName);
  95. }
  96. public static function getAllObjectKey($bucketName)
  97. {
  98. $oss = new OSS();
  99. return $oss->ossClient->getAllObjectKey($bucketName);
  100. }
  101. }