Upload file from url to AWS in laravel - php

I know how to upload file from local storage to aws using laravel. But I want to upload file directly from external url to aws without downloading.
Any suggestion, how can I achieve this.

I finally solved this using Intervention Image Library.
use Image;
use Storage;
$image = Image::make('url');
$image->encode('jpg');
$s3 = Storage::disk('s3');
$filePath = '/profilePhotos/'.$time();
$s3->put($filePath, $image->__toString(), 'public');
Installation instructions for Image library can be found here in the "Integration in Laravel" section.

The accepted answer is dependent on another library. You can do it without the Intervention Image Library. Here is how you can do it-
$url = 'https://remote.site/photo/name.jpg'
$contents = file_get_contents($url);
$name = substr($url, strrpos($url, '/') + 1);
Storage::put($name, $contents);

Related

Can't write image data to path - Intervention Image

I'm trying to resize my images using Intervention package in my laravel project. I'm able to store images using Storage facade, but when I store them with Image class it throws below error.
Can't write image data to path
(/var/www/commerce/storage/featureds/2-1537128615.jpg)
if ($request->has("featured_img")) {
$file_path = $request->get("featured_img");
$exp = explode("/",$file_path);
$filename = $exp[1];
$featured_path = "featureds/".$filename;
\Storage::copy($request->get("featured_img"), $featured_path);
\Image::make(\Storage::get($featured_path))->fit(400, 300)->save(storage_path($featured_path));
$product->media()->where("type", "featured")->update(["path" => $featured_path]);
}
How can I fix this ? Thanks for all your helps
Is there featureds directory in storage?
If no, you use mkdir to create directory because Intervention don't create directory automatically.

Move images from one server to s3 PHP Laravel 5.3

I'm trying to write a script where I'm trying to move images from an old server to Amazon s3. Is it possible to do by downloading the image from an url?
$companies = Company::all();
$companies->each( function ($company) {
//some method to download file
$file = download($company->logo);
//Store on s3
$filename = $file->storeAs('images', uniqid('img_') . "." . $file->guessClientExtension(),'s3');
//Get the new path
$new_path = Storage::disk('s3')->url($filename);
//save the new path to logo
$company->logo = $new_path;
//save the new path
$company->save();
}
You can use this library Php League FileSystem
It has an integration for laravel and other framewoks such as zend, Cake etc
The library has an adapter for amazon S3 V2 and amazon s3 V3
Full documentation here

Download .mp4 from URL using Laravel

How to download .m4v video from given URL with Laravel? I have a URL for example rtmp://123456.r.cdnsun.net/_definst_/mp4:123456/h264/123456.m4v ... Can you recommend me a package for Laravel which could make the process easier?
It depends on what exactly you're trying to achieve. For example, if you want to just copy file from a remote server, you could use copy():
$remoteFile = 'rtmp://123456.r.cdnsun.net/_definst_/mp4:123456/h264/123456.m4v';
$localFile = storage_path().'movies';
copy($remoteFile, $localFile);
You can use file_put_contents to download video from URL
$video_name="video_name.mp4";
$video_path=public_path('videos').'/'.$video_name; // path where you want to store video
file_put_contents($video_path, fopen($file, 'r'));

How to store base 64 decoded image to amazon s3 bucket in php

I was trying to store base 64 decoded image to amazon s3 bucket in php here is my code
$data = base64_decode($pro_img_nm);
$camp_name = "name";
$bucketName = "bucket";
$file = $camp_name.uniqid().'.png';
$image = imagecreatefromstring($data);
header('Content-Type: image.png');
imagepng($image,'folder/'.$file);
$s3->putObjectFile($image, $bucketName, "folder/".$file, S3::ACL_PUBLIC_READ)
and image getting tored on local because of this (imagepng()) but failed to store on s3 bucket error as "S3::inputFile(): Unable to open input file " can anyone help me on this. Thanks
Finally i found the solution, the error is occurred because of the version i used the version we need to use v3 this is the link that we should follow.
http://docs.aws.amazon.com/aws-sdk-php/v3/guide/

PHP AWS SDK Change_content_type not successful

I am attempting to change the content type of a file after is had been uploaded to AWS, from it's default to an image/jpeg. I am using the Amazon Web Service Bundle in my symfony2 project. I got this code snippet from the docs.
$bucket = $awsConnectionParams['aws_s3_bucket'];
$s3 = $this->container->get('aws_s3');
$cct = $s3->change_content_type($bucket, $filePath, 'image/jpeg');
sleep(5);
$metadata = $s3->get_object_metadata($bucket, $filePath);
// Success?
var_dump($cct->isOK());
var_dump($metadata['ContentType']);
die;
The output is:
bool(false)
NULL
The file path is correct as I use it to construct a "read file" url later on, with my poor image as a forced download.
By outputting the response body:
var_dump($cct->body);
I was able to see the actual failure message which was
Check your AWS Secret Access Key and signing method.
I had to trim a "/" off my file path for it to authenticate, the following worked.
$filePath = ltrim($filePath, '/');
$s3 = $this->container->get('aws_s3');
$s3->change_content_type($bucket, $filePath, 'image/jpeg');

Categories