0

我可以使以下代码正常工作:

require_once('lib/php/AWSSDKforPHP/aws_v3.phar');
//Set-up AWS Objects
use Aws\Credentials\CredentialProvider;
use Aws\S3\S3Client;
use Aws\S3\MultipartUploader;
use Aws\Exception\MultipartUploadException;
use Aws\S3\Exception\S3Exception;
$CREDENTIALS_PROFILE = 'default';
$CREDENTIALS_FILE = <credentials file location here>;

$IAM_CREDENTIALS = CredentialProvider::ini($CREDENTIALS_PROFILE, $CREDENTIALS_FILE);
$IAM_CREDENTIALS = CredentialProvider::memoize($IAM_CREDENTIALS);

// Instantiate the client with the first credential set
$AWS_S3_OBJECT = new S3Client([
        'version' => 'latest',
        'region'  => 'eu-west-1',
        'credentials' => $IAM_CREDENTIALS
]);

$server_file = $SOME_DIR.'video.MP4';

// Prepare the upload parameters.
$uploader = new MultipartUploader($AWS_S3_OBJECT, $server_file, [
        'bucket' => 'test-bucket',
        'key'    => 'Large_File_Test'
]);

// Perform the upload.
try {
    $result = $uploader->upload();
    echo "Upload complete: {$result['ObjectURL']}" . PHP_EOL;
    //return true;
} catch (MultipartUploadException $e) {
    logError($e->getMessage().PHP_EOL);
    //return false;
}

但是,当我尝试创建一个可用于我的所有脚本的 Multiuploader 通用方法时,我收到以下错误:

PHP Fatal error:  Uncaught Error: Class 'MultipartUploader' not found in.....

例如,我正在尝试做这样的事情,但它给出了上述错误!

require_once('lib/php/AWSSDKforPHP/aws_v3.phar');
//Set-up AWS Objects
use Aws\Credentials\CredentialProvider;
use Aws\S3\S3Client;
use Aws\S3\MultipartUploader;
use Aws\Exception\MultipartUploadException;
use Aws\S3\Exception\S3Exception;
$CREDENTIALS_PROFILE = 'default';
$CREDENTIALS_FILE = <credentials file location here>;

$IAM_CREDENTIALS = CredentialProvider::ini($CREDENTIALS_PROFILE, $CREDENTIALS_FILE);
$IAM_CREDENTIALS = CredentialProvider::memoize($IAM_CREDENTIALS);

// Instantiate the client with the first credential set
$AWS_S3_OBJECT = new S3Client([
        'version' => 'latest',
        'region'  => 'eu-west-1',
        'credentials' => $IAM_CREDENTIALS
]);

$server_file = $SOME_DIR.'video.MP4';

// Prepare the upload parameters.
$uploader = new MultipartUploader($AWS_S3_OBJECT, $server_file, [
        'bucket' => 'test-bucket',
        'key'    => 'Large_File_Test'
]);

require_once('functions.php');

//Following not working as the 'MultipartUploader' class is not being seen in the method....
if(upload_S3_file_large('test-bucket', 'Large_File_Test', $server_file)){
    echo "Job Done";
}else{
    echo "Error logged";
}

functions.php 看起来像这样:

/**
 * Load a Large >2GB file into S3 storage 
 *
 * @param string    $AWS_BUCKET Name of AWS Bucket
 * @param string    $AWS_FILE_NAME  Name of file on AWS
 * @param string    $SYS_FILE_NAME  Name of file on local system
 *
 * @return boolean true on success, false on failure
 */
function upload_S3_file_large($AWS_BUCKET, $AWS_FILE_NAME, $SYS_FILE_NAME){
    global $AWS_S3_OBJECT;
    // Prepare the upload parameters.
    $uploader = new MultipartUploader($AWS_S3_OBJECT, $SYS_FILE_NAME, [
            'bucket' => $AWS_BUCKET,
            'key'    => $AWS_FILE_NAME
    ]);
    
    // Perform the upload.
    try {
        $result = $uploader->upload();
        return true;
    } catch (MultipartUploadException $e) {
        logError($e->getMessage().PHP_EOL);
        return false;
    }
}

我假设它是一个范围的事情,但此刻我对我做错了什么感到困惑......

4

0 回答 0