function s3_file_import_init($import, $config) { $response = array(); $err = ''; if (!$err) { $response['api'] = array(); $response['data'] = array(); $response['method'] = 'put'; $id = id_unique(); $bucket = $config['s3_bucket']; $region = $config['s3_region'] ? $config['s3_region'] : 'us-east-1'; $key = path_strip_slash_start($config['user_media_directory']); // remove bucket name from url if it's there if (substr($key, 0, strlen($bucket)) == $bucket) { $key = substr($key, strlen(path_add_slash_end($bucket))); } $key = path_concat(path_concat(path_concat($key, $import['uid']), $id), $config['import_original_basename'] . '.' . $import['extension']); $options_array = ['version' => 'latest', 'region' => $region]; if ($config['aws_secret_access_key'] && $config['aws_access_key_id']) { $options_array['credentials'] = []; $options_array['credentials']['key'] = $config['aws_access_key_id']; $options_array['credentials']['secret'] = $config['aws_secret_access_key']; } try { $s3 = new Aws\S3\S3Client($options_array); $cmd = $s3->getCommand('PutObject', ['Bucket' => $config['s3_bucket'], 'Key' => $key, 'ACL' => $config['s3_acl'], 'ContentType' => $import['mime']]); $request = $s3->createPresignedRequest($cmd, $config['s3_expires']); // Get the actual presigned-url $response['endpoint'] = $presignedUrl = (string) $request->getUri(); } catch (Exception $e) { $err = $e->getMessage(); } // data key children all become part of the POST file upload request $response['api']['id'] = $id; $response['api']['type'] = $import['type']; $response['api']['extension'] = $import['extension']; $response['api']['label'] = $import['label']; } if ($err) { $response['error'] = $err; } return $response; }
function path_strip_slashes($s) { return path_strip_slash_start(path_strip_slash_end($s)); }
function __s3_key($id, $import, $config, $prefix = 's3') { $bucket = $config[$prefix . '_bucket']; $key = path_strip_slash_start($config['user_media_directory']); // remove bucket name from url if it's there if (substr($key, 0, strlen($bucket)) == $bucket) { $key = substr($key, strlen(path_add_slash_end($bucket))); } $key = path_concat(path_concat(path_concat($key, $import['uid']), $id), $config['import_original_basename'] . '.' . $import['extension']); return $key; }