How to Create a Service in Drupal to Integrate AWS S3 and Fetch Data from Uploaded Files

In this post, I will guide you through the steps to create a custom service in Drupal that integrates with AWS S3. By the end of this tutorial, you’ll have a service that can fetch data from files uploaded to your S3 bucket. This integration can be particularly useful for handling large datasets, like CSV files, and processing them within Drupal.

Step 1: Install AWS SDK for PHP in Drupal

To start, we need to install the AWS SDK for PHP, which allows us to communicate with AWS S3 from within Drupal.

Substep 1: Install AWS SDK using Composer

This will download the required package and make it available to your project.

composer require aws/aws-sdk-php

Step 2: Create a Custom Module for S3 Integration

Next, we will create a custom module called s3_csv_importer that contains the AWS S3 integration logic.

Substep 1: Define the module

Create the s3_csv_importer.info.yml file in your module directory (/modules/custom/s3_csv_importer/).

name: 'S3 CSV Importer'
type: module
description: 'A module for integrating AWS S3 and fetching CSV data.'
core_version_requirement: ^10
package: Custom
dependencies:
  - aws/aws-sdk-php

Substep 2: Create the S3 Service

Now, let’s create the actual service that communicates with AWS S3.

<?php

namespace Drupal\s3_csv_importer\Service;

use Aws\S3\S3Client;
use Psr\Log\LoggerInterface;

/**
 * Class S3CsvImporter for importing files from AWS S3.
 */
class S3CsvImporter {

  protected $s3Client;
  protected $logger;

  public function __construct($aws_key, $aws_secret, $region, LoggerInterface $logger) {
    $this->logger = $logger;

    $this->s3Client = new S3Client([
      'version' => 'latest',
      'region' => $region,
      'credentials' => [
        'key'    => $aws_key,
        'secret' => $aws_secret,
      ],
    ]);
  }

  public function fetchFileData($bucket, $file_key) {
    try {
      $result = $this->s3Client->getObject([
        'Bucket' => $bucket,
        'Key'    => $file_key,
      ]);

      return (string) $result['Body'];
    }
    catch (\Exception $e) {
      $this->logger->error('Failed to fetch file from S3: @message', ['@message' => $e->getMessage()]);
      return NULL;
    }
  }
}

Step 3: Create a Configuration Form for AWS Credentials

We want to manage AWS credentials within the Drupal admin interface to make it easier to switch between environments (e.g., Dev, Test, Live).

Substep 1: Create the Form

<?php

namespace Drupal\s3_csv_importer\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Settings form for S3 CSV Importer module.
 */
class S3CsvImporterSettingsForm extends ConfigFormBase {

  protected function getEditableConfigNames() {
    return ['s3_csv_importer.settings'];
  }

  public function getFormId() {
    return 's3_csv_importer_settings_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('s3_csv_importer.settings');

    $form['aws_key'] = [
      '#type' => 'textfield',
      '#title' => $this->t('AWS Access Key'),
      '#default_value' => $config->get('aws_key'),
    ];

    $form['aws_secret'] = [
      '#type' => 'textfield',
      '#title' => $this->t('AWS Secret Key'),
      '#default_value' => $config->get('aws_secret'),
    ];

    $form['region'] = [
      '#type' => 'textfield',
      '#title' => $this->t('AWS Region'),
      '#default_value' => $config->get('region'),
    ];

    return parent::buildForm($form, $form_state);
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('s3_csv_importer.settings')
      ->set('aws_key', $form_state->getValue('aws_key'))
      ->set('aws_secret', $form_state->getValue('aws_secret'))
      ->set('region', $form_state->getValue('region'))
      ->save();

    parent::submitForm($form, $form_state);
  }
}

Step 4: Use the Service in a Controller to Fetch and Display Data

Finally, let’s create a controller that fetches data from S3 using the service we created and displays it on a Drupal page.

Substep 1: Create the Controller

<?php

namespace Drupal\s3_csv_importer\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\s3_csv_importer\Service\S3CsvImporter;

/**
 * Class S3CsvImportController.
 */
class S3CsvImportController extends ControllerBase {

  protected $s3CsvImporter;

  public function __construct(S3CsvImporter $s3CsvImporter) {
    $this->s3CsvImporter = $s3CsvImporter;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('s3_csv_importer.importer')
    );
  }

  public function fetchData() {
    $bucket = 'your-s3-bucket';
    $file_key = 'path/to/your/file.csv';
    $data = $this->s3CsvImporter->fetchFileData($bucket, $file_key);

    return [
      '#markup' => '<pre>' . print_r($data, TRUE) . '</pre>',
    ];
  }
}

Conclusion

We’ve now successfully created a service in Drupal that integrates with AWS S3. This setup allows you to fetch and display data from S3 buckets, and the service can easily be extended for more complex use cases, such as processing large datasets.

Get a Free Consultation Today!

At Krushna53, we specialize in building exceptional web applications using Drupal and JavaScript frameworks like ReactJs' in all the places wherever we have it. Our expert team is here to provide you with a customized solution that meets your unique business needs.

Schedule Your Free Consultation
Let's work together to create a website that drives growth and success. Click the button above to get started!
Contact Us

Copyright All Rights Reserved