I want to download files from the S3 server to the EC2 server.

Asked 2 years ago, Updated 2 years ago, 139 views

I am developing it with laravel.

This may be a rudimentary question, but how do I download files from the S3 server to the EC2 server?
Can I download it as a byte array?

We are thinking of retrieving files from the S3 server, combining byte arrays at the end, and overwriting and saving the files to the S3 server again.

Thank you for your cooperation.

It will be added on April 18th.

Regarding the file retrieval from the S3 server to the EC2 server, we have responded with the following code.

class Media extensions Model
{
    public function getPreSignedUrl($minutes)
    {
        $url=\Storage::disk('s3')
            ->temporaryUrl(
                $this->file_name,
                Carbon::now()->addMinute($minutes));
        return$url;
    }
}

class MediaController extensions Controller
{
    public function uploadSplit (Request$request, Media$media)
    {
        $input=$request->all();
        
        $content=Storage:::disk('s3')->get($media->getPreSignedUrl(10));
        
        if(isset($content))
        {
            return [
                'message' = > 'ok',
            ];
        }
        
        return [
            'message' = > 'ng',
        ];
    }
}

I tried to do a binary operation using "$content", but from the middle

League\\Flysystem\\FileNotFoundException (code:0): File not found at path:

Errors such as this have occurred.
Before and after the error statement, there is a file download URL for the S3 server.

Is it not possible to obtain a temporary URL here?
Also, is it possible to treat the retrieved "$content" as a binary file?

This may be a rudimentary question, but I appreciate your cooperation.

php aws laravel amazon-ec2 amazon-s3

2022-09-29 22:02

1 Answers

Yes, Ravel can also read and write S3 data with raw data.

While AWS SDK for PHP is of course available, Ravel's File Storage feature is easy to read and write to local storage.

Please check the documentation for details, but roughly

  • Grant EC2 access to S3
      Configure
    • AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY accessible to S3 in
    • .env or attach S3 accessible roles to EC2
  • Add required package for Composer
    • composer require "league/flysystem-aws-s3-v3 to 1.0"
  • Get an instance of S3 and read/write
    Configure
  • AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY accessible to S3 in
  • .env or attach S3 accessible roles to EC2
  • composer require "league/flysystem-aws-s3-v3 to 1.0"
use Illuminate\Support\Facades\Storage;

$content=Storage:::disk('s3')->get('path/to/file');
Storage::disk('s3')->put('path/to/file',$content);

I will use it in this way.

If you only use S3 for storage, you can omit the disk('s3') specification if FILESYSTEM_DRIVER in .env is s3.

The definition of the s3 disk is located in config/filesystem.php, which is freely configurable, so you can also add s3-primary s3-secondary to move back and forth between multiple disks.


2022-09-29 22:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.