代码:
try {
$setting = [
‘oss_key’ => ‘xxx’,
‘oss_secret’ => ‘xxx’,
‘oss_bucket’ => ‘xxx’,
];
if (empty($setting[‘oss_key’]) || empty($setting[‘oss_secret’]) || empty($setting[‘oss_bucket’])) {
print(‘oss未配置’);die;
}
$endpoint = ‘http://oss-cn-‘.empty($setting[‘oss_endpoint’]) ? ‘shanghai’ : $setting[‘oss_endpoint’].’.aliyuncs.com’;
$ossClient = new \OSS\OssClient($setting[‘oss_key’], $setting[‘oss_secret’], $endpoint);
// 临时文件位置
$tmpFile = $_FILES[‘image’][‘tmp_name’];
// 定义文件存储的oss位置
$ossPath = date(‘yndhis’).mt_rand(10000,99999);
// 定义oss object
$object = $ossPath . ‘.’ . $ext;
$info = $ossClient->uploadFile($setting[‘oss_bucket’], $object, $tmpFile);
} catch (\OSS\Core\OssException $e) {
print ($e->getMessage());
}
原因:
uploadFile()传本地文件的
解决:
1.先把数据保存到本地再上传
2.HttpRequest传base64,使用putObject()方法,用来直接传内存数据