Tested and it’s working:
1) install v.1 of league flysystem library (documentation)
user@machine:~$ composer require league/flysystem-aws-s3-v3:^1.0
// 2) php code
class DigitalOceanSpacesClient {
protected $privateKey;
protected $publicKey;
protected $path;
protected $bucket;
protected $region;
protected $version;
protected $spacesEndpoint = 'https://BUCKET.REGION.digitaloceanspaces.com';
protected $error = false;
/** @var boolean $log If you should log errors and properties for debuging */
protected $log = false;
/** @var \League\Flysystem\Filesystem $client The `\League\Flysystem\Filesystem` class object */
protected $client;
/**
* Constructor.
*
* The passed argument is a hash array with the following keys and types:
* ```
* string privateKey
* string publicKey
* string path
* string bucket
* string region
* string version
* bool log
* ```
*
* @param array[] $arr A hash array of key-values for the class properties
*
* @return DigitalOceanSpacesClient A DigitalOceanSpacesClient object
*/
public function __construct(array $arr = array()) {
$this->privateKey = @$arr['privateKey'];
$this->publicKey = @$arr['publicKey'];
$this->path = @$arr['path'];
$this->bucket = @$arr['bucket'];
$this->region = @$arr['region'];
$this->version = @$arr['version'];
$this->log = (bool)@$arr['log'];
$this->setSpacesEndpoint($this->bucket, $this->region);
}
/**
* Dependency inversion principle.
*
* @return \League\Flysystem\Filesystem A `\League\Flysystem\Filesystem` object
*/
public function getClient() {
if(!isset($this->client)) {
$options = [
/*
* version 2
'driver' => 's3',
'key' => $this->publicKey,
'secret' => $this->privateKey,
'region' => $this->region,
'bucket' => $this->bucket,
'endpoint' => $this->spacesEndpoint
*/
'credentials' => [
'key' => $this->publicKey,
'secret' => $this->privateKey,
],
'region' => $this->region,
'version' => $this->version,
'endpoint' => $this->spacesEndpoint
];
// This is my custom method at Flysystem as I don't
// use composer and I want global functions to load
// on demand - don't care about composer's approach;
// just skip it if you use composer :((
//\League\Flysystem\Utils::run();
$client = new S3Client($options);
$adapter = new AwsS3Adapter($client, $this->bucket);
$this->client = new Filesystem($adapter);
}
return $this->client;
}
/**
* Calculates spaces url based on object properties.
*
* @param string $bucket The Spaces name
* @param string $region The Spaces region
*
* @return void
*/
public function setSpacesEndpoint(string $bucket, string $region) {
if($bucket && $region) {
$this->spacesEndpoint = \str_replace(['BUCKET.', '.REGION.'], [$bucket . '.', '.' . $region . '.'], $this->spacesEndpoint);
}
}
}
3) Now, it’s easy to build a \League\Flysystem\Filesystem
object
$arr = array(
'log' => false,
'privateKey' => 'private key',
'publicKey' => 'public key',
'path' => 'a filepath',
'bucket' => 'spaces name',
'region' => 'e.g. ams3'
);
$test = new DigitalOceanSpacesClient($arr);
Can you please help me with the configs
with Thanks
Of course! first of all from your digital ocean dashboard go to the api tab. Then look at your token/keys, specifically the spaces access keys. When you create a new key or regenerate one you will see the secret, you MUST take note as that is the only time you will see it (without regenerating again invalidating any apps attached). So now you have the key (AWSKEY in .env) and the secret (AWSSECRET). At the moment the only region available is NYC3 so it just set AWSREGION to NY3. When you created the space you will have called it something, whatever you called the space this will be the bucket name set AWSBUCKET to the name of the space. Finally the url was AWS_URL=https://nyc3.digitaloceanspaces.com.
Set all of these in your .env and then add an entry in your filesystems.php as below
You can then use the storage facade as follows
$disk = Storage::disk(‘DO’);
$content = filegetcontents($file);
$this->storage->put($filePath, $content , 'public’);
Where file is a posted file from a form.