Wednesday, 10 July 2019

How to upload very large file in S3 using PHP?

How to upload very large file in S3 using PHP?

Step 1: Install AWS S3 Library (If not installed)


composer require aws/aws-sdk-php


Step 2: Get Access key/Secret Access Key (If you have not)


  1. Open the IAM console (https://console.aws.amazon.com/iam/home?#home).
  2. From the navigation menu, click Users.
  3. Select your IAM user name.
  4. Click User Actions, and then click Manage Access Keys.
  5. Click Create Access Key.
  6. Your keys will look something like this:
    Access key ID example: AKARUNKODNN7EXAMPLE Secret access key example: wJaXUtFDADnFEMI/K7MDDEG/bPxRfiDDDEXAMPLEKEY


Step 3:Upload the files


require 'vendor/autoload.php';

use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\MultipartUploader;
use Aws\S3\S3Client;
$myAwsKey='AWS_ACCESS_KEY';
myAwsSecretKey='AWS_SECRET_KEY';
 
$s3Media='s3-bucket-name';
$s3FileName='filename.mp4';

//Init S3 Object
try {
    $s3 = new S3Client([
        'version' => 'latest',
        'region'  => 'us-east-1',
        'credentials' => [
            'key'    => $myAwsKey,
            'secret' => $myAwsSecretKey,
        ]                
    ]);
} catch (MultipartUploadException $e) {
        echo $e->getMessage() . PHP_EOL;
        die;
} 

//Start uploading
try{
    $uploader = new MultipartUploader($s3, $staticFilePath.'/GM-hKStd-1211-28453_720p.mp4', 
        array(
        'bucket' => $s3Media,
        'key'    => $s3FileName,
        'ACL'    => 'public-read',
    )
    );                
}  catch (Exception $e){
    pr($e->getMessage());
}


// Perform the upload.
try {
    $result = $uploader->upload();
    //It will give you complete path of S3 URL
    echo "Upload complete: {$result['ObjectURL']}" . PHP_EOL;
} catch (MultipartUploadException $e) {
    echo $e->getMessage() . PHP_EOL;
}



Thursday, 6 June 2019

How to send email from zend framework 3?

How to send email from zend framework 3?
define('MR_MAIL_HOST','smtp.sendgrid.net');
define('MR_MAIL_PORT','587');
define('MR_MAIL_USERNAME','username');  
define('MR_MAIL_PASSWORD','password');  

$transport = new Mail\Transport\Smtp();
$options   = new Mail\Transport\SmtpOptions(array(
    'name'              => 'emailName',
    'host'              => MR_MAIL_HOST,
    'connection_class'  => 'login',
    'connection_config' => array(
        'username' => (MR_MAIL_USERNAME),
        'password' => (MR_MAIL_PASSWORD),
         'port' => MR_MAIL_PORT,
    ),
));
$transport->setOptions($options);

$mail = new Mail\Message();
$mail->setFrom('hello@domain.com');
$mail->setSubject('This is simple subject');
$mail->setBody('this is html body text');
$mail->addTo('arun@www.web-technology-experts-notes.in');
$transport->send($mail);