Last updated on March 2nd, 2019 at 04:18 pm
In my last post, I will explain how we can upload data to Digital Ocean Space now in this post I can show you how we can upload a private file and read that private file in PHP.
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Step 2. Now once composer is installed successfully then we need to add a plugin to upload and read files from Digital Ocean Space.
composer require league/flysystem-aws-s3-v3
Step 3. Now we can add this plugin in PHP file where we need. In below code, we can upload the file as a private file to Space.
<?php
include __DIR__ . '/vendor/autoload.php';
use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;
// Upload file to Space
$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'
]);
// Upload image file to server
$adapter = new AwsS3Adapter($client, 'your-bucket-name');
$filesystem = new Filesystem($adapter);
$imageName = "profile_images.jpeg";
$baseUrl = 'img/profile_images.jpeg';
$stream = fopen($baseUrl, 'r+');
$filesystem->writeStream('/sample/'.$imageName, $stream, ['visibility' => 'private']);
With the help of the above code, we can upload a private file to Digital Ocean Space.
Step 4. Now we successfully upload the private file now we need to read uploaded file. To read a private file in digital ocean space run below code.
<?php
include __DIR__ . '/vendor/autoload.php';
use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;
// Upload file to Space
$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'
]);
$cmd = $client->getCommand('GetObject', [
'Bucket' => 'your-bucket-name',
'Key' => 'sample/profile_images.jpeg'
]);
$request = $client->createPresignedRequest($cmd, '+1 minutes');
$presignedUrl = (string) $request->getUri();
If everything works fine you will get temporary Url like shown below:-
https://your-bucket-name.sgp1.digitaloceanspaces.com/sample/profile_images.jpeg?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AISVAHJSASHJMJVIQTI%2F20190228%2Fsgp1%2Fs3%2Faws4_request&X-Amz-Date=20190228T092753Z&X-Amz-SignedHeaders=host&X-Amz-Expires=60&X-Amz-Signature=a9d191d365744b998d006ba6c553f4a7a5084f78214ff5b649534Hope this above code helps you understand digital ocean space private file read and write operation. Please like and share this post. Happy coding 🙂