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.

50 lines
1.4 KiB

  1. <?php
  2. namespace App\Controller;
  3. use OSS\Core\OssException;
  4. use OSS\OssClient;
  5. class FileUpload extends AbstractController
  6. {
  7. private $file;
  8. private $path;
  9. private $type;
  10. public function __construct()
  11. {
  12. if (is_file(__DIR__ . '/../autoload.php')) {
  13. require_once __DIR__ . '/../autoload.php';
  14. }
  15. if (is_file(__DIR__ . '/../vendor/autoload.php')) {
  16. require_once __DIR__ . '/../vendor/autoload.php';
  17. }
  18. }
  19. public function ossUpload($fileContent,$fileName)
  20. {
  21. $img = base64_decode($fileContent);
  22. $accessKeyId = "LTAI4G8QMM97Fg1Gx8TXeast";
  23. $accessKeySecret = "f1KIbMCiO4CXmTzC0FDl6hTRXgFobm";
  24. // Endpoint以杭州为例,其它Region请按实际情况填写。
  25. $endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
  26. // 设置存储空间名称。
  27. $bucket = "desion-test1";
  28. // 设置文件名称。
  29. $object = $fileName;
  30. // 配置文件内容。
  31. $content = $img;
  32. try {
  33. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  34. $ossClient->createBucket($bucket);
  35. $result = $ossClient->putObject($bucket, $object, $content);
  36. return $result;
  37. } catch (OssException $e) {
  38. printf(__FUNCTION__ . ": FAILED\n");
  39. printf($e->getMessage() . "\n");
  40. return false;
  41. }
  42. }
  43. }