Laravel 文件上传 s3 Multipart

问题描述 投票:0回答:1

如何集成s3的分段上传?我正在上传到 s3,一切正常。只是我想重构代码为S3分段上传,因为服务器上的文件太大了

//    Amazon checking folder 
$directory  = 'Case/'. $caseDir;           
foreach ($request->file('fileslab') as $s3file) {
    // Getting request names & extension
    $s3patientFirstName = $request->patient_firstname;
    $s3patientLastName = $request->patient_lastname;
    $s3SavedOrigName = $s3file->getClientOriginalName();
    $SendFileToS3 = $s3patientFirstName . '_' . $s3patientLastName . '_' . time() . $s3SavedOrigName;
    $contents = file_get_contents($dbfile->getRealPath());
    $path = Storage::disk('s3')->put($directory. '/' .$SendFileToS3, $contents);
    
    if (!Storage::disk('s3')->exists($directory)){
       Storage::disk('s3')->makeDirectory($directory);
       $path = Storage::disk('s3')->put( $directory. '/' .  $SendFileToS3, $contents );
                
    }else{
       $path = Storage::disk('s3')->put( $directory. '/' .$SendFileToS3, $contents);
}

Amazon SK 示例如下:

 require 'vendor/autoload.php';

use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\MultipartUploader;
use Aws\S3\S3Client;

$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';
                        
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1'
]);
 
// Prepare the upload parameters.
$uploader = new MultipartUploader($s3, '/path/to/large/file.zip', [
    'bucket' => $bucket,
    'key'    => $keyname
]);

// Perform the upload.
try {
    $result = $uploader->upload();
    echo "Upload complete: {$result['ObjectURL']}" . PHP_EOL;
} catch (MultipartUploadException $e) {
    echo $e->getMessage() . PHP_EOL;
}
laravel amazon-web-services amazon-s3
1个回答
5
投票

我修好了。

foreach ($request->file('fileslab') as $s3file) {
  $directory  = 'Case/'. $caseDir;
  $contents = fopen($s3file, 'rb');
  $s3patientFirstName = $request->patient_firstname;
  $s3patientLastName = $request->patient_lastname;
  $s3SavedOrigName = $s3file->getClientOriginalName();
  $SendFileToS3 = $s3patientFirstName . '_' . $s3patientLastName . 
    '_' . time() . $s3SavedOrigName;
  $disk = Storage::disk('s3');

  $s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-west-1'
  ]);

  $uploader = new MultipartUploader($s3, $contents, [
    'bucket' => $_ENV['AWS_BUCKET'],
    'key'    => $SendFileToS3,
  ]);
    
  try {
    $result = $uploader->upload();               
  } 
  catch (MultipartUploadException $e) {
    return $e->getMessage();
  }
}               
© www.soinside.com 2019 - 2024. All rights reserved.