Showing blank image when uploading to AWS S3 - php

I'm learning how to use Amazon for storing images and have gotten to the point I can upload a file with PHP. The problem is when I upload an image it shows as a blank image like it didn't upload. Here's what I've got:
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'Body' => $filepath,
'ContentType' => 'image/jpeg',
'ACL' => 'public-read',
'StorageClass' => 'STANDARD',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
Here's the image it uploaded (which doesn't help much): https://motorcyclealbum.s3.amazonaws.com/test.jpg
Solved
So, 'Body' needed to be 'SourceFile' if uploading an image. I'm a dork.

So, 'Body' needed to be 'SourceFile' if uploading an image. I'm a dork.

Related

PHP - Upload video convert mp4 and upload to Amazon S3

I'm using amazon s3 as video storage for my website. I'm having problems for some videos. black screen or sound problems etc.
I want to convert the video to mp4 format after uploading the video to my server and then upload it to amazon. Is it possible with FFMPEG?
I'm using this code for uploading files now:
$file1 = $_FILES['file']['name'];
$videoFileType = strtolower(pathinfo($file1,PATHINFO_EXTENSION));
$file_name = sprintf('%s_%s', uniqid(),uniqid().".".$videoFileType);
$temp_file_location = $_FILES["file"]["tmp_name"];
require 'application/libraries/Amazon/aws-autoloader.php';
$s3 = new Aws\S3\S3Client([
'region' => $amazon_region,
'version' => 'latest',
'credentials' => [
'key' => $amazon_key,
'secret' => $amazon_secret,
]
]);
$result = $s3->putObject([
'Bucket' => $amazon_bucket,
'Key' => $file_name,
'SourceFile' => $temp_file_location,
'ACL' => 'public-read',
'CacheControl' => 'max-age=3153600',
]);
$filepath = $result['ObjectURL'] . PHP_EOL;
echo json_encode([
'status' => 'ok',
'path' => $filepath
]);

AWS PHP Upload cannot upload files larger than 15mb

I have been able to upload files to my bucket using the below code from the AWS PHP SDK. The problem is when I try to upload files that are larger than 15 mb, the script throws an error. Otherwise it works as expected. Any Ideas what I'm doing wrong? Thanks in advance.
require $dir . 'aws/aws-autoloader.php';
$s3Client = new Aws\S3\S3Client(array(
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => array(
'key' => 'KEY',
'secret' => 'SECRET',
)
));
$result = $s3Client->putObject(array(
'Bucket' => 'eot-resources',
'Key' => $org_id."_".$_FILES["fileToUpload"]["name"],
'SourceFile' => $_FILES["fileToUpload"]["tmp_name"],
'Body' => new GuzzleHttp\Psr7\Stream(fopen($_FILES["fileToUpload"]["tmp_name"], 'r')),
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'Foo' => 'abc',
'Baz' => '123'
)
));
echo "URL: ".$result['ObjectURL'] . "<br>";
I get the following error when trying to upload a large file bigger than 15mb. Other than that it works.
Warning: fopen(): Filename cannot be empty in /Users/xxx/Code/xxx/wp-content/plugins/xxx/parts/part-upload_file.php on line 37.
Line 37 reads..
'Body' => new GuzzleHttp\Psr7\Stream(fopen($_FILES["fileToUpload"]["tmp_name"], 'r')),
Any help/advice/tips would be appreciated.
I suspect your server has a limit on the size of file that can be uploaded. Check using phpinfo() and look for post_max_size and upload_max_filesize. Assuming that's the problem then you can increase the limit.

Amazon AWS s3 upload using image content - PHP

I am using the following code to upload image to the ECS server. Here I am first storing the image first to a temporary location and then uploading that to the server.
$result = $s3->putObject(array(
'Bucket' => $this->bucket,
'SourceFile' => $temp,
'Key' => $Destination_folder,
'ACL' => 'public-read',
'ContentType' => 'text/plain',
'Expires' => $expire
));
I want to remove the use of this temporary location. So is there any way to upload image directly using the image content only.
Try using 'Body' of s3 putObject for image content.
$result = $s3->putObject(array(
'Bucket' => $this->bucket,
'Key' => $Destination_folder,
'Body' => $image_content
));

Upload to AWS S3 failing

I'm trying to upload a file from my Wordpress application to a S3 bucket by Ajax:
Somehow, I don't get an answer and the script fails with a 500 error when applying the 'putObject' method.
app/ajax.php
require_once 's3/start.php'
//wp_die(var_dump($s3)); Seems to be fine
$upload = $s3->putObject([
'Bucket' => $config['s3']['bucket'],
'Key' => 'video,
'Body' => fopen( $_FILES['file']['tmp_name'], 'r' ),
'ACL' => 'public-read',
]);
if ($upload) {
wp_die('Uploaded');
} else {
wp_die('Upload Error');
}
app/s3/start.php
use Aws\S3\S3Client;
require 'aws/aws-autoloader.php';
$config = require('config.php');
$s3 = new S3Client([
'key' => $config['s3']['key'],
'secret' => $config['s3']['secret'],
'region' => $config['s3']['region'],
'version' => 'latest',
]);
app/s3/aws
Latest version of the official AWS SDK for PHP
SOLUTION
The credentials in app/start.php where not assigned correctly when initialising the $s3 object. That's how it must look like
$s3 = S3Client::factory([
'region' => $config['s3']['region'],
'version' => 'latest',
'credentials' => [
'key' => $config['s3']['key'],
'secret' => $config['s3']['secret']
]
]);
If you upload a file, you should use SourceFile instead of Body.
Example code:
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));
More info from here - http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpPHP.html

PutObject into directory Amazon s3 / PHP

I need to upload my files inside specific directories that I created on my amazon s3 storage. I always uploaded the files on the "absolute path" of my bucket doing something like so:
$s3->putObject(array(
'Bucket' => $bucket,
'ContentType' => $mime,
'Key' => $localImage,
'ACL' => 'public-read',
'SourceFile' => $localImage,
'CacheControl' => 'max-age=172800',
"Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years")),
'Metadata' => array(
'profile' => $localImage,
),
));
How can I define where this file should be uploaded on a given directory?
You must include that information in the "Key" parameter. S3 isn't actually a filesystem, it's more like a big (hash table) associative array. The "Bucket" is the name of the hash table, and the "Key" is the key (e.g., $bucket[$key] = $content). So all path/directory information must be a part of the "Key".
$localImage = '/Users/jim/Photos/summer-vacation/DP00342654.jpg';
$s3->putObject(array(
'Bucket' => 'my-uniquely-named-bucket',
'SourceFile' => $localImage,
'Key' => 'photos/summer/' . basename($localImage)
));
thank you Jeremy Lindblom, this is my python example that worked for me.
import boto3
s3 = boto3.resource('s3')
data = open('/home/briansanchez/www/red-hat.jpg', 'rb')
s3.Bucket('briansanchez').put_object(Key='www/red-hat.jpg', Body=data)
Updated code according to the latest SDK of AWS:-
$result = $s3->putObject(array(
'Bucket' => 'bucket name of S3',
'Key' => 'pawan-trying',
'SourceFile' => 'local image path or document root image path ',
'ContentType' => 'image',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY',
'Metadata' => array(
'param1' => 'value 1',
'param2' => 'value 2'
)
));

Categories