Skip to content

Commit

Permalink
add the tool
Browse files Browse the repository at this point in the history
.
  • Loading branch information
ksere committed Feb 21, 2023
1 parent 9c9bce9 commit 86c13c5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Rename to .env and set your own values.
AWS_ACCESS_KEY=""
AWS_SECRET=""
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
.env
.env
composer.lock
53 changes: 53 additions & 0 deletions bin/s3-uploader
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env php
<?php
/**
* A simple command line tool to upload files to S3.
*/

use AsyncAws\SimpleS3\SimpleS3Client;

global $_composer_autoload_path;
global $argv;

$composer_autoload_path = $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';

if ( is_file( $composer_autoload_path ) === true ) {
require_once $composer_autoload_path;
} else {
die( 'Autoloader not found. Please run "composer install".' . PHP_EOL );
}

$dotenv = \Dotenv\Dotenv::createImmutable( getcwd() );
$dotenv->SafeLoad();

if ( ! isset( $_ENV ) || ! isset( $_ENV['AWS_ACCESS_KEY'] ) || ! isset( $_ENV['AWS_SECRET'] ) ) {
die( 'Please set AWS_ACCESS_KEY & AWS_SECRET in a .env file.' . PHP_EOL );
}

$s3 = new SimpleS3Client(
array(
'accessKeyId' => $_ENV['AWS_ACCESS_KEY'],
'accessKeySecret' => $_ENV['AWS_SECRET'],
'region' => $_ENV['AWS_REGION'] ?? 'us-east-2',
)
);

$bucket = isset( $argv[1] ) ? $argv[1] : false;
$file = isset( $argv[2] ) ? $argv[2] : false;

if ( ! $bucket || ! $file ) {
die( sprintf( 'Usage: %s target_bucket file_to_upload [target_name]', $argv[0] ) . PHP_EOL );
}

$target = isset( $argv[3] ) ? $argv[3] : basename( $file );

if ( ! file_exists( $file ) ) {
die( sprintf( 'File "%s" does not exist.', $file ) . PHP_EOL );
}

printf( 'Uploading file "%s" to bucket "%s"' . PHP_EOL, $file, $bucket );

$resource = \fopen( $file, 'r' );
$s3->upload( $bucket, $target, $resource );

echo 'Done!' . PHP_EOL;
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "saucal/s3-uploader",
"autoload": {
"psr-4": {
"Saucal\\S3Uploader\\": "src/"
}
},
"authors": [
{
"name": "Kostas Seressiotis",
"email": "kostas@saucal.com"
}
],
"bin": ["bin/s3-uploader"],
"require": {
"async-aws/simple-s3": "^1.1",
"vlucas/phpdotenv": "^5.4"
}
}

0 comments on commit 86c13c5

Please sign in to comment.