1. Go to this page and download the library: Download zhu/aliyun-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/ */
zhu / aliyun-oss example snippets
namespace App\Services;
use Zhu\AliyunOSS\AliyunOSS;
use Config;
class OSS {
private $ossClient;
public function __construct($isInternal = false)
{
$serverAddress = $isInternal ? Config::get('app.ossServerInternal') : Config::get('app.ossServer');
$this->ossClient = AliyunOSS::boot(
$serverAddress,
Config::get('app.AccessKeyId'),
Config::get('app.AccessKeySecret')
);
}
public static function upload($ossKey, $filePath)
{
$oss = new OSS(true); // 上传文件使用内网,免流量费
$oss->ossClient->setBucket('提前设置好的Bucket的名称');
$oss->ossClient->uploadFile($ossKey, $filePath);
}
public static function getUrl($ossKey)
{
$oss = new OSS();
$oss->ossClient->setBucket('提前设置好的Bucket的名称');
return $oss->ossClient->getUrl($ossKey, new \DateTime("+1 day"));
}
public static function createBucket($bucketName)
{
$oss = new OSS();
return $oss->ossClient->createBucket($bucketName);
}
public static function getAllObjectKey($bucketName)
{
$oss = new OSS();
return $oss->ossClient->getAllObjectKey($bucketName);
}
}