Created
November 13, 2025 16:19
-
-
Save olcortesb/536dbb368d9cd208e12cae7b01f61f3e to your computer and use it in GitHub Desktop.
PHP Sample SKD AWS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bash | |
| composer require aws/aws-sdk-php | |
| ``` | |
| <?php | |
| require 'vendor/autoload.php'; | |
| use Aws\S3\S3Client; | |
| use Aws\Exception\AwsException; | |
| $bucketName = $_ENV['S3_BUCKET'] ?? 'your-bucket-name'; | |
| $region = $_ENV['AWS_DEFAULT_REGION'] ?? 'us-east-1'; | |
| $s3Client = new S3Client([ | |
| 'version' => 'latest', | |
| 'region' => $region | |
| // Las credenciales se toman automáticamente de: | |
| // - Variables de entorno (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) | |
| // - IAM Role (si está en EC2/ECS/Lambda) | |
| // - ~/.aws/credentials | |
| ]); | |
| try { | |
| $result = $s3Client->listObjectsV2(['Bucket' => $bucketName]); | |
| foreach ($result['Contents'] ?? [] as $object) { | |
| echo $object['Key'] . "\n"; | |
| } | |
| } catch (AwsException $e) { | |
| echo "Error: " . $e->getMessage() . "\n"; | |
| } | |
| ?> | |
| ``` | |
| ``` | |
| # Con variables de entorno | |
| export AWS_ACCESS_KEY_ID=your_key | |
| export AWS_SECRET_ACCESS_KEY=your_secret | |
| export S3_BUCKET=your-bucket-name | |
| php s3_ls.php | |
| ``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment