1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| if(!function_exists('s3_upload')) {
function s3_upload($local_path, $cloud_path) { $config = [ 'credentials' => [ 'key' => config('site.os_access_key_id'), 'secret' => config('site.os_access_key_secret'), ], 'bucket' => config('site.os_bucket'), 'endpoint' => config('site.os_endpoint'), 'region' => config('site.os_region'), 'version' => '2006-03-01', ];
if(strpos($cloud_path, '/') === 0) { $cloud_path = substr($cloud_path, 1); }
try { $uploader = new \Aws\S3\MultipartUploader(new \Aws\S3\S3Client($config), $local_path, [ 'bucket' => $config['bucket'], 'key' => $cloud_path, ]);
$uploader->upload(); } catch (\Aws\Exception\MultipartUploadException $e) { unlink($local_path);
throw $e; }
if(true) { unlink($local_path); }
return s3_url($cloud_path); } }
if(!function_exists('s3_url')) {
function s3_url($path) { $config = [ 'bucket' => config('site.os_bucket'), 'endpoint' => config('site.os_endpoint'), 'domain' => config('site.os_domain'), ];
if(strpos($path, '/') !== 0) { $path = '/' . $path; }
$path = parse_url($path, PHP_URL_PATH);
if($config['domain']) { return $config['domain'] . $path; }
$host = parse_url($config['endpoint'], PHP_URL_HOST);
if(strpos($config['endpoint'], '.myqcloud.com') !== false) { return sprintf('https://%s.%s%s', $config['bucket'], $host, $path); }
return sprintf('https://%s%s', $host, $path); } }
if(!function_exists('s3_has')) {
function s3_has($path) { $url = s3_url($path);
if($url === false) { return false; }
$header = get_headers($url, true);
return strpos($header[0], '200') !== false; } }
|