public function testConstrunct8()
 {
     try {
         $ossClient = new OSSClient(Config::OSS_ACCESS_ID, Config::OSS_ACCESS_KEY, "http://123.123.123.1", true);
         $ossClient->listBuckets();
         $this->assertFalse(true);
     } catch (Exception $e) {
     }
 }
Example #2
0
function putResourceObject(OSSClient $client, $bucket, $key, $content, $size)
{
    $result = false;
    try {
        $result = $client->putObject(array('Bucket' => $bucket, 'Key' => $key, 'Content' => $content, 'ContentLength' => $size));
    } catch (Exception $e) {
        $result = false;
    }
    return $result;
}
Example #3
0
 function putResourceObject(OSSClient $client, $bucket, $key, $content, $size, $output)
 {
     $result = false;
     try {
         $result = $client->putObject(array('Bucket' => $bucket, 'Key' => $key, 'Content' => $content, 'ContentLength' => $size));
     } catch (Exception $e) {
         $output["message"] = "upload exception : " . $e->getMessage();
         $result = false;
     }
     return $result;
 }
 public function boot()
 {
     Storage::extend('oss', function ($app, $config) {
         $client = OSSClient::factory(array('AccessKeyId' => $config['access_id'], 'AccessKeySecret' => $config['access_key'], 'Endpoint' => $config['endpoint']));
         return new Filesystem(new AliyunOssAdapter($client, $config['bucket'], $config['prefix']));
     });
 }
Example #5
0
function multipartUploadSample()
{
    $fileName = '/path/to/file';
    $bucket = 'your-bucket-name';
    $key = 'your-object-key';
    $partSize = 5 * 1024 * 1024;
    // 5M for each part
    $client = OSSClient::factory(array('AccessKeyId' => 'your-access-key-id', 'AccessKeySecret' => 'your-access-key-secret'));
    // Init multipart upload
    $uploadId = $client->initiateMultipartUpload(array('Bucket' => $bucket, 'Key' => $key))->getUploadId();
    // upload parts
    $fileSize = filesize($fileName);
    $partCount = (int) ($fileSize / $partSize);
    if ($fileSize % $partSize > 0) {
        $partCount += 1;
    }
    $partETags = array();
    for ($i = 0; $i < $partCount; $i++) {
        $uploadPartSize = ($i + 1) * $partSize > $fileSize ? $fileSize - $i * $partSize : $partSize;
        $file = fopen($fileName, 'r');
        fseek($file, $i * $partSize);
        $partETag = $client->uploadPart(array('Bucket' => $bucket, 'Key' => $key, 'UploadId' => $uploadId, 'PartNumber' => $i + 1, 'PartSize' => $uploadPartSize, 'Content' => $file))->getPartETag();
        $partETags[] = $partETag;
    }
    $result = $client->completeMultipartUpload(array('Bucket' => $bucket, 'Key' => $key, 'UploadId' => $uploadId, 'PartETags' => $partETags));
    echo "Completed: " . $result->getETag();
}
Example #6
0
 /**
  * 获取所有的对象的Key.
  *
  * @param string $bucket
  *
  * @return array
  */
 public function getAllObjectKeys($bucket = null)
 {
     $objectLists = $this->client->listObjects([OSSOptions::BUCKET => $this->getBucket($bucket)]);
     return array_map(function ($objectSummary) {
         return $objectSummary->getKey();
     }, $objectLists->getObjectSummarys());
 }
Example #7
0
File: OSS.php Project: sr71k/pet
 /**
  * Instance the S3 object
  */
 public function connect()
 {
     if ($this->aKey === NULL || $this->sKey === NULL) {
         throw new CException('OSS Keys are not set.');
     }
     $this->_oss = OSSClient::factory(array('AccessKeyId' => $this->aKey, 'AccessKeySecret' => $this->sKey, 'Endpoint' => $this->endpoint));
 }
Example #8
0
 private function putResourceObject(OSSClient $client, $fileInfo)
 {
     $strPath = './Public/uploads/' . $fileInfo['savename'];
     $content = fopen($strPath, 'r');
     $size = filesize($strPath);
     $fileName = $fileInfo['md5'] . '.' . $fileInfo['ext'];
     $result = $client->putObject(array('Bucket' => $this->bucket, 'Key' => $fileName, 'Content' => $content, 'ContentLength' => $size, 'ContentType' => $fileInfo['type'], 'ContentDisposition' => "inline;filename={$fileName}"));
     fclose($content);
     if ($result->getETag()) {
         $req['code'] = 0;
         $req['imgUrl'] = $this->url . $fileName;
         return $req;
     } else {
         $req['code'] = -1;
     }
     return $req;
 }
Example #9
0
 /**
  * 初始化OSS客户端
  *
  * @author Xuan
  */
 protected function initialClient()
 {
     $configs = array('AccessKeyId' => $this->accessKey, 'AccessKeySecret' => $this->accessKeySecret);
     if ($city = $this->getConfig('buckets.' . $this->bucket)) {
         $configs[OSSOptions::ENDPOINT] = $this->getServerAddr($city);
     }
     $this->client = OSSClient::factory($configs);
 }
Example #10
0
 public function __construct(array $config)
 {
     $endpoint = array_get($config, 'endpoint', '');
     if (!starts_with($endpoint, 'http://') || !starts_with($endpoint, 'https://')) {
         $endpoint = 'http://' . $endpoint;
     }
     $this->client = OSSClient::factory([OSSOptions::ACCESS_KEY_ID => array_get($config, 'access_key_id', ''), OSSOptions::ACCESS_KEY_SECRET => array_get($config, 'access_key_secret', ''), OSSOptions::ENDPOINT => $endpoint]);
     $this->setBucketPrefix(array_get($config, 'bucket_prefix', ''));
 }
Example #11
0
 /**
  * 临时文件夹直接上传到oss上
  * @param array  $file_param   $file_param['name'] 对应的临时文件夹的文件名 $file_param['tmp_name'] 临时文件夹的路径
  * 
  * @return string 阿里云OSS图片地址
  */
 public static function simpleUploadV2($file_param)
 {
     $img_name = md5('pico' . microtime()) . '.' . end(explode(".", $file_param['name']));
     //阿里云地址
     $ossClient = OSSClient::factory(array('AccessKeyId' => Yii::$app->params['keyId'], 'AccessKeySecret' => Yii::$app->params['keySecret'], 'Endpoint' => Yii::$app->params['Endpoint']));
     $ossClient->putObject(array('Bucket' => Yii::$app->params['classPicBucket'], 'Key' => $img_name, 'Content' => fopen($file_param['tmp_name'], 'r'), 'ContentLength' => filesize($file_param['tmp_name'])));
     $url = "http://" . Yii::$app->params['classPicBucket'] . ".oss-cn-qingdao.aliyuncs.com/" . $img_name;
     return $url;
 }
Example #12
0
 /**
  * 上传至oss
  * @method putFileToOss
  * @since 0.0.1
  * @param {string} $key 文件名(包括存储路径)
  * @param {string} $file 文件本地路径
  * @param {string} [$type=images] bucket别名
  * @return {string|boolean}
  * @example \Yii::$app->fileupload->putFileToOss($key, $file, $type);
  */
 public function putFileToOss($key, $file, $type = 'images')
 {
     $src = null;
     if (isset($this->oss[$type])) {
         OSSClient::factory($this->oss['config'])->putObject(['Bucket' => $this->oss[$type]['Bucket'], 'Key' => $key, 'Content' => fopen($file, 'r'), 'ContentLength' => filesize($file)]);
         $src = $this->oss[$type]['src'] . DIRECTORY_SEPARATOR . $key;
         unlink($file);
     }
     return $src;
 }
 public function boot()
 {
     Storage::extend('oss', function ($app, $config) {
         $ossconfig = ['AccessKeyId' => $config['access_id'], 'AccessKeySecret' => $config['access_key']];
         if (isset($config['endpoint']) && !empty($config['endpoint'])) {
             $ossconfig['Endpoint'] = $config['endpoint'];
         }
         $client = OSSClient::factory($ossconfig);
         return new Filesystem(new AliyunOssAdapter($client, $config['bucket'], $config['prefix']));
     });
 }
Example #14
0
function handleExceptionSample()
{
    try {
        $client = OSSClient::factory(array('AccessKeyId' => 'your-access-key-id', 'AccessKeySecret' => 'your-access-key-secret'));
        $client->listBuckets();
    } catch (OSSException $ex) {
        echo "OSSException: " . $ex->getErrorCode() . " Message: " . $ex->getMessage();
    } catch (ClientException $ex) {
        echo "ClientExcetpion, Message: " . $ex->getMessage();
    }
}
 /**
  * @return Oss
  */
 protected function prepareAdapter()
 {
     $config = [];
     /**
      * 获取OSSClient实例用以访问OSS服务
      *
      * @param array $config Client的配置信息,可以包含下列Key:
      * <li> Endpoint(必选) - OSS服务的Endpoint。必须以"http://"开头。
      * <li> AccessKeyId(必选) - 访问OSS的Access Key ID。 </li>
      * <li> AccessKeySecret(必选) -  访问OSS的Access Key Secret。</li>
      *
      * @return OSSClient
      */
     $config[ServiceOptions::ACCESS_KEY_ID] = $this->client_key;
     $config[ServiceOptions::ACCESS_KEY_SECRET] = $this->client_secret;
     $config[ServiceOptions::ENDPOINT] = $this->endpoint;
     $ossClient = OSSClient::factory($config);
     return new AliyunOssAdapter($ossClient, $this->bucket);
 }
 /**
  * {@inheritdoc}
  */
 public function __construct($app)
 {
     parent::__construct($app);
     if (class_exists('\\Zhuxiaoqiao\\Flysystem\\BaiduBos\\BaiduBosAdapter')) {
         $this->extend('bos', function ($app, $config) {
             return $this->createFlysystem(new \Zhuxiaoqiao\Flysystem\BaiduBos\BaiduBosAdapter(new \BaiduBce\Services\Bos\BosClient(Arr::except($config, ['driver', 'bucket'])), $config['bucket']), $config);
         });
     }
     if (class_exists('\\Enl\\Flysystem\\Cloudinary\\CloudinaryAdapter')) {
         $this->extend('cloudinary', function ($app, $config) {
             return $this->createFlysystem(new \Enl\Flysystem\Cloudinary\CloudinaryAdapter(new \Enl\Flysystem\Cloudinary\ApiFacade($config)), $config);
         });
     } elseif (class_exists('\\T3chnik\\FlysystemCloudinaryAdapter\\CloudinaryAdapter')) {
         $this->extend('cloudinary', function ($app, $config) {
             return $this->createFlysystem(new \T3chnik\FlysystemCloudinaryAdapter\CloudinaryAdapter($config, new \Cloudinary\Api()), $config);
         });
     }
     if (class_exists('\\Rokde\\Flysystem\\Adapter\\LocalDatabaseAdapter')) {
         $this->extend('eloquent', function ($app, $config) {
             return $this->createFlysystem(new \Rokde\Flysystem\Adapter\LocalDatabaseAdapter($app->make(Arr::get($config, 'model', '\\Rokde\\Flysystem\\Adapter\\Model\\FileModel'))), $config);
         });
     }
     if (class_exists('\\Litipk\\Flysystem\\Fallback\\FallbackAdapter')) {
         $this->extend('fallback', function ($app, $config) {
             return $this->createFlysystem(new \Litipk\Flysystem\Fallback\FallbackAdapter($this->disk($config['main'])->getAdapter(), $this->disk($config['fallback'])->getAdapter()), $config);
         });
     }
     if (class_exists('\\Potherca\\Flysystem\\Github\\GithubAdapter')) {
         $this->extend('github', function ($app, $config) {
             $settings = new \Potherca\Flysystem\Github\Settings($config['project'], [\Potherca\Flysystem\Github\Settings::AUTHENTICATE_USING_TOKEN, $config['token']]);
             return $this->createFlysystem(new \Potherca\Flysystem\Github\GithubAdapter(new \Potherca\Flysystem\Github\Api(new \Github\Client(), $settings)), $config);
         });
     }
     if (class_exists('\\Ignited\\Flysystem\\GoogleDrive\\GoogleDriveAdapter')) {
         $this->extend('gdrive', function ($app, $config) {
             $client = new \Google_Client();
             $client->setClientId($config['client_id']);
             $client->setClientSecret($config['secret']);
             $client->setAccessToken(json_encode(["access_token" => $config['token'], "expires_in" => 3920, "token_type" => "Bearer", "created" => time()]));
             return $this->createFlysystem(new \Ignited\Flysystem\GoogleDrive\GoogleDriveAdapter(new \Google_Service_Drive($client)), $config);
         });
     }
     if (class_exists('\\Superbalist\\Flysystem\\GoogleStorage\\GoogleStorageAdapter')) {
         $this->extend('google', function ($app, $config) {
             $client = new \Google_Client();
             $client->setAssertionCredentials(new \Google_Auth_AssertionCredentials($config['account'], [\Google_Service_Storage::DEVSTORAGE_FULL_CONTROL], file_get_contents($config['p12_file']), $config['secret']));
             $client->setDeveloperKey($config['developer_key']);
             return $this->createFlysystem(new \Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter(new \Google_Service_Storage($client), $config['bucket']), $config);
         });
     }
     if (class_exists('\\Litipk\\Flysystem\\Fallback\\FallbackAdapter') && class_exists('\\League\\Flysystem\\Replicate\\ReplicateAdapter')) {
         $this->extend('mirror', function ($app, $config) {
             return $this->createFlysystem($this->buildMirrors($config['disks']), $config);
         });
     }
     if (class_exists('\\JacekBarecki\\FlysystemOneDrive\\Adapter\\OneDriveAdapter')) {
         $this->extend('onedrive', function ($app, $config) {
             return $this->createFlysystem(new \JacekBarecki\FlysystemOneDrive\Adapter\OneDriveAdapter(new \JacekBarecki\FlysystemOneDrive\Client\OneDriveClient(Arr::get($config, 'access_token'), new \GuzzleHttp\Client())), $config);
         });
     } elseif (class_exists('\\Ignited\\Flysystem\\OneDrive\\OneDriveAdapter')) {
         $this->extend('onedrive', function ($app, $config) {
             $oneConfig = Arr::only($config, ['base_url', 'access_token']);
             if ($config['use_logger']) {
                 $logger = Log::getMonolog();
             } else {
                 $logger = null;
             }
             return $this->createFlysystem(new \Ignited\Flysystem\OneDrive\OneDriveAdapter(\Ignited\Flysystem\OneDrive\OneDriveClient::factory($oneConfig, $logger)), $config);
         });
     }
     if (class_exists('\\Orzcc\\AliyunOss\\AliyunOssAdapter')) {
         $this->extend('oss', function ($app, $config) {
             $ossconfig = ['AccessKeyId' => $config['access_id'], 'AccessKeySecret' => $config['access_key']];
             if (isset($config['endpoint']) && !empty($config['endpoint'])) {
                 $ossconfig['Endpoint'] = $config['endpoint'];
             }
             return $this->createFlysystem(new \Orzcc\AliyunOss\AliyunOssAdapter(\Aliyun\OSS\OSSClient::factory($ossconfig), $config['bucket'], $config['prefix']), $config);
         });
     } elseif (class_exists('\\Shion\\Aliyun\\OSS\\Adapter\\OSSAdapter')) {
         $this->extend('oss', function ($app, $config) {
             return $this->createFlysystem(new \Shion\Aliyun\OSS\Adapter\OSSAdapter(new \Shion\Aliyun\OSS\Client\OSSClient(Arr::except($config, ['driver', 'bucket'])), $config['bucket']), $config);
         });
     }
     if (class_exists('\\EQingdan\\Flysystem\\Qiniu\\QiniuAdapter')) {
         $this->extend('qiniu', function ($app, $config) {
             return $this->createFlysystem(new \EQingdan\Flysystem\Qiniu\QiniuAdapter($config['accessKey'], $config['secretKey'], $config['bucket'], $config['domain']), $config);
         });
     } elseif (class_exists('\\Polev\\Flysystem\\Qiniu\\QiniuAdapter')) {
         $this->extend('qiniu', function ($app, $config) {
             return $this->createFlysystem(new \Polev\Flysystem\Qiniu\QiniuAdapter($config['accessKey'], $config['secretKey'], $config['bucket']), $config);
         });
     }
     if (class_exists('\\Danhunsaker\\Flysystem\\Redis\\RedisAdapter')) {
         $this->extend('redis', function ($app, $config) {
             $client = $app->make('redis')->connection(Arr::get($config, 'connection', 'default'));
             return $this->createFlysystem(new \Danhunsaker\Flysystem\Redis\RedisAdapter($client), $config);
         });
     }
     if (class_exists('\\Engineor\\Flysystem\\RunaboveAdapter')) {
         $this->extend('runabove', function ($app, $config) {
             $config['region'] = constant(\Engineor\Flysystem\Runabove::class . '::REGION_' . strtoupper($config['region']));
             $client = new \Engineor\Flysystem\Runabove(Arr::except($config, ['driver']));
             return $this->createFlysystem(new \Engineor\Flysystem\RunaboveAdapter($client->getContainer()), $config);
         });
     }
     if (class_exists('\\Coldwind\\Filesystem\\KvdbAdapter')) {
         $this->extend('sae', function ($app, $config) {
             return $this->createFlysystem(new \Coldwind\Filesystem\KvdbAdapter(new \Coldwind\Filesystem\KvdbClient()), $config);
         });
     }
     if (class_exists('\\RobGridley\\Flysystem\\Smb\\SmbAdapter')) {
         $this->extend('smb', function ($app, $config) {
             $server = new \Icewind\SMB\Server($config['host'], $config['username'], $config['password']);
             $share = $server->getShare($config['path']);
             return $this->createFlysystem(new \RobGridley\Flysystem\Smb\SmbAdapter($share), $config);
         });
     }
     if (class_exists('\\Emgag\\Flysystem\\TempdirAdapter')) {
         $this->extend('temp', function ($app, $config) {
             return $this->createFlysystem(new \Emgag\Flysystem\TempdirAdapter(Arr::get($config, 'prefix'), Arr::get($config, 'tempdir')), $config);
         });
     }
 }
Example #17
0
 /**
  * 构造方法初始化阿里云sdk
  */
 public function __construct()
 {
     $this->client = OSSClient::factory(array('AccessKeyId' => $this->AccessKeyId, 'AccessKeySecret' => $this->AccessKeySecret, 'Endpoint' => 'http://oss-cn-qingdao-internal.aliyuncs.com'));
 }
 public function setUp()
 {
     $this->bucket = Common::getBucketName();
     $this->ossClient = Common::getOSSClient();
     $this->ossClient->createBucket($this->bucket);
 }
Example #19
0
function deleteBucket(OSSClient $client, $bucket)
{
    $client->deleteBucket(array('Bucket' => $bucket));
}
Example #20
0
 private static function getOssClient()
 {
     $ossClient = OSSClient::factory(array('AccessKeyId' => CdnConfig::ACCESS_KEY_ID, 'AccessKeySecret' => CdnConfig::ACCESS_KEY_SECRET));
     return $ossClient;
 }
Example #21
0
function deleteObject(OSSClient $client, $bucket, $key)
{
    $client->deleteObject(array('Bucket' => $bucket, 'Key' => $key));
}
Example #22
0
 /**
  * 构造
  *
  * @param array $db_conf
  *        	array(host, port, dbname, usr, pwd)
  * @param boolean $persistent
  *        	or not
  * @throws Exception
  */
 public function __construct()
 {
     $this->client = OSSClient::factory(array('AccessKeyId' => $this->_oss_key_id, 'AccessKeySecret' => $this->_oss_key_secret));
     $this->_buckets = array("1" => array("name" => "a1-peoplecdn", "host" => "a1.peoplecdn.cn"), "2" => array("name" => "a2-peoplecdn", "host" => "a2.peoplecdn.cn"), "3" => array("name" => "a3-peoplecdn", "host" => "a3.peoplecdn.cn"), "4" => array("name" => "a4-peoplecdn", "host" => "a4.peoplecdn.cn"));
 }
Example #23
0
 public function __construct($serverName, $AccessKeyId, $AccessKeySecret)
 {
     $this->ossClient = OSSClient::factory([OSSOptions::ENDPOINT => $serverName, 'AccessKeyId' => $AccessKeyId, 'AccessKeySecret' => $AccessKeySecret]);
 }
 function client()
 {
     $client = OSSClient::factory(array('Endpoint' => $this->config['Endpoint'], 'AccessKeyId' => $this->config['AccessKeyId'], 'AccessKeySecret' => $this->config['AccessKeySecret']));
     return $client;
 }