ComurImageBundle icon indicating copy to clipboard operation
ComurImageBundle copied to clipboard

Compatibility with s3fs to upload to S3 bucket ...

Open sunandang opened this issue 8 years ago • 4 comments

Hi - This is not working if I want to upload images directly into an S3 bucket mounted on my EC2 instance using s3fs.

Have you tested it?

sunandang avatar Jul 19 '16 13:07 sunandang

I have tried that myself, but s3fs seems to be a bit laggy when you mount a bucket with a huge load of images. I wondered that it might affect server boot time and general performance.

That's why i chose to just do manual AWS S3 API upload inside the Controller. It works fast and not too many issues.

antonskv avatar Aug 23 '16 16:08 antonskv

@sunandang @antonskv Do you want to add a feature to directly bind the bundle to S3 ? I don't have any case to test so...

comur avatar May 23 '19 14:05 comur

With requirement of "aws/aws-sdk-php" in composer, you can do this inside upload controller. Obviously inside TRY and CATCH block to see if the upload went through. S3 Client will throw an error on putObject if something is wrong.

/* @var $uploaded_file \Symfony\Component\HttpFoundation\File\UploadedFile */
$uploaded_file = $request->files->get('uploadFile');

$s3key = $this->container->getParameter('comur_image.s3.aws_key');
$s3secret = $this->container->getParameter('comur_image.s3.aws_secret');
$bucket = $this->container->getParameter('comur_image.s3.bucket');
$uploadPath = $this->container->getParameter('comur_image.s3.path');

$client = \Aws\S3\S3Client::factory(['key' => $s3key, 'secret' => $s3secret]);

/** @NOTE: Or whatever other random mechanism to get long UID */
$newFileName = \hash('sha256',  \uniqid()) . ".{$uploaded_file->getClientOriginalExtension()}";
$fullPath = $uploadPath . "/{$newFileName}";

$client->putObject([
                'Bucket' => $bucket,
                'Key'    => $fullPath,
                'SourceFile' => $uploaded_file->getPathname(),
                'ContentType' => $uploaded_file->getMimeType()
            ]);

/** @NOTE: Clean up original file from temp upload directory */
if(\file_exists($uploaded_file->getPathname()))
    \unlink($uploaded_file->getPathname());

Why i commenting with a sample code response, is cause i never actually done a real code contribution to somebody elses' repo in github.... I wouldn't know how. I know it's kind of sad =) LOL

antonskv avatar May 23 '19 15:05 antonskv

Hi @antonskv Thx for your code. I don’t know if we can add some code with an optionally included package. I will take a look. For contributing, you just need to fork this repo, push your code on your fork and create a pull request on github from your fork to original repo. Tell me if you can do it so we can have you between contributors ;) Thx

comur avatar May 25 '19 11:05 comur