Posts By vishavjeet (23)

How to upload file on AWS S3 Bucket using Rest API

#Step1: Create Aws3 class which file path application/libraries/Guzzlehttp.php

Source Code:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

include("./vendor/autoload.php");

use GuzzleHttp\Client;

class Guzzlehttp{

     public function __construct(){

          $this->client = new \GuzzleHttp\Client();

     }

    public function sendMultiForm($end_point_url, $multipart){

          return $this->client->request('POST', $end_point_url, [ 'multipart' => $multipart ]);

    }

    public function sendForm($end_point_url, $form_params){

        return $this->client->request('POST', $end_point_url, ['form_params' =>$form_params]);

     }

 }

#Step2: load library in Controller

class Product_Api extends CI_Controller

{

      public function __construct()

     {

         parent::__construct();

        $this->load->library('guzzlehttp');

        date_default_timezone_set('Asia/Kolkata');

     } 

    public function addProductImage()

    {  

      

       $image_abs_path = 'uploads/products/'.$product_id.'/'.$img1_nm;

      $end_point_url="https://www.xyz.com/";

     $multipart=[

            ['name' => 'file_path_name','contents' =>$image_abs_path],

            ['name' => 'image_url', 'contents' => fopen($image_abs_path, 'r')]

       ];

     $response=$this->guzzlehttp->sendMultiForm($end_point_url, $multipart);

    // echo $body = $response->getBody();

    }




How to upload file on AWS S3 Bucket CDN using PHP CodeIgniter

#Step1: Create Aws3 class which file path application/libraries/Aws3.php

Source Code:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

include("./vendor/autoload.php");

use Aws\S3\S3Client;

use Aws\Exception\AwsException;

class Aws3{

private $S3;

public function __construct(){

$this->S3 = S3Client::factory([

'key' => 'XXXXXXX',

'secret' => 'XXXXXXX',

'region' => 'ap-south-1'

]);

}

    public function addBucket($bucketName){

    $result = $this->S3->createBucket(array(

    'Bucket'=>$bucketName,

    'LocationConstraint'=> 'ap-south-1'));

    return $result;

   }

  public function sendFile($bucketName, $file_path, $filename){

  $bucketName='vishavjeet-s3-repo';

   $result = $this->S3->putObject(array(

   'Bucket' => $bucketName,

   'Key' => $file_path,

  'SourceFile' => $filename['tmp_name'],

  'ContentType' => 'image/png',

  'StorageClass' => 'STANDARD',

   'ACL' => 'public-read'

  ));

return $result['ObjectURL']."\n";

}

  public function uploadfile_s3($keyName,$file)

  {

      $bucketName = 'vishavjeet-s3-repo';

     $IAM_KEY = 'XXXXX';

    $IAM_SECRET = 'XXXXXX';

    try {

       $s3 = S3Client::factory(

    array(

        'credentials' => array(

        'key' => $IAM_KEY,

       'secret' => $IAM_SECRET

      ),

   'version' => 'latest',

   'region' => 'ap-south-1'

    )

 );

} catch (Exception $e) { 

    die("Error: " . $e->getMessage()); 

  }

  try {

     $s3->putObject(

     array(

         'Bucket'=>$bucketName,

         'Key' => $keyName,

         'ACL' => 'public-read',

        'CacheControl' => 'max-age=172800',

        "Expires"=> gmdate("D, d M Y H:i:s T", strtotime("+7 days")),

        'SourceFile' =>$file,

        /*'StorageClass' => 'REDUCED_REDUNDANCY'*/

         )

     );

   } catch (S3Exception $e) {

        die('Error:' . $e->getMessage());

     } catch (Exception $e) {

           die('Error:' . $e->getMessage());

         }

      }

}


#Step2: load library in Controller

class Product_Api extends CI_Controller

{

      public function __construct()

     {

        parent::__construct();

       $this->load->library('aws3');

        date_default_timezone_set('Asia/Kolkata');

    }

  public function addImage()

  {

         $keyName = 'uploads/product/'.$product_id.'/'.$_FILES[$filename]['name'];

         $file = $_FILES[$filename]['tmp_name'];

      $response=$this->uploadfile_s3($keyName,$file)

   }

}

Python syntax compare to other programming language

Python syntax is very easy and understandable like English which can be write easily. It works smarty because of vast in-built Libraries.It is high level language or programmer friendly language.In python, we are using Interpreter for executing the program which is very fast and taking less time than complier. Python is used in AI.It is dynamically typed language.We don't need to compile the program as Java, C and C++.

First Previous 3 4 5