@raven_corwin
To upload a file into Amazon S3 from an EC2 instance using PHP, you can use the AWS SDK for PHP. Here's a step-by-step guide on how to upload a file to S3 from an EC2 instance:
1 2 3 4 5 |
{ "require": { "aws/aws-sdk-php": "^3.0" } } |
Then, run composer install
to install the SDK.
1 2 3 |
require 'vendor/autoload.php'; use AwsS3S3Client; use AwsCredentialsCredentialProvider; |
1 2 3 |
[default] aws_access_key_id = YOUR_ACCESS_KEY aws_secret_access_key = YOUR_SECRET_KEY |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$bucket = 'your-bucket-name'; $key = 'path/to/uploaded/file.txt'; $filename = 'path/to/local/file.txt'; $client = new S3Client([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => 'latest' ]); $result = $client->putObject([ 'Bucket' => $bucket, 'Key' => $key, 'Body' => fopen($filename, 'r'), 'ACL' => 'public-read' ]); echo 'File uploaded successfully'; |
Replace 'your-bucket-name'
, 'path/to/uploaded/file.txt'
, and 'path/to/local/file.txt'
with your own values.
That's it! This is how you can upload a file to Amazon S3 from an EC2 instance using PHP.