phpqrcode + save cached file to Amazon instead of folder on server - php

What I do now is creating a QR code image (PNG) and save a copy of it in a folder on the root of my server. Now I would like to save this image not on my server but on my amazon bucket.
I know how I can save a file on amazon but I can't figure out how to make this work together.
This is my original code for saving it in a root folder on my server:
$fileName = $quiz_url . '.png';
$pngAbsoluteFilePath = APPLICATION_PATH . '/../public/qrcodecaches/' . $fileName;
$urlRelativeFilePath = '/qrcodecaches/' . $fileName;
// generating
if (!file_exists($pngAbsoluteFilePath)) {
QRcode::png('http://mysitelink.com/s/'.$quiz_url, $pngAbsoluteFilePath, 'L', 4, 2);
}
And this is how I save a file on Amazon:
$bucket = 'mybucket';
$map = 'qrcodecaches';
$client = S3Client::factory(array(
'key' => 'mykey',
'secret' => 'mysecret',
));
$fileName = $quiz_url . '.png';
$keyname = $map . '/' . $fileName;
try {
$client->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'Body' => fopen('/path/to/file', 'r'),
'ACL' => 'public-read',
));
} catch (S3Exception $e) {
echo "There was an error uploading the file.\n";
}
But what do I need to save in the 'Body' tag? Can somebody help me with this?

You can create the image file passing a temporary file name like this:
$pngAbsoluteFilePath = tempnam(sys_get_temp_dir(), 'qr_code_');
if (!file_exists($pngAbsoluteFilePath)) {
QRcode::png('http://mysitelink.com/s/'.$quiz_url, $pngAbsoluteFilePath, 'L', 4, 2);
}
then all you need to do is to get that file contents and pass it in to the Body of the object in bucket:
try {
$client->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'Body' => file_get_contents($pngAbsoluteFilePath), // like this
'ACL' => 'public-read'
));
}
catch (S3Exception $e) {
echo "There was an error uploading the file.\n";
}
After you're done - in case of successful upload - you can delete the temporary file. It is not necessary, however recommended because it will fill your /tmp with QR images until your server is restarted:
if ( empty($e) ) {
unlink($pngAbsoluteFilePath);
}
This worked well for me.

Amazon S3Client documentation suggests that you can poll to check that the upload was successful.
$client->waitUntilObjectExists(array(
'Bucket' => $bucket,
'Key' => $keyname
));
Alternatively if you're already writing the file to disk on your server you can supply S3Client with the path to that file and let it handle the filestream itself.
try {
$client->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => '/path/to/file',
'ACL' => 'public-read'
));
} catch (S3Exception $e) {
echo "There was an error uploading the file.\n";
}
Source: http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_putObject

Related

Uploading image to s3 bucket usket using aws sdk php

I am trying to move a file from my php code to s3 bucket using this code:
$bucket = "bucket-name";
$s3 = S3Client::factory(array(
'key' => 'xxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
));
$baseImageUrl = "http://www.xxxxx.com/banner_mobile.jpg";
$keyname = "31.jpg";
try {
// Upload data.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'Body' => $baseImageUrl,
'ContentType' => 'image/jpg',
'ACL' => 'public-read'
));
// Print the URL to the object.
echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
} catch(Exception $ex){
echo $ex->getMessage()."\n";
}
An empty file (or corrupted file) gets uploaded to s3 bucket. The actual size of image is 37KB, but the uploaded file was of size 40B. What is the reason for this?
As it seems you're inserting the url as the body of the file, so you're uploading a image file with text, if you want to upload that file you'll need to have the path or the content of the file

Download files from AWS S3 to client system

I am trying to provide download file option to my users. I am working on AWS EC2 with AWS PHP SDK V2.8. I am able to display images on my website. I try according to question force-download-with-php-on-amazon-s3 but no success. Most of the answer of this question are pretty old. I am using below code for uploading
try {
$result = $s3->putObject(array(
'Bucket' => $bucketName,
'ACL' => 'authenticated-read',
'Key' => "s3112.png",
'ServerSideEncryption' => 'AES256',
'SourceFile' => $filepath,
'ContentType' => mime_content_type($filepath),
'debug' => [
'logfn' => function ($msg) {
echo $msg . "\n";
},
'stream_size' => 0,
'scrub_auth' => true,
'http' => true,
],
));
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
Here is what is tried.
header('Content-disposition: attachment; filename=s3112.png');
header('Content-type: image/png');
readfile("https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png");
//header("Location: https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png");
//// Location redirection to a MP3 that lets the browser decide what to do.
//header("Location: https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png");
I tried with
<a href="https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png" download>
but no success. Any help appreciate.
If the object has public-read access, you should just be able to link to it.
You can also set read access to all objects in a bucket using a bucket policy.
You could also redirect to an S3 presigned URL of the object:
$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => 'my-bucket',
'Key' => 'testKey'
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
// Get the actual presigned-url
$presignedUrl = (string) $request->getUri();
header('Location: ' . $presignedUrl);

Upload resized image on amazon S3 with PHP SDK

I have a script wich resize and crop an image and I would like to upload the image on my amazon S3 on the fly.
The problem is that I get an error message when I try to run my script because I guess the sourcefile is not recognized as a direct path from the disk ($filepath). Do you have any idea to get through this situation?
Fatal error: Uncaught exception 'Aws\Common\Exception\InvalidArgumentException' with message 'You must specify a non-null value for the Body or SourceFile parameters.' in phar:///var/www/submit/aws.phar/Aws/Common/Client/UploadBodyListener.php:...
$myResizedImage = imagecreatetruecolor($width,$height);
imagecopyresampled($myResizedImage,$myImage,0,0,0,0, $width, $height, $origineWidth, $origineHeight);
$myImageCrop = imagecreatetruecolor(612,612);
imagecopy( $myImageCrop, $myResizedImage, 0,0, $posX, $posY, 612, 612);
//Save image on Amazon S3
require 'aws.phar';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'yol';
$keyname = 'image_resized';
$filepath = $myImageCrop;
// Instantiate the client.
$s3 = S3Client::factory(array(
'key' => 'private-key',
'secret' => 'secrete-key',
'region' => 'eu-west-1'
));
try {
// Upload data.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ACL' => 'public-read',
'ContentType' => 'image/jpeg'
));
// Print the URL to the object.
echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
You need to convert your image resource to an actual string containing image data. You can use this function to achieve this:
function image_data($gdimage)
{
ob_start();
imagejpeg($gdimage);
return(ob_get_clean());
}
You are setting the SourceFile of the upload to $filepath, which is assigned from $myImageCrop = imagecreatetruecolor(...). As such, it's not actually a path at all — it's a GD image resource. You can't upload these to S3 directly.
You'll need to either write that image out to a file (using e.g. imagejpeg() + file_put_contents()), or run the upload using data in memory (again, from imagejpeg() or similar).
require 'aws.phar';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'kkkk';
$keyname = 'test';
// $filepath should be absolute path to a file on disk
$newFielName = tempnam(null,null); // take a llok at the tempnam and adjust parameters if needed
imagejpeg($myImageCrop, $newFielName, 100); // use $newFielName in putObjectFile()
$filepath = $newFielName;
// Instantiate the client.
$s3 = S3Client::factory(array(
'key' => 'jjj',
'secret' => 'kkk',
'region' => 'eu-west-1'
));
try {
// Upload data.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ACL' => 'public-read',
'ContentType' => 'image/jpeg'
));
// Print the URL to the object.
echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}

Upload file on amazon S3 with PHP SDK

I'm trying to upload a picture on my amazon S3 via their PHP SDK. So I made a little script to do so. However, my script doesn't work and my exception doesn't send me back any error message.
I'm new with AWS thank you for your help.
Here is the code :
Config.php
<?php
return array(
'includes' => array('_aws'),
'services' => array(
'default_settings' => array(
'params' => array(
'key' => 'PUBLICKEY',
'secret' => 'PRIVATEKEY',
'region' => 'eu-west-1'
)
)
)
);
?>
Index.php
<?php
//Installing AWS SDK via phar
require 'aws.phar';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'infact';
$keyname = 'myImage';
// $filepath should be absolute path to a file on disk
$filepath = 'image.jpg';
// Instantiate the client.
$s3 = S3Client::factory('config.php');
// Upload a file.
try {
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filePath,
'ContentType' => 'text/plain',
'ACL' => 'public-read',
'StorageClass' => 'REDUCED_REDUNDANCY'
));
// Print the URL to the object.
echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
?>
EDIT : I'm now using this code but its still not working. I don't even have error or exception message.
<?php
require 'aws.phar';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'infactr';
$keyname = 'sample';
// $filepath should be absolute path to a file on disk
$filepath = 'image.jpg';
// Instantiate the client.
$s3 = S3Client::factory(array(
'key' => 'key',
'secret' => 'privatekey',
'region' => 'eu-west-1'
));
try {
// Upload data.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filePath,
'ACL' => 'public-read',
'ContentType' => 'image/jpeg'
));
// Print the URL to the object.
echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
?>
Try something like this (from the AWS docs):
<?php
require 'aws.phar';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = '<your bucket name>';
$keyname = 'sample';
// $filepath should be absolute path to a file on disk
$filepath = '/path/to/image.jpg';
// Instantiate the client.
$s3 = S3Client::factory(array(
'key' => 'your AWS access key',
'secret' => 'your AWS secret access key'
));
try {
// Upload data.
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ACL' => 'public-read'
));
// Print the URL to the object.
echo $result['ObjectURL'] . "\n";
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
?>
It works fine for me as long as you have the right credentials. Keep in mind that the key name is the name of your file in S3 so if you want to have your key have the same name of your file you have to do something like: $keyname = 'image.jpg'; . Also, a jpg is generally not a plain/text file type, you can ommit that Content-type field or you can just simply specify: image/jpeg
$s3 = S3Client::factory('config.php');
should be
$s3 = S3Client::factory(include 'config.php');
For those looking an up to date working version, this is what I am using
// Instantiate the client.
$s3 = S3Client::factory(array(
'credentials' => [
'key' => $s3Key,
'secret' => $s3Secret,
],
'region' => 'us-west-2',
'version' => "2006-03-01"
));
try {
// Upload data.
$result = $s3->putObject(array(
'Bucket' => $s3Bucket,
'Key' => $fileId,
'SourceFile' => $filepath."/".$fileName
));
return $result['ObjectURL'];
} catch (S3Exception $e) {
return false;
}
An alternative way to explain is by showing the curl, and how to build it in php - the pragmatic approach.
Please don't stone me for ugly code, just thought that this example is easy to follow for uploading to Azure from PHP, or other language.
$azure1 ='https://viperprodstorage1.blob.core.windows.net/bucketnameAtAzure/';
$azure3 ='?sr=c&si=bucketnameAtAzure-policy&sig=GJ_verySecretHashFromAzure_aw%3D';
$shellCmd='ls -la '.$outFileName;
$lsOutput=shell_exec($shellCmd);
#print_r($lsOutput);
$exploded=explode(' ', $lsOutput);
#print_r($exploded);
$fileLength=$exploded[7];
$curlAzure1="curl -v -X PUT -T '" . $outFileName . "' -H 'Content-Length: " . $fileLength . "' ";
$buildedCurlForUploading=$curlAzure1."'".$azure1.$outFileName.$azure3."'";
var_dump($buildedCurlForUploading);
shell_exec($buildedCurlForUploading);
This is the actual curl
shell_exec("curl -v -X PUT -T 'fileName' -H 'Content-Length: fileSize' 'https://viperprodstorage1.blob.core.windows.net/bucketnameAtAzure/fileName?sr=c&si=bucketNameAtAzure-policy&sig=GJ_verySecretHashFromAzure_aw%3D'")
Below are the code for upload image/file in amazon s3 bucket.
function upload_agreement_data($target_path, $source_path, $file_name, $content_type)
{
$fileup_flag = false;
/*------------- call global settings helper function starts ----------------*/
$bucketName = "pilateslogic";
//$global_setting_option = '__cloud_front_bucket__';
//$bucketName = get_global_settings($global_setting_option);
/*------------- call global settings helper function ends ----------------*/
if(!$bucketName)
{
die("ERROR: Template bucket name not found!");
}
// Amazon profile_template template js upload URL
$target_profile_template_js_url = "/".$bucketName."/".$target_path;
// Chatching profile_template template js upload URL
//$source_profile_template_js_url = dirname(dirname(dirname(__FILE__))).$source_path."/".$file_name;
// file name
$template_js_file = $file_name;
$this->s3->setEndpoint("s3-ap-southeast-2.amazonaws.com");
if($this->s3->putObjectFile($source_path, $target_profile_template_js_url, $template_js_file, S3::ACL_PUBLIC_READ, array(), array("Content-Type" => $content_type)))
{
$fileup_flag = true;
}
return $fileup_flag;
}

AWS S3 PHP 2.0 - How to upload an image from computer to a bucket?

I've been trying to figure out how to push an image to an S3 bucket using the new PHP 2.0 SDK. All I've found are tutorials for how to upload an image from your web server rather than from a local computer.
Here is the code that I'm using
$result = $s3->putObject(array(
'Bucket' => $bucketname,
'Key' => $filename,
'SourceFile' => $path,
'ACL' => 'public-read',
'ContentType' => 'image/jpeg',
'StorageClass' => 'STANDARD'
));
$filename is just the file name that I want to appear in the bucket and $path is the local path to the file on my computer. This puts a file onto the bucket but when I try to open the image, it just shows up as an empty screen with the no image thumb nail. I checked and it seems to only be uploading like 30 bytes. Can someone please point me in the right direction?
So in order to upload you need to specify the Body as well. So if you're uploading from your computer this would be the code
$s3 = $aws->get('s3');
$filename = $_FILES['file']['name'];
$tmpFile = $_FILES['file']['tmp_name'];
$imageType = $_FILES['file']['type'];
// Upload a publicly accessible file.
// The file size, file type, and MD5 hash are automatically calculated by the SDK
try {
$s3->putObject(array(
'Bucket' => $bucketname,
'Key' => $filename,
'Body' => fopen($tmpFile, 'r'),
'ACL' => 'public-read',
'ContentType' => $imageType,
'StorageClass' => 'STANDARD'
));
} catch (S3Exception $e) {
echo "There was an error uploading the file.\n";
}
where $tmpFile is the path to your file on your computer. I'm using an upload mechanism hence why i use temp. but you can just put the static path.
I had the same problem and Shahin's answer is correct, you can set
'Body' => fopen($tmpFile, 'r'),
However when I did this on my WAMP localhost, I got the error
Warning: fopen(C:\Windows\Temp\phpBDF3.tmp): failed to open stream: No such file or directory
This seems to be a windows permissions issue, and you can resolve it by copying the windows temp file to another temp file in a directory where there are no permissions problems (eg web root):
// $tmpFile was 'C:\Windows\Temp\php8A16.tmp';
// create a new temp file in webroot, and copy the windows tempfile there
$new_temp_location = tmpfile();
$new_temp_location = basename($tmpFile);
copy($tmpFile, $new_temp_location);
// now put the file in the bucket
try {
$s3->putObject(array(
'Bucket' => $bucketname,
'Key' => $filename,
'Body' => fopen($new_temp_location, 'r'),
'ACL' => 'public-read',
'ContentType' => $imageType,
'StorageClass' => 'STANDARD'
));
} catch (S3Exception $e) {
echo "There was an error uploading the file.\n";
}
It worked for me - hope it saves someone else some time...
EDIT:
Or slightly simpler, you can use
'SourceFile' => $new_temp_location,
Instead of
'Body' => fopen($new_temp_location, 'r'),

Categories