| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace backend\controllers;
- use Yii;
- use yii\web\Controller;
- use yii\filters\AccessControl;
- use yii\data\Pagination;
- use yii\helpers\Json;
- use OSS\OssClient;
- use OSS\Core\OssException;
- /**
- * 阿里云 OSS
- * @author sofashi
- *
- */
- class OosController extends BackendController
- {
- public function actionIndex()
- {
- Yii::$app->end();
- //用户不能直接传图片到阿里云,需要放服务器上再传上去,多了一步操作
- //使用OSS域名新建OssClient
- $accessKeyId = "jIDNrMhDswHnaApr"; ;
- $accessKeySecret = "hZgNkwAqloRWpnFX9m8NhoClPNOqBm";
- $endpoint = "oss-cn-shanghai.aliyuncs.com";
- try {
- $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
- } catch (OssException $e) {
- print $e->getMessage();
- }
- $bucket = "sofashi7988";
- // try {
- // $ossClient->createBucket($bucket);
- // } catch (OssException $e) {
- // print $e->getMessage();
- // }
- // $object = "myObject";
- // $content = "Hi, OSS";
- // try {
- // $ossClient->putObject($bucket, $object, $content);
- // } catch (OssException $e) {
- // print $e->getMessage();
- // }
- // $bucketListInfo = $ossClient->listBuckets();
- // $bucketList = $bucketListInfo->getBucketList();
- // foreach($bucketList as $bucket) {
- // print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
- // }
- $object = "mypicture";
- $filePath = '/usr/local/nginx/html/a.jpg';
- try{
- $ossClient->uploadFile($bucket, $object, $filePath);
- } catch(OssException $e) {
- printf(__FUNCTION__ . ": FAILED\n");
- printf($e->getMessage() . "\n");
- return;
- }
- print(__FUNCTION__ . ": OK" . "\n");
- return $this->render('index');
- }
- }
|