Skip to content

Instantly share code, notes, and snippets.

@sodonnell
Last active March 7, 2019 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sodonnell/ab0fe94d68f119bbd90a1dc03fc140d1 to your computer and use it in GitHub Desktop.
Save sodonnell/ab0fe94d68f119bbd90a1dc03fc140d1 to your computer and use it in GitHub Desktop.
Using the AWS PHP SDK to upload files to S3 bucket using EC2 Instance Profile (IAM Role) Credentials
<?php
require_once 'vendor/autoload.php';
use Aws\Credentials\CredentialProvider;
use Aws\S3\S3Client;
$provider = CredentialProvider::instanceProfile();
$mprovider = CredentialProvider::memoize($provider);
$s3 = new S3Client([
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => $mprovider
]);
$data = [
'Bucket' => 'my-s3-bucket',
'Key' => 'path/to/upload/file/to.txt',
'Body' => 'Hello, S3.',
'ACL' => 'public-read'
];
$result = $s3->putObject($data);
print_r($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment