sapatos
sapatos

Reputation: 1304

AWS php putObject to S3 from form $_FILE[] multipart upload

I'm uploading up to 4 images from a multipart form to an apache server. I'm currently migrating to Amazon S3 for storage and I'm wondering on the best option.

1) My php script currently uploads an image firstly as a temporary file to the Ec2 server, for example named phpwG3oTb. I would then normally move_uploaded_file to rename it and move it to the correct directory. Using AWS PHP SDK for S3 I can see I could avoid this moving around and upload it directly using putObject as:

$result = $this -> s3Client -> putObject(array('Bucket' => $this -> bucket, 'Key' => 'file_1', 'SourceFile' => 'phpwG3oTb', 'ACL' => 'public-read);

However the file on the S3 bucket now is called phpwG3oTb with a key of file_1. The url provided for this file is https://url.amazonaws.com/site-dev/folder_1/phpwG3oTb

When I click on this the image is downloaded and not shown in the browseer as, I guess, it has no mime_type. If i upload the same image as image.jpg it works fine.

In order to avoid this I need to rename the locally uploaded file to image.jpg and then use putObject. I'd like to avoid this step.

2) Next one is perhaps instead of taking this $_FILE[] of files and uploading it to the Ec2 server and then calling putObject. Perhaps I can take the file directly from the array memory and write it to putObject some how. I'm not sure how that could. I'm thinking in pseudo code of something like:

$result = $this -> s3Client -> putObject(array('Bucket' => $this -> bucket, 'Key' => 'file_1', 'SourceFile' => $_FILE["<ref_file>"], 'ACL' => 'public-read);

Upvotes: 1

Views: 1375

Answers (1)

datasage
datasage

Reputation: 19563

You can do what you did in the first one. Without the extension it does not correctly detect the content type.

If you know what the content type of the file is suppose to be, you can set it manually using 'ContentType' => 'image/jpeg' as part of your putObject parameters.

Upvotes: 1

Related Questions