PHP - Upload video convert mp4 and upload to Amazon S3 - php

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
]);

Related

Upload URL image to s3

I'm trying to upload an image from an external url (another website) to my s3. this is what I've done so far.
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
// AWS Info
$bucketName = 'my-bucket';
$IAM_KEY = 'XXXXXXXXXXXX';
$IAM_SECRET = 'XXXXXXXXXXXX';
// Connect to AWS
try {
$s3 = new S3Client(
[
'credentials' => [
'key' => $IAM_KEY,
'secret' => $IAM_SECRET
],
'version' => 'latest',
'region' => 'ap-southeast-1'
]
);
} catch (Exception $e) {
die("Error: " . $e->getMessage());
}
$binary = file_get_contents('https://blabla.com/imgs.jpg');
$filename = '/storage/test.jpg';
$response = $s3->putObject(
[
'Bucket' => $bucketName,
'Key' => 'obj-'.ceil(rand(1,100000)),
'SourceFile' => $filename,
'Body' => $binary,
'ContentType' => 'image/jpg',
'StorageClass' => 'REDUCED_REDUNDANCY'
]
);
dd($response);
when i run the code, i get this error
Unable to open "test.jpg" using mode "r": fopen(test.jpg): Failed to open stream: No such file or directory
how do i upload to s3? Thank you.

AWS S3 URL is different from the original one

I am uploading files from php to aws s3. I have successfully uploaded the file.
The url it is returning is => https://BUCKETNAME.s3.ap-south-1.amazonaws.com/images1740/1550830121572.jpg
The actual url is => https://s3.ap-south-1.amazonaws.com/BUCKETNAME/images1740/1550830121572.jpg
(bucket name is coming in starting instead at the end of url)
Because of this it is giving me error while loading images => "Specified Key not found"
$source = $source;
$bucket = 'xxxxxxxxxxxxxxxxx';
$keyname = 'images'.$usr_id."/".$name;
// for push
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => "xxxxxxxxxxxxxx",
'secret' => "xxxxxxxxxxxxxxx"
),
'version' => 'latest',
'region' => 'ap-south-1'
)
);
try {
// Upload data.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $source,
'ServerSideEncryption' => 'AES256',
));
// Print the URL to the object.
print_r($result);
return $result['ObjectURL'] . PHP_EOL;
// print_r($result);
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
Set use_path_style_endpoint to true when initializing the S3 client to have it use the S3 path style endpoint by default when building the object URL. 1
Implementation details has the object URL to be in the path style if the bucket name makes a valid domain name otherwise it fallback to the S3 path style.
You want to keep the later behavior all the time.
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => "xxxxxxxxxxxxxx",
'secret' => "xxxxxxxxxxxxxxx"
),
'use_path_style_endpoint' => true,
'version' => 'latest',
'region' => 'ap-south-1'
)
);
You can also do as below if you wanted to disable it one-time for the PutObject operation.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $source,
'ServerSideEncryption' => 'AES256',
'#use_path_style_endpoint' => true
));

Delete object (single image) in Amazon S3 using php Yii2 not working?

Here I have the code of connection
$s3 = S3Client::factory(array(
'key' => 'xxxxxxxxx',
'secret' => '0xxxxxxxxxx',
'version' => 'latest',
'region' => 'us-west-2',
))
Here the bucket name $bucket = 'ariana-ios-storages'; and here the name of file $p = parse_url($images->name) which is the
name of file in amazon to delete;
Here is the code to delete the file
$delete = $s3->deleteObject([
'Bucket' => $bucket,
'Key' => $p['path']
]);
var_dump($delete);
exit();
The file path is generated like this;
It is not deleting the single file from the bucket so can any one help what i am missing
public function actionDeleteImages($id)
{
$exportImage= \common\models\ExportImages::findOne($id);
$base_url='base url to bucket';
$s3 = S3Client::factory(array(
'key' => 'xxxxxxx',
'secret' => '0pdfOx21xxxxxxxxxx',
'version' => 'latest',
'region' => 'us-west-2',
));
$bucket = 'name of bucket';
$key=urldecode(explode($base_url,$exportImage->name)[1]);// $images->name is Path from Db, base url is path to s3 server
$delete = $s3->deleteObject([
'Bucket' => 'ariana-ios-storages',
'Key' => $key
]);
$object_exist = $s3->doesObjectExist($bucket,$key);
if(!$object_exist){
return "succesfully deleted";
}else{
return "there is some problem while procesing the deletion";
}
}
}

In AWS S3 Bucket Uploading images as 0 Bytes

I am trying to upload images in AWS s3 bucket but all times its uploading 0 bytes in the bucket.
I really don't know why it's happening like this.
Anyone is here experienced the same issue
Here is the snapshot of image and code
https://www.awesomescreenshot.com/image/3369121/5ac921c28f569f45d1b51ac2ce7b6da1
use Aws\S3\S3Client;
public function aws_upload(){
require 'vendor/autoload.php';
if(isset($_FILES["left_front"]) && $_FILES["left_front"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["left_front"]["name"];
$filetype = $_FILES["left_front"]["type"];
$filesize = $_FILES["left_front"]["size"];
}
// Instantiate an Amazon S3 client.
$s3 = new S3Client([
'version' => 'latest',
'region' => AWS_REGION,
'credentials' => [
'key' => ACCESS_KEY_ID,
'secret' => SECRET_ACCESS_KEY
]
]);
$bucketName = 'demokrishna';
$key = basename($filename);
try {
$result = $s3->putObject([
'Bucket' => $bucketName,
'Key' => $key,
'acl' => 'public-read'
// 'ACL' => 'public-read',
]);
echo $result->get('ObjectURL');
} catch (Aws\S3\Exception\S3Exception $e) {
echo "There was an error uploading the file.\n";
echo $e->getMessage();
}
}
You have supplied bucket, key, and ACL but you have not supplied the file contents. You can do this using either a Body or a SourceFile.
Example with Body:
$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $key,
'Body' => 'Hello world!',
'ACL' => 'public-read'
]);
Example with SourceFile:
$file_Path = 'folder/filename.jpg';
$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $key,
'SourceFile' => $file_Path,
'ACL' => 'public-read'
]);
Also, note that it's 'ACL', not 'acl'. Please read the putObject documentation.
$key_bucket = basename($filename);
$result = $s3->putObject([
'Bucket' => $bucketName, // bucketname
'Key' => $key_bucket,
'StorageClass' => 'REDUCED_REDUNDANCY',
'SourceFile' => $value['tmp_name'] //temporary path
]);
It works for me

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
));

Categories