PHP code example of easyswoole / oss
1. Go to this page and download the library: Download easyswoole/oss library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
easyswoole / oss example snippets
/**
* Created by PhpStorm.
* User: Tioncico
* Date: 2019/11/20 0020
* Time: 15:28
*/
'accessKeyId' => ACCESS_KEY_ID,
'accessKeySecret' => ACCESS_KEY_SECRET,
'endpoint' => END_POINT,
]);
$client = new \EasySwoole\Oss\AliYun\OssClient($config);
$data = $client->putObject('tioncicoxyz','test',__FILE__);
var_dump($data);
});
go(function (){
$auth = new \EasySwoole\Oss\QiNiu\Auth(QINIU_ACCESS_KEY,QINIU_SECRET_KEY);
$key = 'formPutFileTest';
$token = $auth->uploadToken('tioncico', $key);
$upManager = new \EasySwoole\Oss\QiNiu\Storage\UploadManager();
list($ret, $error) = $upManager->putFile($token, $key, __file__, null, 'text/plain', null);
var_dump($ret,$error);
});
\EasySwoole\Oss\QiNiu\Config::setTimeout(3);
\EasySwoole\Oss\QiNiu\Config::setConnectTimeout(5);
de "../../phpunit2.php";
go(function (){
//config配置
$config = new \EasySwoole\Oss\Tencent\Config([
'appId' => TX_APP_ID,
'secretId' => TX_SECRETID,
'secretKey' => TX_SECRETKEY,
'region' => TX_REGION,
'bucket' => TX_BUCKET,
]);
//new客户端
$cosClient = new \EasySwoole\Oss\Tencent\OssClient($config);
$key = '你好111.txt';
//生成一个文件数据
$body = generateRandomString(2 * 1024 + 1023);
//上传
$cosClient->upload($bucket = TX_BUCKET,
$key = $key,
$body = $body,
$options = ['PartSize' => 1024 + 1]
);
//获取文件内容
$rt = $cosClient->getObject(['Bucket' => TX_BUCKET, 'Key' => $key]);
var_dump($rt['Body']->__toString());
});
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}