Search Our Database

How to upload and delete files in buckets using PHP

Last updated on |
by

Introduction

This guide will help you programmatically upload and delete files in a storage bucket using PHP, specifically with sample code compatible with both PHP 8.1 and PHP 8.2 (examples in this guide use PHP 8.2). This tutorial covers the installation of PHP, setting up necessary dependencies, and running scripts to upload and delete files in your bucket. The guide is intended for Debian-based and Red Hat-based distributions, so be sure to follow the appropriate installation steps for your environment.

 

Prerequisites

  • PHP 8.2 installed on your server.
  • Composer for managing PHP dependencies.
  • Access credentials for your bucket (access key, secret key, endpoint, and region).
  • Basic knowledge of command-line usage and editing configuration files.

 

Step-by-Step Guide

Step 1: Install PHP 8.2 and Composer

For Debian-based Distributions

Update the system and install PHP with required extensions:

sudo apt install software-properties-common 
sudo apt update 
sudo apt install php82 php-xml unzip wget

Download and install Composer:

wget https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer -O - -q | php -- --quiet
sudo mv composer.phar /usr/local/bin/composer

 

For Red Hat Enterprise Linux 9-based Distributions

Install PHP 8.2 with the necessary modules:

sudo dnf install epel-release
sudo dnf install http://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf install dnf-utils
sudo dnf module reset php
sudo dnf module install php:remi-8.2
sudo dnf install php8.2 php-xml unzip wget

Download and install Composer:

wget https://getcomposer.org/installer -O composer-installer.php
sudo php composer-installer.php --filename=composer --install-dir=/usr/local/bin
rm composer-installer.php

 

 

Step 2: Confirm PHP Installation

Verify that PHP 8.2 is installed correctly by checking the PHP version:

php -v

You should see output similar to the following:

PHP 8.2.11 (cli) (built: Oct 6 2023 09:47:18) (NTS)

 

 

Step 3: Download Sample Code and Install Dependencies

Download the provided sample code for uploading and deleting files:

wget https://www.ipserverone.info/wp-content/uploads/2023/10/php-8.tar.gz
tar -xf php-8.tar.gz
cd php-8
composer install

 

 

Step 4: Set Up Working Directory

Navigate to the src directory where the scripts are located:

cd src

 

 

Step 5: Configure Access Credentials and Endpoint

Open adapter.php in a text editor to set up your access credentials and endpoint information:

vim adapter.php

Edit the following lines to add your bucket credentials and region details:

'endpoint' => 'https://ap-southeast-mys1.oss.ips1cloud.com',
'region' => 'ap-southeast-mys1',
'credentials' => ['key' => 'ACCESS_KEY', 'secret' => 'SECRET_KEY'],
return new League\Flysystem\AwsS3V3\AwsS3V3Adapter($client, 'sample-bucket-01');

 

Note: If you’re unsure about the endpoint of your bucket, access the customer portal and check the object storage tab. In our example, we have a bucket named “sample-bucket-01” which has “ap-southeast-mys1.oss.ips1cloud.com” as an endpoint.

 

Configure your endpoint and region accordingly:

'endpoint': 'https://ap-southeast-mys1.oss.ips1cloud.com',
'region': 'ap-southeast-mys1'

 

 

 

Uploading Files

Step 1: Prepare Files for Upload

Create a directory for the assets you wish to upload and create a sample file:

mkdir assets
echo "Hello world" > assets/sample_upload.txt

 

Step 2: Configure uploadFile.php

Open uploadFile.php in a text editor:

vim uploadFile.php

Specify the name of the file after it is uploaded to the bucket and the path of the file to be uploaded. Edit line 16 as shown below:

$filesystem->write('uploaded_text.txt', file_get_contents('./assets/sample_upload.txt'));

 

Step 3: Run the Upload Script

Run the upload script to transfer the file to your bucket:

php uploadFile.php

If the upload is successful, no output will be displayed.

 

 

Deleting Files

Step 9: Configure deleteFile.php

Open deleteFile.php in a text editor:

vim deleteFile.php

Edit line 15 to specify the file you want to delete from the bucket:

$filesystem->delete('uploaded_text.txt');

 

Step 10: Run the Delete Script

Run the delete script to remove the specified file from your bucket:

php deleteFile.php

If the deletion is successful, there will be no output displayed.

 

 

Conclusion

This guide has shown you how to programmatically upload and delete files in a storage bucket using PHP. Configuring adapter.php with the correct credentials and endpoint details ensures secure and efficient file management in the bucket. For a guide on how to programmatically upload and delete files from buckets using JavaScript, refer to the following guide.

Should you have any inquiries about the guidelines, please feel free to open a ticket through your portal account or contact us at support@ipserverone.com. We’ll be happy to assist you further.

 

Article posted on 15 October 2023 by Louis