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
| <?php
require 'vendor/autoload.php';
use OSS\OssClient; use OSS\Core\OssException; use Aws\S3\S3Client; use Aws\Exception\AwsException; use Aws\S3\MultipartUploader; use Aws\Exception\MultipartUploadException;
$config = [ 'huawei' => [ 'credentials' => [ 'key' => '3R9RMYDLWTSWUTL8SZ0C', 'secret' => 'iEgSGsHongfsxJjXFS4X5ixxPxR6TSeBxYYlfPNC', ], 'bucket' => 'hongfs-backup-wc0dl2nipqz68tjr', 'endpoint' => 'https://obs.cn-south-1.myhuaweicloud.com', 'region' => 'cn-south-1', 'version' => '2006-03-01', ], 'tencent' => [ 'credentials' => [ 'key' => 'xx', 'secret' => 'xx', ], 'bucket' => 'hongfs-backup-xx-1252156936', 'endpoint' => 'https://cos.ap-guangzhou.myqcloud.com', 'region' => 'ap-guangzhou', 'version' => '2006-03-01', ], ];
function handler($event, $context) { global $config;
$event = json_decode($event, true)['events'][0];
$oss = new OssClient( $context['credentials']['accessKeyId'], $context['credentials']['accessKeySecret'], 'https://oss-' . $event['region'] . '-internal.aliyuncs.com', false, $context['credentials']['securityToken'] );
$object_key = $event['oss']['object']['key'];
$filepath = '/tmp/' . $context['requestId'] . '-' . basename($object_key);
try { $oss->getObject($event['oss']['bucket']['name'], $object_key, [ OssClient::OSS_FILE_DOWNLOAD => $filepath, ]); } catch (OssException $e) { echo $e->getMessage() . "\n"; return '0'; }
foreach($config as $item) { $uploader = new MultipartUploader(new S3Client($item), $filepath, [ 'bucket' => $item['bucket'], 'key' => $object_key, ]);
try { $uploader->upload(); } catch (MultipartUploadException $e) { echo $e->getMessage() . "\n"; } }
unset($filepath);
return '1'; }
|