Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please kindly explain this code to me #429

Open
mitmelon opened this issue Mar 30, 2023 · 1 comment
Open

Please kindly explain this code to me #429

mitmelon opened this issue Mar 30, 2023 · 1 comment

Comments

@mitmelon
Copy link

// server.php
// composer require aws/aws-sdk-php

use Aws\S3\S3Client;
use TusPhp\Tus\Server;
use Aws\Credentials\Credentials;

$awsAccessKey = 'AWS_ACCESS_KEY'; // YOUR AWS ACCESS KEY
$awsSecretKey = 'AWS_SECRET_KEY'; // YOUR AWS SECRET KEY
$awsRegion    = 'eu-west-1';      // YOUR AWS BUCKET REGION
$basePath     = 's3://your-bucket-name';

$s3Client = new S3Client([
    'version' => 'latest',
    'region' => $awsRegion,
    'credentials' => new Credentials($awsAccessKey, $awsSecretKey)
]);
$s3Client->registerStreamWrapper();

$server = new Server('file');
$server->setUploadDir($basePath);

$response = $server->serve();
$response->send();

exit(0);

My question is that how does the file gets transfered to s3 storage because I can see that $s3Client is not been used in Server() constructor, then how does $s3Client know where the file is for upload. The setUploadDir($basePath); is only using the basePath of the s3 location but not the $s3Client. Its not possible to use just the basepath to upload file to s3 without authorization.

Please i need clear explanations on how the code works above or is the code not complete?

@dimisdas
Copy link

The $s3Client->registerStreamWrapper() line registers the Amazon S3 stream wrapper, which allows you to use Amazon S3 buckets as a file system. This means you can use file system functions like fopen(), file_get_contents(), etc., with Amazon S3 paths (e.g., s3://your-bucket-name/file.txt).

When you call $server->setUploadDir($basePath), you're setting the upload directory to the S3 bucket path. The Tus server will use this path to store the uploaded files.

Under the hood, when the Tus server receives an upload request and saves the file using the S3 path, the registered stream wrapper intercepts the file system operations and translates them into the appropriate Amazon S3 API calls using the $s3Client instance. This is how the file gets transferred to the S3 storage.

So, even though $s3Client is not explicitly used in the Server() constructor or setUploadDir() method, it's still being utilized by the stream wrapper to handle the file upload to S3.

The code looks complete to me. As long as you have provided the correct AWS access key, secret key, region, and bucket name, it should work as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants