I'm using a NeutrinoAPI API for watermarking images (https://www.neutrinoapi.com/api/image-watermark/)
That's working just fine, but now I need to upload the watermarked image to Amazon S3, which I use this code (https://gist.github.com/keithweaver/70eb06d98b008113ce97f6148fbea83d)
As the first API the response is a image, I don't know how to use it on the AWS API. That's how I'm doing, but it keeps uploading a 0kb file.
// Add it to S3
try {
// Uploaded:
$file = $_FILES[$json];
$response = $s3->putObject(
array(
'Bucket'=>$bucketName,
'Key' => $keyName,
'ACL' => 'public-read',
'SourceFile' => $file
)
);
When I use the 'file_put_contents($filename, $json);' it works, it exports the image I want, but how do I put it on the amazon $file?
Related
I'm able to upload a file to google drive with that code:
//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setName(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');
$data = file_get_contents('a.jpg');
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart'
));
But it will be counted in the quota. How to send it to free storage like google photo ?
If you want to use the Google Photos, you should use the Google Photos APIs upload media.
Note: All media items uploaded to Google Photos through the API are stored in full resolution at original quality. If your uploads exceed 25MB per user, your application should remind the user that these uploads will count towards storage in their Google Account.
Bytes are uploaded to Google using upload requests. If the upload request is successful, an upload token which is in the form of a raw text string, is returned. To create media items, use these tokens in the batchCreate call.
// Create a new upload request
// Specify the filename that will be shown to the user in Google Photos
// and the path to the file that will be uploaded
// When specifying a filename, include the file extension
try {
$uploadToken = $photosLibraryClient->upload(file_get_contents($localFilePath), $filename);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
// Handle error
}
Laravel 5.5 app. I need to retrieve images of driver's licenses from Amazon S3 (already working) and then upload it to Stripe using their api for identity verification (not working).
Stripe's documents give this example:
\Stripe\Stripe::setApiKey(PLATFORM_SECRET_KEY);
\Stripe\FileUpload::create(
array(
"purpose" => "identity_document",
"file" => fopen('/path/to/a/file.jpg', 'r')
),
array("stripe_account" => CONNECTED_STRIPE_ACCOUNT_ID)
);
However, I am not retrieving my files using fopen().
When I retrieve my image from Amazon S3 (using my own custom methods), I end up with an instance of Intervention\Image -- essentially, Image::make($imageFromS3) -- and I don't know how to convert this to the equivalent of the call to fopen('/path/to/a/file.jpg', 'r'). I have tried the following:
$image->stream()
$image->stream()->__toString()
$image->stream('data-url')
$image->stream('data-url')->__toString()
I have also tried skipping intervention image and just using Laravel's storage retrieval, for example:
$image = Storage::disk('s3')->get('path/to/file.jpg');
All of these approaches result in getting an Invalid hash exception from Stripe.
What is the proper way to get a file from S3 and convert it to the equivalent of the fopen() call?
If the file on S3 is public, you could just pass the URL to Stripe:
\Stripe\FileUpload::create(
array(
"purpose" => "identity_document",
"file" => fopen(Storage::disk('s3')->url($file)),
),
array("stripe_account" => CONNECTED_STRIPE_ACCOUNT_ID)
);
Note that this requires allow_url_fopen to be turned on in your php.ini file.
If not, then you could grab the file from S3 first, write it to a temporary file, and then use the fopen() method that the Stripe documentation speaks of:
// Retrieve file from S3...
$image = Storage::disk('s3')->get($file);
// Create temporary file with image content...
$tmp = tmpfile();
fwrite($tmp, $image);
// Reset file pointer to first byte so that we can read from it from the beginning...
fseek($tmp, 0);
// Upload temporary file to S3...
\Stripe\FileUpload::create(
array(
"purpose" => "identity_document",
"file" => $tmp
),
array("stripe_account" => CONNECTED_STRIPE_ACCOUNT_ID)
);
// Close temporary file and remove it...
fclose($tmp);
See https://secure.php.net/manual/en/function.tmpfile.php for more information.
My application uploads images to s3. I use front-end rendering to get the colors of the image, but because uploading to s3 lowers the quality (jpeg'ish), I get more colors then desired.
$s3 = \Storage::disk('s3');
$s3->put('/images/file.jpg', '/images/file.jpg', 'public');
Is there way to prevent this quality loss? I noticed that if I upload the file directly using aws console website, the quality stays the same which is ideal.
Thank you!
In the controller Action
public function uploadFileToS3(Request $request)
{
$image = $request->file('image');
}
Next we need to assign a file name to the uploaded file.You could leave this as the original filename, but in most cases you will want to change it to keep things consistent. Let’s change it to a timestamp, and append the file extension to it.
$imageFileName = time() . '.' . $image->getClientOriginalExtension();
Now get the image content as follows
$s3 = \Storage::disk('s3');
$filePath = '/images/file.jpg' . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
For further info you can refer to this
I am not familiar with Laravel but i am familiar with AWS S3, and i'm using aws-sdk-php.
As far as i know, neither AWS S3 nor php-sdk don't do something implicitly under the hood. So it must be something going wrong elsewhere in your project.
You can try use plain aws-sdk-php:
$s3 = S3Client::factory([
'region' => 'us-west-2',
'credentials' => $credentials,
'version' => 'latest',
]);
$s3->putObject([
'Bucket' => 'myBucket',
'Key' => 'test/img.jpg',
'SourceFile' => '/tmp/img.jpg',
]);
It works perfectly.
I'm trying to put a remote object to amazon s3, i'm using this code :
$s3 = Aws\S3\S3Client::factory();
$bucket = getenv('S3_BUCKET')?: die('No "S3_BUCKET" config var in found in env!');
$s3->putObject(array(
'Bucket' => $bucket,
'Key' => 'myvideo.mp4',
'Body' => 'http://example.fr/video.mp4'
));
this code is working but it's not uploading a full size file.
This doesn't do what you want:
'Body' => 'http://example.fr/video.mp4'
This sets the object body to contain the string of the URL... not the content from the remote URL.
To upload a "remote" object, you have to download it first. There is no built-in capability in S3 to fetch content from a remote URL.
I have a website here http://www.voclr.it/acapellas/ my files are hosting on my Amazon S3 Account, but when a visitor goes to download an MP3 from my website it forces them to stream it but what I actually want it to do is download it to there desktop.
I have disabled S3 on the website for now, so the downloads are working fine. but really I want S3 to search the MP3s
Basically, you have to tell S3 to override the content-disposition header of the response. You can do that by appending the response-content-disposition query string parameter to the S3 file url and setting it to the desired content-disposition value. To force download try:
<url>&response-content-disposition="attachment; filename=somefilename"
You can find this in the S3 docs. For information on the values that the content-disposition header can assume you can look here.
As an additional information this also works with Google Cloud Storage.
require_once '../sdk-1.4.2.1/sdk.class.php';
// Instantiate the class
$s3 = new AmazonS3();
// Copy object over itself and modify headers
$response = $s3->copy_object(
array( // Source
'bucket' => 'your_bucket',
'filename' => 'Key/To/YourFile'
),
array( // Destination
'bucket' => 'your_bucket',
'filename' => 'Key/To/YourFile'
),
array( // Optional parameters
**'headers' => array(
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment'**
)
)
);
// Success?
var_dump($response->isOK());
Amazon AWS S3 to Force Download Mp3 File instead of Stream It
I created a solution for doing this via CloudFront functions (no php required since it all runs at AWS by linking to the .mp3 file on Cloudfront with a ?title=TitleGoesHere querystring to force a downloaded file with that filename). This is a fairly recent way of doing things (as of August 2022). I documented my function and how I set up my "S3 bucket behind a CloudFront Distribution" here: https://stackoverflow.com/a/73451456/19823883