PHPackages                             arraypress/s3-utilities - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. arraypress/s3-utilities

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

arraypress/s3-utilities
=======================

Offers utility methods for cleansing and validating input parameters associated with S3 operations.

021PHP

Since Nov 6Pushed 1y ago1 watchersCompare

[ Source](https://github.com/arraypress/s3-utilities)[ Packagist](https://packagist.org/packages/arraypress/s3-utilities)[ RSS](/packages/arraypress-s3-utilities/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

S3 Utilities Library
====================

[](#s3-utilities-library)

The S3 Utilities Library is a comprehensive toolkit designed for developers working with Amazon S3 and S3-compatible storage solutions. This library encompasses a wide range of functionalities including sanitization, validation, and serialization of S3 parameters, ensuring adherence to AWS standards and enhancing the safety and efficiency of cloud storage operations.

**Key Features:**

- **Comprehensive Sanitization:** Ensures that bucket names, object keys, endpoints, and other parameters conform to S3 standards by removing or replacing invalid characters.
- **Robust Validation:** Validates inputs to ensure they meet specific criteria for AWS access keys, bucket names, object keys, and more, preventing common errors.
- **Advanced Serialization:** Facilitates the correct encoding and decoding of object keys, crucial for handling special characters in S3 object paths.
- **Wide Compatibility:** Designed to work seamlessly with AWS S3 and various S3-compatible services like Linode, DigitalOcean Spaces, and BackBlaze.
- **Utility Functions:** Provides helper functions for validation and sanitization, simplifying common tasks and streamlining development.

Ensure your cloud storage interactions are optimized, secure, and compliant by leveraging the S3 Utilities Library.

Minimum Requirements
--------------------

[](#minimum-requirements)

- **PHP:** 7.4 or later

Installation
------------

[](#installation)

To integrate the S3 Utilities Library into your project, use Composer:

```
composer require arraypress/s3-utilities
```

### Including the Vendor Library

[](#including-the-vendor-library)

Include the Composer autoloader in your project to access the library:

```
require_once __DIR__ . '/vendor/autoload.php';
```

Sanitize Class Examples
-----------------------

[](#sanitize-class-examples)

The `Sanitize` class provides comprehensive methods for sanitizing various parameters for Amazon S3 operations, ensuring compliance with AWS S3 standards and enhancing data safety.

### accessKey (Sanitizing AWS Access Key ID)

[](#accesskey-sanitizing-aws-access-key-id)

```
use ArrayPress\S3\Sanitize;

$accessKey = "AKIA1234EXAMPLE!@#$%";
$sanitizedAccessKey = Sanitize::accessKey($accessKey);
echo $sanitizedAccessKey; // Outputs: "AKIA1234EXAMPLE"
```

### secretKey (Sanitizing AWS Secret Access Key)

[](#secretkey-sanitizing-aws-secret-access-key)

```
$secretKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY%^&*";
$sanitizedSecretKey = Sanitize::secretKey($secretKey);
echo $sanitizedSecretKey; // Outputs: "wJalrXUtnFEMIK7MDENGbPxRfiCYEXAMPLEKEY"
```

### objectKey (Sanitizing S3 Object Keys)

[](#objectkey-sanitizing-s3-object-keys)

```
$objectKey = "my_folder/my_file.txt*";
$sanitizedObjectKey = Sanitize::objectKey($objectKey);
echo $sanitizedObjectKey; // Outputs: "my_folder/my_file.txt"
```

### bucket (Sanitizing Bucket Names)

[](#bucket-sanitizing-bucket-names)

```
$bucketName = "My_Bucket-Name.123!";
$sanitizedBucketName = Sanitize::bucket($bucketName);
echo $sanitizedBucketName; // Outputs: "my-bucket-name.123"
```

### region (Sanitizing S3 Region Strings)

[](#region-sanitizing-s3-region-strings)

```
$region = "us-west-1_extra";
$sanitizedRegion = Sanitize::region($region);
echo $sanitizedRegion; // Outputs: "us-west-1"
```

### endpoint (Sanitizing Endpoints)

[](#endpoint-sanitizing-endpoints)

```
$endpoint = "https://my.endpoint.com/";
$sanitizedEndpoint = Sanitize::endpoint($endpoint);
echo $sanitizedEndpoint; // Outputs: "my.endpoint.com"
```

### duration (Sanitizing Duration Values)

[](#duration-sanitizing-duration-values)

```
$duration = -60;
$sanitizedDuration = Sanitize::duration($duration);
echo $sanitizedDuration; // Outputs: 60
```

### extra\_query\_string (Sanitizing Extra Query Strings)

[](#extra_query_string-sanitizing-extra-query-strings)

```
$extraQueryString = "param1=value1&param2=value2!";
$sanitizedQueryString = Sanitize::extra_query_string($extraQueryString);
echo $sanitizedQueryString; // Outputs: "param1=value1&param2=value2"
```

### key (Sanitizing Generic Keys)

[](#key-sanitizing-generic-keys)

```
$key = "my_key-123!";
$sanitizedKey = Sanitize::key($key);
echo $sanitizedKey; // Outputs: "my_key-123"
```

### bool (Sanitizing Boolean Values)

[](#bool-sanitizing-boolean-values)

```
$boolValue = "true";
$sanitizedBool = Sanitize::bool($boolValue);
echo $sanitizedBool ? 'true' : 'false'; // Outputs: true
```

### html (Sanitizing HTML Strings)

[](#html-sanitizing-html-strings)

```
$htmlString = "Some content";
$sanitizedHtml = Sanitize::html($htmlString);
echo $sanitizedHtml; // Outputs: "&lt;div&gt;Some content&lt;/div&gt;"
```

### url (Sanitizing URLs)

[](#url-sanitizing-urls)

```
$url = "http://example.com/page?query=";
$sanitizedUrl = Sanitize::url($url);
echo $sanitizedUrl; // Outputs: "http://example.com/page?query=%3Cunsafe%20string%3E"
```

Validate Class Examples
-----------------------

[](#validate-class-examples)

The `Validate` class offers a range of methods to ensure that your Amazon S3 operations use correctly formatted and compliant parameters.

### accessKey (Validating AWS Access Key ID)

[](#accesskey-validating-aws-access-key-id)

```
use ArrayPress\S3\Validate;

try {
    Validate::accessKey("AKIA1234EXAMPLE");
    echo "Access key is valid.";
} catch (InvalidArgumentException $e) {
    echo "Access key validation error: " . $e->getMessage();
}
```

### secretKey (Validating AWS Secret Access Key)

[](#secretkey-validating-aws-secret-access-key)

```
try {
    Validate::secretKey("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY");
    echo "Secret key is valid.";
} catch (InvalidArgumentException $e) {
    echo "Secret key validation error: " . $e->getMessage();
}
```

### objectKey (Validating S3 Object Keys)

[](#objectkey-validating-s3-object-keys)

```
try {
    Validate::objectKey("my_folder/my_file.txt");
    echo "Object key is valid.";
} catch (InvalidArgumentException $e) {
    echo "Object key validation error: " . $e->getMessage();
}
```

### bucket (Validating Bucket Names)

[](#bucket-validating-bucket-names)

```
try {
    Validate::bucket("my-bucket-name.123");
    echo "Bucket name is valid.";
} catch (InvalidArgumentException $e) {
    echo "Bucket name validation error: " . $e->getMessage();
}
```

### region (Validating S3 Region Strings)

[](#region-validating-s3-region-strings)

```
try {
    Validate::region( "us-west-1" );
    echo "Region is valid.";
} catch (InvalidArgumentException $e) {
    echo "Region validation error: " . $e->getMessage();
}
```

### endpoint (Validating Endpoints)

[](#endpoint-validating-endpoints)

```
try {
    Validate::endpoint( "my.endpoint.com" );
    echo "Endpoint is valid.";
} catch (InvalidArgumentException $e) {
    echo "Endpoint validation error: " . $e->getMessage();
}
```

### duration (Validating Duration Values)

[](#duration-validating-duration-values)

```
try {
    Validate::duration( 60 );
    echo "Duration is valid.";
} catch (InvalidArgumentException $e) {
    echo "Duration validation error: " . $e->getMessage();
}
```

### extraQueryString (Validating Extra Query Strings)

[](#extraquerystring-validating-extra-query-strings)

```
try {
    Validate::extraQueryString( "param1=value1&param2=value2" );
    echo "Extra query string is valid.";
} catch (InvalidArgumentException $e) {
    echo "Extra query string validation error: " . $e->getMessage();
}
```

Serialization Class Examples
----------------------------

[](#serialization-class-examples)

The `Serialization` class facilitates the encoding and decoding of S3 object keys to ensure safe and compliant storage paths.

### encodeObjectName (Encoding S3 Object Names)

[](#encodeobjectname-encoding-s3-object-names)

Encoding S3 object names into URL-safe representations is crucial for handling special characters correctly, especially when dealing with file paths that include spaces or reserved characters.

```
use ArrayPress\S3\Serialization;

$objectName = "my folder/my file.txt";
$encodedObjectName = Serialization::encodeObjectName( $objectName );
echo $encodedObjectName; // Outputs: "my%20folder/my%20file.txt"
```

This method ensures that spaces are encoded as `%20` and slashes (`/`) are preserved, making the string safe for use in URLs while retaining its significance as a path delimiter.

### decodeObjectName (Decoding URL-encoded S3 Object Keys)

[](#decodeobjectname-decoding-url-encoded-s3-object-keys)

Decoding is as important as encoding, especially when you need to retrieve and accurately identify resources based on URL-encoded paths.

```
$encodedKey = "my%20folder/my%20file.txt";
$decodedKey = Serialization::decodeObjectName( $encodedKey );
echo $decodedKey; // Outputs: "my folder/my file.txt"
```

This method converts URL-encoded representations back to their original form, allowing for the correct identification and handling of object names within your application.

By utilizing these serialization methods, you can ensure that object names are consistently and securely processed across your S3 operations, minimizing the risk of errors and enhancing data management practices.

Helper Functions for S3 Operations
----------------------------------

[](#helper-functions-for-s3-operations)

The helper functions `validate` and `sanitize` offer simplified interfaces for validating and sanitizing data respectively, by leveraging the `Validate` and `Sanitize` classes from the `ArrayPress\S3` namespace.

### Validate Helper Function

[](#validate-helper-function)

The `validate` function checks if a given value meets specific criteria defined in the `Validate` class methods. It returns `true` for successful validation or `false` otherwise.

```
$isValid = validate( 'bucket', 'my-valid-bucket-name' );
if ( $isValid ) {
    echo "Bucket name is valid.";
} else {
    echo "Bucket name is invalid or method doesn't exist.";
}
```

This function abstracts the complexity of direct validation calls and exception handling, making it easier to integrate into validation flows.

### Sanitize Helper Function

[](#sanitize-helper-function)

The `sanitize` function cleans a given value according to the rules specified in the `Sanitize` class methods. It directly returns the sanitized value.

```
// Should output the sanitized version of the object key, e.g., "my folder/my file.txt"
$sanitizedValue = sanitize( 'objectKey', 'my folder/my file.txt*' );
echo $sanitizedValue;
```

In case the specified method does not exist within the `Sanitize` class, an `InvalidArgumentException` is thrown, indicating an implementation error.

Note: Direct use of `sanitize` without checking for method existence might lead to exceptions if the method name is incorrect or not supported.

Utilizing these utility functions streamlines the process of ensuring that data interacting with Amazon S3 is properly formatted and validated, enhancing the security and reliability of your S3 operations.

Contributions
-------------

[](#contributions)

Contributions to this library are highly appreciated. Raise issues on GitHub or submit pull requests for bug fixes or new features. Share feedback and suggestions for improvements.

License: GPLv2 or later
-----------------------

[](#license-gplv2-or-later)

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity16

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/cd6eb8aff0903d87eb674d1ba3c5f3653899c0d7661504eb0deb7798ed86b643?d=identicon)[arraypress](/maintainers/arraypress)

---

Top Contributors

[![arraypress](https://avatars.githubusercontent.com/u/22668877?v=4)](https://github.com/arraypress "arraypress (5 commits)")

### Embed Badge

![Health badge](/badges/arraypress-s3-utilities/health.svg)

```
[![Health](https://phpackages.com/badges/arraypress-s3-utilities/health.svg)](https://phpackages.com/packages/arraypress-s3-utilities)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
