You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

51 lines
1.4 KiB

<?php
namespace App\Controller;
use OSS\Core\OssException;
use OSS\OssClient;
class FileUpload extends AbstractController
{
private $file;
private $path;
private $type;
public function __construct()
{
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
}
public function ossUpload($fileContent,$fileName)
{
$img = base64_decode($fileContent);
$accessKeyId = "LTAI4G8QMM97Fg1Gx8TXeast";
$accessKeySecret = "f1KIbMCiO4CXmTzC0FDl6hTRXgFobm";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
// 设置存储空间名称。
$bucket = "desion-test1";
// 设置文件名称。
$object = $fileName;
// 配置文件内容。
$content = $img;
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->createBucket($bucket);
$result = $ossClient->putObject($bucket, $object, $content);
return $result;
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return false;
}
}
}