php & AWS: s3 file upload example

Trabla: php & AWS: s3 file upload example.

Server not in AWS (amazone web services ) infrastructure.

Solving: 

use Aws\S3\S3Client;

...

$bucket   = 'my-bucket'; //bucket on aws e.g. 'my-bucket/myfolder'
$filename = 'manifest.xml'; // file name will be set on amazone s3
$filepath = '/var/www/storage/manifest.xml'; //local file path
$contenttype = 'text/xml'; // e.g. application/zip , image/jpeg etc.
// type list here https://en.wikipedia.org/wiki/Internet_media_type


$keyname = 'YOUR_ACCESS_KEY_ID'; //small string
$secret = 'YOUR_SECRET_AWS_S3_KEY'; //big string


// Instantiate the client.
$s3 = S3Client::factory( array(
'key' => $keyname,
'secret'         => $secret
));

// Upload a file.
$result = $s3->putObject(array(
   'Bucket'       => $bucket, 
   'Key'          => $filename,
   'SourceFile'   => $filepath,
   'ContentType'  => $contenttype,
    'ACL'          => 'public-read' // file access permissions on aws after upload
    
));

return $result['ObjectURL'];

No comments:

Post a Comment