Upload file to amazon S3 using the PHP SDK

I am trying to upload an image to my Amazon S3 through their PHP SDK. So I made a little script for this. However, my script does not work, and my exception does not send me any error messages.

I am new to AWS, thanks 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 am using this code now, but it still does not work. I don't even have an 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";
}

?>
+4
source share
5 answers

Try something like this (from 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";
}

?>

Name your file sample.phpand run it from the command line:

php sample.php

, . , - S3, , , , - : $keyname = 'image.jpg';. , jpg plain/text, Content-type : image/jpeg

+7
$s3 = S3Client::factory('config.php');

$s3 = S3Client::factory(include 'config.php');
+2

, , ,

// 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;
}
+2

- php - . , , , Azure PHP .

$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);

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'")
0

/ amazon s3.

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;
}
0

Source: https://habr.com/ru/post/1532047/


All Articles