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

Using more than one set of credentials or storage locations s3 compatible #39

Closed
zoomphoto opened this issue Mar 26, 2021 · 2 comments
Closed

Comments

@zoomphoto
Copy link

First, thank you for this git, it works perfect when pulling data from an s3 location.

I've been hacking the config and stream files to work in the following situation:

  1. We store all the files on s3 (one set of creds in the .env)
  2. After x number of days the files are transferred to our Minio cluster in the DC. (another set of creds in the .env)

The idea is that s3 if first checked with a try and catch exception then goes to the Minio file.

My only issue, is should I run a composer update, will I break my setup?

What do you suggest is the best way to pass the creds variables to be used in the Zip::create so that they are not broken on a future update?

Thanks!

@jszobody
Copy link
Member

jszobody commented Mar 26, 2021

If you have two different clients:

$s3Client = new Aws\S3\S3Client([
   // ...
]);

$minioClient = new Aws\S3\S3Client([
   // ...
]);

You can set the client on each individual file if you make it yourself:

use Zip;
use STS\ZipStream\Models\File;

$zip = Zip::create("download.zip")
    ->add(File::make("s3://path/to/file.txt")->setS3Client($s3Client))
    ->add(File::make("s3://path/to/another.txt")->setS3Client($minioClient));

Note the method here:

https://github.com/stechstudio/laravel-zipstream/blob/master/src/Models/S3File.php#L35

Of course this assumes you even know which client to use at this point. I suppose you could even stick a conditional in there:

$zip = Zip::create("download.zip")
    ->add(File::make("s3://path/to/file.txt")->setS3Client(
        $s3Client->doesObjectExist("bucket", "file.txt") ? $s3Client : $minioClient
    ))

That's all untested of course, but something along these lines should work.

Alternatively you could extend the S3File model and add custom logic in the getS3Client method:

class MyCloudFile extends S3File {
    public function getS3Client(): S3Client
    {
        // This assumes you have your two clients bound in the container
        return app('s3Client')->doesObjectExist($this->getBucket(), $this->getKey())
            ? app('s3Client')
            : app('minioClient');
    }
}

Then you need to make this file explicitly:

Zip::create("download.zip")
    ->add(MyCloudFile::make("s3://path/to/file.txt"));

Hope that helps!

@zoomphoto
Copy link
Author

This was the secret sauce here:

->add(File::make("s3://path/to/another.txt")->setS3Client($minioClient));

Works perfectly, thank you!

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