今天 Q 群有朋友說(shuō)不會(huì) OSS 直傳,那我就來(lái)寫個(gè)例子吧。歡迎兄弟們探討交流。
composer require aws/aws-sdk-php
$s3client = new \Aws\S3\S3Client([
'credentials' => [
'key' => 'KEY',
'secret' => 'SK',
],
'region' => 'oss-cn-chengdu',
'version' => '2006-03-01',
'endpoint' => 'https://caylof.oss-cn-chengdu.aliyuncs.com',
'use_path_style_endpoint' => true,
// 'endpoint' => 'https://oss-cn-chengdu.aliyuncs.com',
// 'addressing_style' => 'virtual',
]);
通常前端先上傳到服務(wù)器,然后通過(guò)服務(wù)器再上傳到OSS,即中傳了一次。
function put(\Aws\S3\S3Client $s3client): void
{
$bucket = 'a';
$key = '123.txt';
$s3client->putObject([
'Bucket' => $bucket,
'Key' => $key,
'Body' => file_get_contents(__DIR__.'/1.php'),
'ContentType' => 'text/plain',
]);
}
function buildForm(\Aws\S3\S3Client $s3client): array
{
$bucket = 'a/123.txt'; // 這里阿云的兼容似乎有點(diǎn)別扭,用真正的bucket會(huì)有問(wèn)題
$key = 'a/123.txt';
$maxUploadSize = 1024 * 1024 * 10;
$contentType = 'text/plain';
$formInputs = ['acl' => 'private'];
$options = [
['acl' => 'private'],
['bucket' => $bucket],
['key' => $key],
['Content-Type' => $contentType],
['content-length-range', 1, $maxUploadSize],
];
$expires = '+10 minutes';
$postObject = new \Aws\S3\PostObjectV4(
$s3client,
$bucket,
$formInputs,
$options,
$expires
);
$formAttributes = $postObject->getFormAttributes();
$formInputs = $postObject->getFormInputs();
$formInputs['key'] = $key;
$formInputs['Content-Type'] = $contentType;
// print_r($formAttributes);
// print_r($formInputs);
return [$formAttributes, $formInputs];
}
返回的 $formAttributes
和 $formAttributes
就是前端需要的表單屬性和表單輸入,然后用于前端進(jìn)行構(gòu)造表單并提交即可。
我這里不采用JS(通常會(huì)是axios)來(lái)構(gòu)造表單,而是用 PHP 的 guzzle client 來(lái)模擬前端實(shí)現(xiàn)。
function uploadMock(array $formAttributes, array $formInputs): void
{
$multipart = [];
foreach ($formInputs as $name => $value) {
$multipart[] = [
'name' => $name,
'contents' => $value,
];
}
$multipart[] = [
'name' => 'file',
'contents' => file_get_contents(__DIR__.'/1.php'),
];
$http = new \GuzzleHttp\Client();
$resp = $http->put($formAttributes['action'], [
'multipart' => $multipart,
'headers' => [
'Accept' => 'application/json',
],
]);
echo $resp->getStatusCode() . PHP_EOL;
print_r($resp->getBody()->getContents());
}
$s3client = new \Aws\S3\S3Client([
'credentials' => [
'key' => 'KEY',
'secret' => 'SK',
],
'region' => 'auto',
'version' => 'latest',
'endpoint' => 'https://xxx-yyy.cos.ap-chengdu.myqcloud.com',
'use_path_style_endpoint' => true,
]);
服務(wù)器上傳文件同上阿里云。
function buildForm(\Aws\S3\S3Client $s3client): array
{
$bucket = 'xxx-yyy'; // 這里騰訊云看起來(lái)比阿里云做的合理
$key = 'a/123.txt';
$maxUploadSize = 1024 * 1024 * 10;
$contentType = 'text/plain';
$formInputs = ['acl' => 'private'];
$options = [
['acl' => 'private'],
['bucket' => $bucket],
['key' => $key],
['Content-Type' => $contentType],
['content-length-range', 1, $maxUploadSize],
];
$expires = '+10 minutes';
$postObject = new \Aws\S3\PostObjectV4(
$s3client,
$bucket,
$formInputs,
$options,
$expires
);
$formAttributes = $postObject->getFormAttributes();
$formInputs = $postObject->getFormInputs();
$formInputs['key'] = $key;
$formInputs['Content-Type'] = $contentType;
// print_r($formAttributes);
// print_r($formInputs);
return [$formAttributes, $formInputs];
}
function uploadMock(array $formAttributes, array $formInputs): void
{
$multipart = [];
foreach ($formInputs as $name => $value) {
$multipart[] = [
'name' => str_replace('X-Amz', 'X-Cos', $name), // 騰訊云對(duì)名稱做了替換處理,一個(gè)小細(xì)節(jié)
'contents' => $value,
];
}
$multipart[] = [
'name' => 'file',
'contents' => file_get_contents(__DIR__.'/1.php'),
];
$http = new \GuzzleHttp\Client();
$resp = $http->put($formAttributes['action'], [
'multipart' => $multipart,
'headers' => [
'Accept' => 'application/json',
],
]);
echo $resp->getStatusCode() . PHP_EOL;
print_r($resp->getBody()->getContents());
}
~ over ~
aws不是亞馬遜的包嗎?