Last updated on August 22nd, 2018 at 06:22 pm

Welcome to TrinityTuts, In this latest blog, I’m going to explain to you how we can upload large statics file to Digital Ocean Space. Digital Ocean Space is same as AWS S3. Now for this post, I am going to use CakePHP 3 framework to do all uploading stuff. Now without wasting any time, I will explain you step by step so you can implement this in your project. You can also check video what we going to do on this project and after completing code part what we get and don’t forget to subscribe channel.

 

Steps to Setup Digital Ocean Space in your Application

Step 1. Login or Create your Digital Ocean Account.

Step 2. Step your Droplet and Set up your CakePHP 3 application.

Step 3. Now after all the setup is done we need to create a space. Click on create on top of the navigation bar and click on spaces as shown image below.

Now fill all the required details select your region give the name to your space and define access to your space and click on create a space.

Step 4. Now we can create a key through which we can access our Space data from our website to do so click on API link in top of navigation bar and after click scroll to bottom and click on Generate new Keys note we are going to generate a key for Space please check below image for reference

After creating a key, note down the key and secret of space we need that information in our next steps.

Step 5. Now our space is ready and we have key and secret to access that space now we need our application to connect with that space and perform curd operation on space. So first we need to install a plugin in our CakePHP 3. To install plugin we need to SSH access to server, log in to your server and navigate to your project directory and after that run 

composer require league/flysystem-aws-s3-v3

After running this plugin is add to your framework.

Step 6. Now open your controller and add function our you can update old function but before that, you can define this in top of file

use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;

Step 7. Now in your function where file are getting uploaded you need to add below code

public function saveToSpace(){

    $client = S3Client::factory([
        'credentials' => [
            'key' => 'YOUR_KEY',
            'secret' => 'YOUR_SECRET'
        ],
        'region' => 'sgp1', // Region you selected on time of space creation
        'endpoint' => 'https://sgp1.digitaloceanspaces.com',
        'version' => 'latest'
    ]);
    
    $adapter = new AwsS3Adapter($client, 'my_space_name');
    $filesystem = new Filesystem($adapter);
    $lists = $filesystem->listContents();

    if ($this->request->is('post')) {
        
        $stream = fopen($this->request->data['fileToUpload']['tmp_name'], 'r+');
        $filesystem->writeStream($this->request->data['fileToUpload']['name'], $stream, ['visibility' => 'public']);
        if (is_resource($stream)) {
            fclose($stream);
            $this->Flash->success(__('File uploaded successfully'));
            return $this->redirect(['action' => 'saveToSpace']);
        }
    }

    $this->viewBuilder()->layout('default');

    $this->set(compact('lists'));
}

And my template file contains below code

<div>
    <form action="" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>

    <div>
        <h3>Uploaded Images</h3>
        <?php
            foreach($lists as  $key){
                echo "<div><a href='https://your_bucket.sgp1.digitaloceanspaces.com/".$key['path']."' target='_bank'>".$key['path']."</a></div>";
            } 
        ?>
    </div>
</div>

In above function, you need to define your key and secret and also define your bucket/space information. If you want to get list of object exists in your bucket you can call this function

$filesystem->listContents();

Or if you want to make you upload private or public you can do that by simply put

$filesystem->writeStream('filename.jpg', "data", ['visibility' => 'private']);

That’s all 🙂 hope this post helps you. Please don’t forget to like my page and subscribe to my youtube channel Thanks.