OosController.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. use yii\filters\AccessControl;
  6. use yii\data\Pagination;
  7. use yii\helpers\Json;
  8. use OSS\OssClient;
  9. use OSS\Core\OssException;
  10. /**
  11. * 阿里云 OSS
  12. * @author sofashi
  13. *
  14. */
  15. class OosController extends BackendController
  16. {
  17. public function actionIndex()
  18. {
  19. Yii::$app->end();
  20. //用户不能直接传图片到阿里云,需要放服务器上再传上去,多了一步操作
  21. //使用OSS域名新建OssClient
  22. $accessKeyId = "jIDNrMhDswHnaApr"; ;
  23. $accessKeySecret = "hZgNkwAqloRWpnFX9m8NhoClPNOqBm";
  24. $endpoint = "oss-cn-shanghai.aliyuncs.com";
  25. try {
  26. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  27. } catch (OssException $e) {
  28. print $e->getMessage();
  29. }
  30. $bucket = "sofashi7988";
  31. // try {
  32. // $ossClient->createBucket($bucket);
  33. // } catch (OssException $e) {
  34. // print $e->getMessage();
  35. // }
  36. // $object = "myObject";
  37. // $content = "Hi, OSS";
  38. // try {
  39. // $ossClient->putObject($bucket, $object, $content);
  40. // } catch (OssException $e) {
  41. // print $e->getMessage();
  42. // }
  43. // $bucketListInfo = $ossClient->listBuckets();
  44. // $bucketList = $bucketListInfo->getBucketList();
  45. // foreach($bucketList as $bucket) {
  46. // print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
  47. // }
  48. $object = "mypicture";
  49. $filePath = '/usr/local/nginx/html/a.jpg';
  50. try{
  51. $ossClient->uploadFile($bucket, $object, $filePath);
  52. } catch(OssException $e) {
  53. printf(__FUNCTION__ . ": FAILED\n");
  54. printf($e->getMessage() . "\n");
  55. return;
  56. }
  57. print(__FUNCTION__ . ": OK" . "\n");
  58. return $this->render('index');
  59. }
  60. }